Thursday, July 23, 2009

Git commands I love to forget

Here are the most useful GIT commands that I *always* forget.

To push a local branch to your remote repository:

git push remote_repository_name branch_name

Eg: git push origin my_new_branch

Also:

Remove branch from repo.

git push {repo} :heads/{branch}

Ex: git push origin :somebranch
removes somebranch

This works for removing a tag as well git push origin :sometag

You might also want to check out git-publish-branch‘s ’-d’ option.

Thank you, GitHub, for these handy pages. But for me, these are the handiest of the handy.

Fixing an accidental merge (discarding local commits)

Let's say that you merged branch my_branch into master, when, in fact, you wanted to merge master into my_branch. Check git log:

commit 2a4b206e42360b812d6110c5ae9eeced96d99f0e
Merge: 4147391... dd525b5...
Author: baitisj
Date: Thu Jul 23 13:13:11 2009 -0700
Merge branch 'my_branch' of git@blablobbbla:somewhere/repository


commit 4147391a181a05f9754ddf8eecc6c4398dbb1d17
Author: baitisj
Date: Thu Jul 23 13:12:28 2009 -0700

latest

...
When you type git status, even though you did a single merge, git will say:

# On branch master
# Your branch is ahead of 'origin/master' by 20 commits.

Not to worry. Simply do:

git reset --hard 4147391a181a05f9754ddf8eecc6c4398dbb1d17

Poof! Your local repository has reverted to that version, and your merge has disappeared.