Table of Contents

GIT

For a quick memo about different Source Code Management software, see software:scm.

Introduction

Git is a source code version control system. Unlike Subversion (SVN) and CVS it is distributed, so it allows better local source code management (you can commit, branch and so on on your local repository before pushing it on the public repository; you can also never push it on any public repository).

Cheat sheet

Configuration

General tips

Local repository management

Branches

Collaborative repository management

Info

Stats:

Undoing

Howtos

Development method

When you are working on a public project, here is a comfortable method to experiment and publish only what you want:

If you need to fix a past commit, just create a new commit with only the fix, then use “git rebase -i” as explained below, move the fix commit just below the commit to be fixed, and change its status to “fixup”.

Finding a broken commit

git bisect helps you find the commit that broke your code.

Creating a remote repository

Create the remote repository (on the server):

cd <repo-location>
mkdir <repo-name> && cd <repo-name>
chgrp <group> . && chmod a+s . # optional, if you need to change the group
git --bare init [--shared=false|group|all|0xxx]
echo "Project-Title\nExtensive description" > description

If you want to send emails after each commit, check this section.

You're done, you can now do whatever you want with the distant and the local repositories (clone, push, pull…)

Editing commits

Edit the latest commit (change message and include modifications of given files):

git commit --amend [<file1> <file2> ...]

Collapse the latest commits:

git reset --mixed <base-commit>
git commit -a

Edit/Reorganize any past (not yet pushed!) commits:

git rebase -i <latest-commit-to-keep-unchanged>
# set "r" (reword) if you want to change message
# set "e" (edit) if you want to amend the commit (git commit --amend; git rebase --continue)
# set "s" (squash) if you want to merge with the previous commit and merging or editing the commit message
# set "f" (fixup) if you want to merge with the previous commit without interaction
# you can even reorder the commits!

Tips with rebase -i:

Change author or date: use rebase -i, chose “edit”, and commit with:

git commit --amend --author "Name <email-address>" --date "$(date -R)"

To split a commit: use rebase -i, chose “edit”, and reset the index to the commit before, and commit:

git reset --mixed @~
git add -p .
git commit

Archive generation

Create a tag and generate the corresponding archive for a release, for instance:

git tag -m "release 0.3.2" 0.3.2
git archive --format=tar --prefix=liboro-0.3.2/ 0.3.2 | gzip > liboro-0.3.2.tar.gz

Setting up email hooks

If you want an email to be sent every time something is pushed on the server, then do on the server:

cd <repo-location>
wget http://crteknologies.fr/wiki/lib/exe/fetch.php/software:git:post-receive.txt -O hooks/post-receive
chmod a+x hooks/post-receive
git config --add hooks.mailinglist "<email1> [<email2>...]"
git config --add hooks.emailprefix "[git]"

Removing Sensitive Data

http://help.github.com/removing-sensitive-data/

Then people must pull with git pull –rebase the first time.

Switching master branch

http://www.dmo.ca/blog/20080307124544/

Recovery

For finding a lost commit you can use “git reflog”.

For finding a lost stash (eg “git stash drop”) or file (eg “git rm”), you can try “git fsck –full”, and then use “git show” with the hash to see the content of the objects.

https://github.com/blog/2019-how-to-undo-almost-anything-with-git