Notes: Git

DISCLAIMER: this is not meant to be coherent post. Sorry. Just notes for future reference. There will probably be some future updates here.

I sometimes (more like all the time) need to work with git repositories. Here is a collection of references to tricks/useful pieces of information that I have learned over the years. The tidbits below might be useful to know, in general. They are also things that I don't necessarily use everyday, but needed at some point in time to solve my problems, and it is by far easier for me to find them here than to look for them somewhere on the Internet for those solutions.

Good approach to structuring the repository

One of the very first things I have learned - "A successful Git branching model" by Vincent Driessen. Highly recommended read.

How to merge a single file on multiple branches

Here's the description of the problem - given N release branches (e.g major supported versions), merge changes from one file without changing irrelevant information AND without loosing sanity.

So, for example, after working on some feature-better-schwiftifying branch, we can patch the changes to relevant branches (version-{7,8,9}.x), where we have connected by diverging code. (I am not saying that this is a good approach to development. I want to present quite useful feature of Git.)

git checkout version-8.x
git checkout --patch feature-better-schwiftifying --

After applying merging relevant hunks we can commit the changes on the branch version-8.x and move along to other branches.

Move wrongly created commits to the right branch

Have you commited something only to discover that …sigh… it was the wrong branch? Here's an useful snippet to the rescue!

git branch newbranch      # Create a new branch, containing all current commits
git reset --keep HEAD~3   # Move master back by 3 commits (Make sure you know how many commits you need to go back)
git checkout newbranch    # Go to the new branch that still has the desired commits
# Warning: after this it's not safe to do a rebase in newbranch without extra care.'

Consider commenting via e-mail