Git Commands Adding, Committing and Branching Cheatsheet
Here are a list of Git commands I want to remember for myself, mainly but I highly recommend that you bookmark this for future reference. Email yourself and friends the URL if you have to. I’ve come back to it multiple times myself. :)
Store Your Name and Email for Commit Messages
The following allows you to store your name and email address and have them be associated to each commit message.
git config --global user.name "YourFirstName YourLastName" git config --global user.email 'YourEmail@domain.com' git config --list cat .gitconfig
Creating Git Command Aliases
git config --global alias.co checkout git config --global alias.ci commit git config --global alias.lc log ORIG_HEAD.. --stat --no-merges (if you want to list all commits after you fetched but exclude merges)
Initializing an Empty Git Repository
This creates the .git directory which contains all of the Git files, directories and hooks. Hooks can be executed after certain events. More on hooks maybe later.
cd [root of project directory] git init (creates a .git directory)
Ignoring Files from being tracked
The patterns of files to exclude should be saved in the .gitignore file. This file will be checked into Git and is part of the project. So clones will get the .gitignore file.
create a .gitignore file in root of project directory with the following contents: *.log The .git/info/exclude file is your personal copy which doesn't get committed into Git.
Adds All files and sub-directories
However, this doesn’t commit to local repository yet. You must commit the changes. See below.
git add . git status (tells you which files have been added and ready to be committed)
Undos an add
A git add FILENAME/DIRNAME will add files/directories to the index.
If for some reason you’d like to undo the git add, do the following:
git reset FILENAME/DIRNAME git status (to confirm your files now show under the 'Untracked files' section)
Get HEAD version of Master
* Be careful with the following as it will set all of your local changes to HEAD version of master branch.
Change ‘master’ to another branch name if you want your local branch to be the same as the same branch in the origin repo.
git fetch git reset --hard origin/master
Reverting All of Your Local Changes
* Be careful with the following as it will revert all of your local changes to HEAD version.
git status (these are the files which will be reverted when executing the following) git checkout -f
Reverting All of Untracked Files and Directories
* Be careful with the following as it will revert all of your local changes to HEAD version.
git status (these are the files which will be reverted when executing the following) git clean -fd git pull
Commit files to Git to Local Repository for First Time
See below to see how to commit files subsequent times.
git status (tells you which files have been added and ready to be committed) git commit -m "First Commit"
To see which files have been committed to the Local Git Repository
git ls-files
To Remove a File that’s already committed
git rm [filename]
To Amend to last Git Commit
* This can only be done after you have committed and prior to pushing changes to remote repository.
git commit --amend
Commit files to Git to Local Repository Subsequent Times
git commit -a is the same as doing a git add [file name] followed by git commit -m “your commit message”
The git commit -a option will automatically include all files that need to be added to the commit.
git status (tells you which files have been added and ready to be committed) git commit -a Then edit notes in the file and save it. git commit -m "your description of what was changed" -a (you can do this instead of git commit -a. This will prevent the editor from being opened though)
To Include a diff of contents in the commit message
git commit -v
To see a log history of Git commits
git log
To see a log history of Git commits of a given file
git log -- filename
To see change history of given file
gitk filename OR git log -p filename
To open a Commit Viewer for Git
If you receive an error saying “Error reading commits:fatal:unrecognized arguments: -all”, it’s because the all flag option requires two hyphens. See below:
gitk --all
To See a List of Local Branches
The name of the colored branch indicates the branch you are working on currently.
git branch --color
To See a List of All Branches (including remote ones)
git branch -a
To Delete a Local Branch
You can first get a list of branches as described above and then do the following:
git branch -d [branch_name]
To Delete a Remote Branch
Beware to make sure this is really what you want to do!
You can first get a list of remote branches as described above and then execute the command below.
* Be sure the branch name is preceded with a colon. Notice it below.
git push origin :branch_name
To Create a New Branch and Switch to It
It’s important to remember that if you add a file in one branch, the file won’t exist in another branch, unless you add it or merge it.
git branch [branch name] git checkout [branch name] git co [branch name] (you can use co if you've set up the alias co above) git branch --color
To Rename a Branch to one that does NOT exist already
git branch -m old_branch new_branch
To Rename a Branch to one that already exists (overwrite existing
Caution: This will overwrite the existing branch!
git branch -M old_branch existing_branch_to_overwrite
To Push Local Branch to Remote Repository
When you create a branch in your local repo, it’s not automatically added to the remote repo; You need to push it there.
git push origin [branch name]
To Push all Local Branches and Tags to Repository
git push origin '*:*'
To See Remote Branches and Switch to Them
git fetch origin git checkout --track origin/[branch_name]
To Clone a Repository and its Files/Directories
For example, you may want to clone an existing repository that’s located on the same server as where you want to create the new clone. OR you may want to create the new clone on your local computer from the source repository which is on a shared hosting account.
git clone /source/repository/path /destination/repository/path (assumes the source repository is on the same server as where you want to clone) OR cd [to_a_directory_above_where_you_want_the_project_files_to_go] git clone username@yourdomain.com/~/git/yourproject.git (to clone a remote git repository)
To Sync files of this clone to HEAD of other clone
cd this_repository_directory (one directory above .git) git pull /the/other/clone HEAD
Example of Merging Code in Git
In this example, I’ll show you how to do a merge, after a conflict, between code of two branches. This is the case where Git couldn’t auto-merge.
git checkout master (switches you to the master branch) vi testfile (add the following line in testfile and save) adfasfasdfadf asdf a sdf asdf asdfasdf;lkja;kladjsf;klj git add testfile (allows it to be tracked by Git) git commit -m "adding content for master branch" -a git branch test (create the test branch) git checkout test (switches you to the test branch) vi testfile (delete all of the existing content and make your testfile in the test branch look like this:) ;lkajsdf;lkajdsl;kajsdf ;lkajsdf;ladjsl;kadjsf asdf asd fasd fadsf git commit -m "adding content for test branch" -a git merge master (merge contents of this test branch with the master branch. see below) your_username$ git merge master Auto-merged testfile CONFLICT (content): Merge conflict in testfile Automatic merge failed; fix conflicts and then commit the result. vi testfile (edit this file, resolve the conflicts by getting rid of the <<<<<<< HEAD:testfileD:testfile, ======= and >>>>>>> master:testfile and make the file look like how it should) Assume the file's contents should look like this: ;lkajsdf;lkajdsl;kajsdf ;lkajsdf;ladjsl;kadjsf asdf asd fasd fadsf asdfasdf;lkja;kladjsf;klj git commit -m "adding content for master branch" -a (to commit your changes)
Related Article: Setting up Private Git Repository on Shared Hosting Server, BlueHost
If you’d like to set up a Git repository on a shared hosting account, see: Setting up Private Git Repository on Shared Hosting Server, BlueHost
Git Commands Adding, Committing and Branching Cheatsheet,
4 Comments to “Git Commands Adding, Committing and Branching Cheatsheet”