Git tips and trick that are hard to find in the tutorials and man pages


This page attempts to capture some of the more useful features of git that are just about impossible to figure out without someone's help.

Although I'm a kernel developer I've avoided git and bitkeeper before it like the plague.  This year I was forced to take the plunge and started by reading the man pages the tutorials all over the network.  I found them un-helpful for most practical applications and uses of git.

I found myself setting up multiple git clone trees one for each kernel project I was interested, the Linus tree, the next tree, the rt tree and was diffing files between each.  This is not effective or helpful, and as typical in software if something seems like it should be easier, it usually is and you simply don't know the trick yet.

Enter git with multiple remotes.  Learning about these made me feel sorry (only a little) for all the git bashing I've done.

I'm currently working on a project where we have an internal tree, and have to track an upstream external tree keeping up with the significant code-flux on the external tree.  Git makes it easy to re-base or merge our efforts.

After you have set up your baseline upstream repo you can use git-remote add <name you pick> <remote repo git URL>:
i.e.

git-remote add realtime git://git.kernel.org/pub/scm/linux/kernel/git/tglx/linux-2.6-hrt.git
git-remote add internal-project mgross@linux-repo.jf.intel.com:/internal-project/kernel

Once you have the remotes added you now need to create local branches associated with each so you can easily work between each one and your own local work.  Use git-branch -b <name you pick> <name chosen in git-add call/remote-branch-you-care-about>.   Note git-checkout works  the same way  only it will create the branch and check it out in one command.
i.e.

git-branch rt realtime/master
or
git-checkout -b rt realtime/master

git-branch internal internal-project/xxx
or
git-checkout -b internal internal-project/xxx

At this point you have set up your remotes and now you can work away on the internal branch while the remotes continue on there happy way.  At some point its time to re-sync with upstream an you need to "fetch", don't use pull at this point as it does too many things and could mess you up if your current branch is not what you thought at the time you issue the "pull",  git-fetch <branch name you picked>.
i.e.
git-fetch rt
git-fetch internal
This command will not change your working directory or your private working branches but will update the backing stores for the remote tracking branches.  Now you can merge or rebase your work between these branches or cherry pick (look it up, see "git-cherry-pick") patches from rt and internal to merge into the internal branch which you will later "push" so other folks can work with it.

Ok, I know this is is a lame tutorial, but its not supposed to be a tutorial its just a set of tips.  You should look closely at the man pages for the following commands:

git-branch, git-checkout, git-remote, git-merge, git-rebase, git-push

My typical git commands are:
git-fetch internal
git-remote
git-checkout
git-rebase
git-merge
git-branch
git-branch -a
git-commit
git-status
git-diff
git-reset --hard
git-clean -d
It would do you good to study the documentation around this set of commands.