Setting up email

git config --global "<em></em>"
# Set a new email
git config --global
# Verify the setting

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://
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

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

function parse_git_dirty {
  $(git status 2> /dev/null && 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 "%s$(parse_git_dirty) ")\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 .
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:
  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 push safe master


Make sure to use{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 (i.e. the contents of ~/.ssh/ 2) Create a blank repo at Bitbucket 3) Adding a remote (git remote add bitbucket{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{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 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
Last edited by Artem Dudarev, 2013-10-13 02:01:37. Edit