Difference between branches


git diff --name-only dev..master

Updated submodule to latest version


# get the submodule initially
git submodule add ssh://bla submodule_dir
git submodule init

# time passes, submodule upstream is updated
# and you now want to update

# change to the submodule directory
cd submodule_dir

# checkout desired branch
git checkout master

# update
git pull

# get back to your project root
cd ..

# now the submodules are in the state you want, so
git commit -am "Pulled down update to submodule_dir"

Changes since last tag


git log <yourlasttag>..HEAD --oneline

Adding file partially


git add --patch filename.x

  • y stage this hunk for the next commit
  • n do not stage this hunk for the next commit

Remove some commits from Github

To remove last two commits:

git rebase -i HEAD~3 

and delete corresponding two lines. Then, force push to GitHub

git push origin +master

Using bisect

Go to top level of the repository.

Git - git-bisect Documentation

$ git bisect start
$ git bisect bad                 # Current version is bad
$ git bisect good v2.6.13-rc2    # v2.6.13-rc2 was the last version
                                 # tested that was good
$ git bisect reset               # At the end

Ignore message about submodules with modified and untracked content

git submodules with modified and untracked content - why and how to remove it? - Stack Overflow

Add to .submodules:

[submodule "vim/bundle/nerdtree"]                                               
  path = vim/bundle/nerdtree                                                    
  url = https://github.com/scrooloose/nerdtree.git                              
  ignore = dirty

Keep some files during merge

git checkout --ours index.html
git checkout --theirs _layouts/default.html

Stop merge

git merge --abort

Setting up email


git config --global user.email "<em>me@here.com</em>"
# Set a new email
git config --global user.email
# Verify the setting
# me@here.com

Fetch (checkout) remote branch


git checkout --track origin/daves_branch

Undo git merge with fast forward


$ git reflog show master
$ git reset --keep master@{1}

Remove tag and delete from remote


git tag -d 12345
git push origin :refs/tags/12345

Merge commits from Github fork


Pulling in a single commit would be a cherry-pick and would rewrite the commit ID (and mark you as the committer while retaining the author). The process is pretty straightforward, though:

git fetch git://github.com/user/project.git
git cherry-pick <SHA-COMMIT-ID>

Open current file in vim on Github

Assuming the remote is not master, but github:

Gbrowse @github

Use Chromium as a default browser in git


$ git config --global web.browser chromium-browser

Hash of a commit before some hash

git rev-parse hash^


Not to fast-forward merge with master

[branch "master"]
  remote = origin 
  merge = refs/heads/master
        mergeoptions = --no-commit --no-ff

What files changed between two revisions


git diff --name-only SHA1 SHA2

where you only need to include enough of the SHA to identify the commits. You can also do, for example

git diff --name-only HEAD~10 HEAD~5

to see the differences between the tenth latest commit and the fifth latest (or so).

Remove submodule


To remove a submodule you need to:

  1. Delete the relevant section from the .gitmodules file.
  2. Delete the relevant section from .git/config.
  3. Run git rm --cached path_to_submodule (no trailing slash).
  4. Commit and delete the now untracked submodule files.

Highlighting git branch

In bash.rc (via https://gist.github.com/938573)

### PROMPT ####################

function parse_git_dirty {
  [[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*"

function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$(parse_git_dirty)]/"


if [[ /Users/$USER != $HOME ]] ; then

export PS1=$PROMPT_USERNAME'$(__git_ps1 "\[\e[0;32m\]%s\[\e[0m\]\[\e[0;33m\]$(parse_git_dirty)\[\e[0m\] ")\w$ '

Pull and merge


In the simplest terms, "git pull" does a "git fetch" followed by a "git merge".

You can do a "git fetch" at any time to update your local copy of a remote branch. This operation never changes any of your own branches and is safe to do without changing your working copy. I have even heard of people running "git fetch" periodically in a cron job in the background (although I wouldn't recommend doing this).

A "git pull" is what you would do to bring your repository up to date with a remote repository.

Revert the last commit


This is the easiest case. Let’s say we have a remote mathnet with branch master that currently points to commit dd61ab32. We want to remove the top commit. Translated to git terminology, we want to force the master branch of the mathnet remote repository to the parent of dd61ab32:

$ git push mathnet +dd61ab32^:master

Where git interprets x^ as the parent of x and + as a forced non-fastforward push. If you have the master branch checked out locally, you can also do it in two simpler steps: First reset the branch to the parent of the current commit, then force-push it to the remote.

$ git reset HEAD^ --hard
$ git push mathnet -f

Set up gh-pages branch in a separate directory


Clone your "grandmaster" repository into the "gh-pages" folder (this will clone in the "master" branch), checkout the "gh-pages" branch, list the files (should have "index.html" and ".git") and then remove the "master" branch to avoid any confusion. Last step is to check that "master" branch was removed and only "gh-pages" branch is listed.

ichris:gh-pages $ git clone git@github.com:chrisjacob/grandmaster.git .
ichris:gh-pages $ git checkout origin/gh-pages -b gh-pages
ichris:gh-pages $ ls -la
ichris:gh-pages $ git branch -d master
ichris:gh-pages $ git branch

Move last commit from a wrong branch


when I switch branches and forget to switch back before making my next commit

git cherry-pick [commit ID]

Undo last commit in git


Add/remove files to get things the way you want:

git rm classdir
git add sourcedir

Then amend the commit:

git commit --amend

The previous, erroneous commit will be edited to reflect the new index state - in other words, it'll be like you never made the mistake in the first place :)

Adding bitbucket git repo as a remote

  1. add public key to bitbucket: https://bitbucket.org/account/#ssh-keys
  2. use git not https link to add another remote, code below adds bitbucket with name safe and pushes there
git remote add safe git@bitbucket.org:user/repo
git push safe master

via http://news.ycombinator.com/item?id=3067795 http://confluence.atlassian.com/display/BITBUCKET/Importing+Code+into+your+Bitbucket+Repository

Make sure to use git@bitbucket.org:{username}/{reponame} instead of any https links bitbucket might give you. I was able to upload an existing repo I had by: 1) Uploading my SSH Public Key at https://bitbucket.org/account/ (i.e. the contents of ~/.ssh/id_rsa.pub) 2) Create a blank repo at Bitbucket 3) Adding a remote (git remote add bitbucket git@bitbucket.org:{username}/{reponame}.git) 4) Pushing to bitbucket (git push bitbucket master) The most important parts were adding my SSH Public Key, and making sure to use git@bitbucket.org:{username}/{reponame}.git instead of any https links you might see on bitbucket.

Adding remote git repository


cd existing_git_repo
git remote add origin git@github.com:caius/foo.git
git push origin master

Stop tracking files in git


git rm --cached filename

Revert (checkout) a single file


This one is hard to find out there so here it is. If you have an uncommitted change (its only in your working copy) that you wish to revert (in SVN terms) to the copy in your latest commit, do the following:

git checkout filename

This will checkout the file from HEAD, overwriting your change. This command is also used to checkout branches, and you could happen to have a file with the same name as a branch. All is not lost, you will simply need to type:

git checkout -- filename



Basic Branching and Mergin - a chapter from Pro Git.

Sending tags to remote

Pushing tags to a remote repository You can create tags locally, but you probably want to send them to the remot repository as well:

# create a local tag "tagname" with the given message.
git tag -a "tagname" -m "message"
# send your tags to the remote repository "origin"
git push origin --tags