Friday, 22 July 2011


A summary of things I need to do with Git and can never quite remember how:

Checkout a URL from Github:
git clone

I made changes locally now I want to push them into a separate branch and then on to github

git checkout -b many_changes
git push origin many_changes

Everything has gone wrong. REVERT!

git reset --hard HEAD

Revert to remote head:

git reset --hard origin/master

Oops! - I commited to wrong branch - undo commits but keep the changes on my local area. more info

git reset --soft HEAD^

Master has gone to hell. My current branch should be master instead

git merge --strategy=ours master
git checkout master
git merge GOOD_BRANCH

Everything has gone wrong. AND I have a local commit to revert

git reset --hard ORIG_HEAD

Revert to last commited state:

git reset --hard

Review unpushed commits and squash:

git rebase -i

Reverting local changes:

git checkout -- file

Go back in Time:

git log -3
git checkout TAG

git checkout master^ (go back 1 commit from master)
git checkout HEAD^^ (go back 2 commits from where i am now)
git checkout HEAD~3 (go back 3 commits from where i am now)


Search thru git logs on my commits only:

git log --all-match --grep=search_string --author=andy

Git logs with follow:

git log --follow

Search for component:

git log -p -S SEARCH_FOR optional_limit_by_dir

Complex multi revert pattern:
 git checkout -b temp
 git rebase -i

  # If we get a merge error then you need to remove more commits as some more modern ones depend on the ones removed
  git checkout master
  git checkout temp -- .   #checkout  branch into the directory '.'
  git commit -a

Some commit has introduced a bug somewhere - lets go find it:


Pull code then review before release

git fetch
  git log HEAD..origin
giant diff:
  git log -p HEAD...origin
git merge origin

Standard GIT workflow:(create a myfeaturebranch from the master branch)

git checkout -b myfeature master

while(I have code):
git pull --rebase origin master

final merge:

git checkout master
git merge myfeature  OR git merge --no-ff myfeature OR git merge --squash myfeature
git branch -d myfeature
git push origin master

--no-ff = No Fast forward = Keep the fact I worked on a separate branch.
(default) = Fast Forward = Fast forward all changes in to the master. 
General Rule: Use --no-ff if you worked on something big and want the branch history.
--squash = Squash this local branch's commits into 1. Makes git logs easier to read
git pull --rebase origin master = remove local changes. Pull master. Re-apply your changes

If you can rebase OFTEN do it. If there are many differences between master and your branch do a merge instead. Rebase is only for LOCAL branches not pushed ones.

Do not rebase a public 'pushed' branch that someone else is using. EG: If you fork a github project your fork is publicly available so do not rebase this fork with the original master. Instead use git fetch, git merge

Delete remote branch
git push origin --delete 

git mergetool

Funky review & merge tool:

Using my file or there file on conflict:

git checkout --ours filename.c
git checkout --theirs filename.c
git add filename.c
git commit -m "using theirs"

Oops! I commited to the wrong branch

How to rebase - This is like merging but better. It updates the origin (head) then applies each of your commits so you have no 'merges'.

More Git Help

I'm in git Merge Hell!

Great Git learning web game

Tool to simplify your git branching Git Flow


  1. reset to remote master:
    git reset --hard origin/master

  2. If you want to rebase from branch to master but you get lots of conflicts. Squash your commits into 1 and then cherry-pick from master:

    git checkout branch_name
    git rebase -i HEAD~N [N= num of commits]
    git checkout master
    git cherry-pick

  3. To track your new github fork locally:
    git remote add up
    git push -u up master

  4. If cmdline starts asking for a password:
    $ git push origin ab-rm-windows Password for '':