Git cheat sheet

GIT

A quick reference guide to the most common and useful Git commands.

Create

Create a new local repo from existing files.

$ cd myproject
$ git init
$ git add .
$ git commit

Clone an existing remote repo

$ git clone git://example.com/myproject
$ cd myproject

... or just the repo contents (note the . at the end of the clone command)

$ mkdir myproject
$ cd myproject
$ git clone git://example.com/myproject .

Browse

$ git status
# On branch master
nothing to commit (working directory clean)
$ git branch -a
* master
  remotes/origin/master

Branch

Checkout a new local branch (and switch to it) then create remotely too.

$ git checkout -b dev
Switched to a new branch 'dev'
$ git branch -a
* dev
  master
  remotes/origin/master
$ git push origin dev
$ git branch -a
* dev
  master
  remotes/origin/dev
  remotes/origin/master

Checkout an existing local branch, update from remote, merge in another and update to remote.

$ git checkout master
Switched to branch 'master'
$ git pull origin master
$ git merge dev
$ git push origin master

Files

Add new files to be tracked and commit.

$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add ..." to include in what will be committed)
#
#	myfile.html
nothing added to commit but untracked files present (use "git add" to track)  
  
$ git add myfile.html  
  
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached ..." to unstage)
#
# new file: myfile.html
#  
  
$ git commit -m"My commit message"
[master (root-commit) b46f4f8] My commit message
 0 files changed
 create mode 100644 myfile.html

Add modified files via interactive method to be staged and commit.

$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
#
# Changes not staged for commit:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#
#	modified:   anotherfile.html
#	modified:   myfile.html
#
no changes added to commit (use "git add" and/or "git commit -a")  
  
$ git add -i
           staged     unstaged path
  1:    unchanged        +1/-0 anotherfile.html
  2:    unchanged        +1/-1 myfile.html  
  
*** Commands ***
  1: status	  2: update	  3: revert	  4: add untracked
  5: patch	  6: diff	  7: quit	  8: help
What now> 2
           staged     unstaged path
  1:    unchanged        +1/-0 anotherfile.html
  2:    unchanged        +1/-1 myfile.html
Update>> 1-2
           staged     unstaged path
* 1:    unchanged        +1/-0 anotherfile.html
* 2:    unchanged        +1/-1 myfile.html
Update>> 
updated 2 paths  
  
*** Commands ***
  1: status	  2: update	  3: revert	  4: add untracked
  5: patch	  6: diff	  7: quit	  8: help
What now> 7
Bye.
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
#
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#	modified:   anotherfile.html
#	modified:   myfile.html
#  
  
$ git commit -m"Another commit message"
[master 85ee80f] Another commit message
 2 files changed, 2 insertions(+), 1 deletion(-)

Stash

So you need to work on a different branch but have changes in your current branch that you don't yet want to commit. Git's handy Stash lets you store your current changes and then restore (apply) them when you return to the branch later.

$ git stash
Saved working directory and index state WIP on master: 85ee80f Another commit message
HEAD is now at 85ee80f Another commit message
$ git stash list
stash@{0}: WIP on master: 85ee80f Another commit message
$ git checkout someotherbranch
...
...
...
$ git checkout master
$ git stash apply

You can also clear your previous stashed states too.

$ git stash clear

Cherry Pick

On occasion you might need to merge a specific commit from one branch to another, rather than merge the whole branch over. You first need to find the SHA for the specific commit in the source branch. Note that you only need the first 6 or 7 characters, enough to be a unique reference. Then we can cherry-pick that commit in to the destination branch.

$ git checkout dev
$ git log --pretty=oneline
87ef1e6557ebec91ea8dd8f2eb4fc302c83a5381 cherry pick test
85ee80f59832d2f93db64f7b6deef355e41d8e65 Another commit message
b9a3ba26698f404b65f2182a47267cc097c34a19 adding removed file
$ git checkout master
Switched to branch 'master'
$ git cherry-pick 87ef1e6
[master 797180a] cherry pick test
 1 file changed, 1 insertion(+), 1 deletion(-)

Tag

The usual approach to marking release points in your code is to create a tag (and then test and deploy that tag). Make sure you have the latest tags from the remote repo first before creating a new one. Also be sure to push the new tag back to the remote repo.

$ git pull origin master --tags
$ git tag
v1.0.1
$ git tag -a v1.0.2 -m"Creating a new release"
$ git tag
v1.0.1
v1.0.2
$ git push origin master --tags
Counting objects: 1, done.
Writing objects: 100% (1/1), 170 bytes, done.
Total 1 (delta 0), reused 0 (delta 0)
 * [new tag]         v1.0.2 -> v1.0.2

Reset

On occasion you might find that you want to bin all the changes you have made and return the current branch to it's last commit. What? You've never started work on the wrong branch before? For the less than perfect of us (that includes me!), Git's Reset provides the solution. Whilst Reset comes furnished with several options for different scenarios, there is one in particular which fits our situation perfectly. Please note that this is of course destructive...you will lose all your changes since the last commit.

$ git reset --hard
HEAD is now at 85ee80f Another commit message

Inspection

If you get lost with your commits or just want to dig a bit deeper into the commit history, Git provides a great tool. Gitk allows you to not only view the actual files changes within each commit but to also view a commit graph.

To view all files

$ gitk

To view a specific file

$ gitk /path/to/file/filename

Or if you prefer a quick in-command-line method...

$ git log
commit 797180a46e90876f0610d3256ecff9b466ab677c
Author: MyName <myname@mydomain.com>
Date:   Sun Nov 10 10:45:23 2013 +0000  
  
    cherry pick test  
  
commit 85ee80f59832d2f93db64f7b6deef355e41d8e65
Author: MyName <myname@mydomain.com>
Date:   Sat Nov 9 22:40:10 2013 +0000  
  
    Another commit message

...and we can simplify the output too.

$ git log --pretty=oneline
797180a46e90876f0610d3256ecff9b466ab677c cherry pick test
85ee80f59832d2f93db64f7b6deef355e41d8e65 Another commit message