Git for the newbie
This brief page describes the mistake I’ve done with git. First of all : read the manual and the howto, as usual this is the first step to go !
Some interesting documentation includes :
Customizing your repository
git-repo-config user.name "Your Name"git-repo-config user.email "your@mail.com"
To check modification:
git-repo-config --list
Working on your branch
- create the branch :
git checkout -f -b my_branch master - work
- add modified files to commit list :
git-update-index $FILE - commit :
git commit - diff :
git diff master..my_branch
Sending your work
First of all, read the docs about code formatting. To send a patch, use git-format-patch :
git-format-patch -o /tmp/ --signoff master..my_branch
It creates a patch for each commit in the /tmp/ directory. For explanation about signoff, read this page.
Apply work from other
To apply a patch send from git tool simply save the mail and do :
git-applymbox /tmp/mbox ~/.signoff
Here, ~/.signoff is a file containing my signed-off-by line.
Update branch
Switch to master to update it first :
git checkout -f master git pull
To only update master :
git pull git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git master
Switch back to your branch :
git checkout -f your_branch git pull
Conflicts
In case there’s a conflict you have to edit the bad file by hand, solve the issue and do git update-index on file before doing a git commit. If a pull or a merge fail you can do git reset --hard to clean your tree.
Work with a private repository
Fetching and pushing your work
To fetch your tree from a distant computer:
git clone ssh://users@server/home/users/linux-2.6/ mydirectory
To push your local commit to the distant tree:
git push ssh://users@server/home/users/linux-2.6/ mybranch
If remote branch is set and if you want to publish only one branch (let’s say master for example), you can run something like:
git push origin master
Merging some branch inside yours
For example if you are working on Linus tree and want to work on 2.6.X.Y (like 2.6.19.4), you need to pull a branch inside your tre:
git pull git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-2.6.19.y.git
Tagging
The following commands will create and push a tag to your repository:
git tag -s mytag git push --tags ssh://users@server/home/users/linux-2.6/
Creating an archive
To create a tar archive for publication:
git archive --format=tar --prefix=nufw-2.4.0-rc1/ HEAD>/tmp/nufw-2.4.0-rc1.tar
Sending patches by mail
Suppose you’ve got a lot of commit against a project (here ulogd2, official branch is git-svn) and that you wan to send your patchset to the Mailling list devel@netfilter.org.
To do so, just open a shell at the root of the git directory and use:
git-format-patch --stat -p --raw --signoff --subject-prefix="ULOGD PATCH" -o /tmp/ulogd2/ -n git-svn git-send-email --compose --no-chain-reply-to --to devel@netfilter.org /tmp/ulogd2/
First command will create a serie of mail from patches in /tmp/ulogd2/ with statistic report and second will start your editor to compose an introduction mail to the patchset.
To avoid awful threaded mail series, one can use :
git-config sendemail.chainreplyto false
Applying patches from mail
Just save mail to an mbox and run:
git apply-mbox MBOX
Please note that it will apply the modification it the patch apply without any warning (or hunk). To apply the patch even if there is some hunks, just run:
git apply-mbox -m MBOX
To transfer patches from a repository to another, one could use git-format-patch on system with modification followed by git-am on target repository.
Merging patches
When git-am fails to cleanly apply It will ask you to apply it yourself, to git add the file and to run git-am --resolved.
In fact, git-am can help you a lot. When the call to git-am for a patch fails, simply run :
git-am --3way
Il will merge the file that can be automatically merge. Some files may be marked with CONFLICT and in this case, edit them to do a manual merge, and once they are fixed and run
git add $FILES_IN_CONFLICT git-am --resolved
Fixing work
Reverting some work
Let’s you’ve coded too late and that all your commits need to go to the trash. You can then use:
git reset --hard COMMITID
This will revert to the state the source after COMMITID.
Modifying commit
This time your work is correct but you are not happy with your commit. To fix this, you can run:
git reset COMMITID
and commit your work in the way you should have done before.
Modifying commits
The more powerfull way to do this kind of stuff is to use git rebase --interactive. With that command, you can modify, reorder suppress commits from your branch. MadCoder has a really cool post about using this feature.
Misc tools and tips
Enhanced shell prompt
One specificity of git over something lke subversion is that all your branch stay in the same directory.
Thanks to the git completion extensions available in git-core, you can enhance your shell prompt to display the name of the branch you are working on. For example, if you set:
PS1='\u@\h:\w$(__git_ps1 " (%s)") \$ '
you can have a prompt displaying:
eric@ice-age:~/netfilter/git/ulogd2 (nohash)$
Zsh lover can found a similar tips here.
Colorized diff
git diff has a –color option to display a colorized diff. It is possible to have such a diff by default by using:
git config --global diff.color true
More to come …

[...] Merging patches [...]