Salomonsson.se
Nov 05 2010

Git Cheat Sheet

I’m pretty experienced with svn, but I’m a total newbie to git – and having some problem remembering the all the command-line syntax, so I’ll use this page as a cheat sheet. Feel free to bookmark =)

Checkout

You might need to add your public ssh-key to the repository (~/.ssh/id_rsa.pub)

$ git clone {url} [folder]

Check the url to the current repository:

$ git remote -v

Adding, committing, rolling back

Add all removed files to your staging area

$ git add -u

Add all changed files to your staging area, both added, changed and deleted

$ git add -A

Revert to your previous commit

$ git reset HEAD^

Revert all files to the state of the last commit. Unstaged files will be lost!

$ git reset --hard

Compare your commited files to the files on a remote:

$ git log origin/master..HEAD --name-only --pretty=oneline

Brancing

will download all branches into special read-only branches called <remoteName/branchName>, you’ll need to merge manually

$ git fetch [remoteName] 

Get list of remote branches

$ git branch -r (or -a to list both remotes and local branches)

Create a new branch that tracks remote branch “origin/dev”

$ git branch -t [localBranchName] [remoteBranchName] (e.g. git branch -t dev origin/dev)

Make an existing branch track a remote branch

$ git checkout {yourBranchName}
$ git branch --set-upstream "origin/dev"

Alternative: push a local branch to remote and track it:

$ git checkout -b mynewfeature

… edit files, add and commit …

$ git push -u origin mynewfeature

Get a list of all branches, and see which branch is tracked to which remote branch

$ git branch -vv

If you create a new local branch, and wish to add it as a remote branch on your server (or github). The -u flag makes your local branch track the new remote branch.

$ git push -u {remote-name} {local-branch-name}

Where the remote-name probably is “origin”. You can set the remote branch name to something other than your local name with:

$ git push {remote-name} {local-branch-name}:{remote-branch-name}

Logging

If you need to see which files were modified for each commit:

$ git log --name-only (exit list with “Q”)

If you also want to know what the modification for the file was:

$ git log --name-status

To only see the last three (change the number to any number you want):

$ git log -3

And at long last, enable colors by typing:

$ git config --global --add color.ui true