Skip to content

Git Reference Sheet

J-east edited this page Jan 16, 2015 · 3 revisions

Git Commands Reference

References:

http://www-cs-students.stanford.edu/~blynn/gitmagic/
http://book.git-scm.com/
http://sysmonblog.co.uk/misc/git_by_example/
http://www.markus-gattol.name/ws/scm.html

What has changed?

  • $ git status
  • $ git diff # changes not yet staged for commit
  • $ git diff --cached # changes staged for commit

Reviewing history

Before committing

  • $ git fetch
  • $ git merge
    --or--
  • $ git pull

Committing

  • $ git add file1 file2 file3

  • $ git add . # looks at the working tree and adds all those paths to the staged changes if they are either changed or are new and not ignored, it does not stage any 'rm' actions.

  • $ git add -u # looks at all the currently tracked files and stages the changes to those files if they are different or if they have been removed. It does not add any new files, it only stages changes to already tracked files.

  • $ git add -A # is a handy shortcut for doing both.

  • $ git commit -m "message"
    --or--
    git commit -a -m "message" # for all files

Add current changes to previous commit

  • $ git commit --amend

Create local-only branch

  • $ git branch # does not switch you to that branch!
  • $ git checkout -b # create and switch to branch

Create local/remote tracking branch

  • $ git branch --track origin/

Create remote tracking branch from existing local branch

  • $ git push -u origin

Switch branch

  • $ git checkout
  • $ Get remote branch and merge with current local branch
  • $ git pull

[Show changes](ref. http://www.lornajane.net/posts/2011/git-tip-what-did-i-just-merge)

Show just what is new on a specific branch

  • $ git log [branch] --not master

Show changes in my repo that aren't in github

  • $ git diff origin/master..HEAD

Merge local current branch commits AFTER applying latest changes from remote

  • $ git pull --rebase

Merge other branch commits AFTER applying latest changes in current branch

Save local changes for quick work on something else

  • $ git stash "optional message about where we paused"
    --and--
  • $ git stash pop # remove single stashed state and apply to working tree

Show branches

  • $ git branch # show local branches
  • $ git branch -r # show only remote branches

Push a single branch to remote

  • $ git push origin
    Push all branches of the repo, not just "master"
  • $ git push --mirror origin

Delete a branch

  • $ git branch -d # delete local branch
  • $ git push origin : # delete remote branch

Clone repo from github

Restore deleted file from previous commit
(single file at a time)

  • $ git checkout $(git rev-list -n 1 HEAD -- "$filename")^ -- "$filename" (be sure to commit addition(s) afterwards)

Find where a bug originated

  • $ git bisect start
  • $ git bisect good
  • $ git bisect bad
    [test and invoke "git bisect good/bad" depending on your result until commit is found]
  • $ git bisect reset
  • (see also: http://webchick.net/node/99)
Clone this wiki locally