Useful git tricks
in-silico toxicology gmbh blog
A collection of helpful commands for the SCM system git. Consider reading the Ubuntu terminal guide first.
General git help
There are some good places to start looking for documentation. Start off by visiting git-scm.com. The official documentation is (contrary to many other projects) the most complete and useful documentation available. Specifically, you will want to understand the section about branching and merging.
Proceed to github.com, where very informative articles about the specifics of this large social coding platform are available. It could be interesting for you to read about forking.
Here is a cheat sheet and here is the development cycle model that IST uses.
Tricks
Here is how to solve some common issues.
Check out a remote branch
Suppose there is a branch on origin
called foo
and you want to checkout
this branch for the first time. Assuming you have cloned the repository, you cannot just do git checkout foo
if foo
was created by someone else after you have cloned.
git remote show origin # see what's available on origin
git fetch # download everything available on origin
git checkout foo # now just checkout foo
Pretty easy, the trick is just in using fetch
.
Get a file from a specific revision
Suppose you want file blub
exactly as it was in commit MYHASH123
. Assume also MYHASH123
is 4 commits behind HEAD
.
git show HEAD~4:blub
or
git show MYHASH123:blub
will show you the file in less
. Use output redirection, such as git show HEAD~4:blub > blub.old
to store in file blub.old
.
Downgrade or delete a remote branch
Suppose your local HEAD on branch <branch>
is rubbish, you have coded something and committed it already. Assume you want to get rid of the last n
commits.
First, make sure you are on branch <branch>
, then remove them locally:
git reset --hard HEAD~n
Using git log
, check that you are now on the right commit. Then force a downgrade on the remote (if you have push
ed already):
git push origin +<branch>
Now, origin should also be downgraded on branch <branch>
. Here is how to remove a remote branch completely:
git push origin :<branch>
Merge specific files from branch to branch
Suppose you want merge a specific FILE
or commit from branch feature/foo
to any other branch eg. branch master
you can use --patch
.
First checkout to your destination branch git checkout master
. Then git checkout --patch feature/foo FILE
. It gives you additional prompt to control merge options where you can agree or deny single diffs within the file you want to merge. Same procedure with single commits.
blog comments powered by Disqus