For a quick memo about different Source Code Management software, see software:scm.
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).
[user] name = Firstname Name email = <email-address> [core] editor = /usr/bin/myeditor
[color]
diff = auto
status = auto
branch = auto
ui = auto
[color "branch"]
current = yellow reverse
local = yellow
remote = green
[color "diff"]
meta = yellow
frag = magenta
old = red bold
new = green bold
[color "status"]
added = green bold
changed = cyan bold
untracked = red bold
[alias]
d = diff -w --color-wordsalias gitk='gitk --color-words'
git init : create a local git repository for your project in your foldergit add [-p] <files|dirs> (or just git add . for all files) : add new or modified files to the index (-p to select chunks to include, y/n/s = yes/no/split)git commit [-m <msg>] : commit the changes of the index to the repositorygit commit -a : commit the changes of the working files to the repositorygit tag -a <tag-name> [-m <msg>] : add a taggit reset --hard [<commit-hash>] : restore the working files to the latest commit, or given commitgit revert <commit-hash> : revert the given commit (<commit> can be just the beginning of its hashcode)git checkout <file> : restore a working file to the last commited versiongit checkout <commit> : switch working files to the commit version, use git checkout master to go back to headgit checkout -b <branch-name> : create a new branch (copy of the current branch)git checkout <branch-name> : switch to the given branchgit branch [-a] : list available branches (-a to list all branches, including distant branches)git branch -D <branch-name> : delete the given branchgit merge <branch-name> : merge current branch with the given branchgit cherry-pick <commit-hash> : apply the changes introduced by a given commit to the current branchgit rebase <branch-name> : rebase the branch on top of the given branch, in order to merge the branch commits that were done after the branch was started (if you have to resolve conflicts, do ”git add .” and ”git rebase --continue” once you resolved conflicts for a commit)git --bare init : create a git repository in the folder without working files, used for remote repositories, see howto for more detailsgit clone <repo-addr> : copy the distant repository (<repo-addr> can be “http:<repo>” or “ssh:<user>@<repo>”)git pull [<repo-addr>] : update from the distant repository (fetch and merge)git pull --rebase [<repo-addr>] : instead of merging your pending commits with the new ones on master, rebase your pending commits on the top of master ; it creates a cleaner tree and avoids artificial “merged” commits.git stash ; git pull|merge|rebase ; git stash apply: to pull or merge or rebase when you have non-commited modifications ; store the modifs, pull or rebase, apply the modifs backgit push <repo-addr> : send to the distant repository (needs ssh clone and write permissions)git push origin <local-branch>[:<dist-branch>] : push a local branch into a new distant branchgit branch <local-branch> origin/<dist-branch> or git checkout -b <local-branch> -t origin/<dist-branch> : create a local branch to track a distant branch (so that you can pull/push with the distant branch) git status : show the status of the repository and the indexgit log [--stat|-p] [<branch-name>] : show the history of modificationsgit diff [-w] : show differences between working files and index (-w ignores whitespace changes) git diff --cached : show differences between index and repositorygit diff <commit-hash> <commit-hash> : show differences between two arbitrary commitsgit info <commit-hash> : show the diff and message of the commitgit blame <file> : show what revision and author last modified each line of the filegit diff [options] > <file-name> : save the patch to a filegit apply <patch> [--index] : apply a patch to the working tree, and optionally add the changes to the indexgit format-patch -M origin/master : create git patches for each of your commits not yet pushedgit format-patch -M <first-commit-hash> [<last-commit-hash>] : create git patches between two arbitrary commitsgit am <patch> : apply a commit created with “git format-patch”Stats:
.mailmap file: to unify same authors with different name spelling or email address.git shortlog -s -n --no-merges: number of commits by author.-w --pretty=format:“commit %H%nAuthor: %aN%nDate: %ad” options to the git log command, in order to ignore blank changes and use .mailmap file.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”.
git bisect helps you find the commit that broke your code.
git bisect start git bisect bad <first-bad-commit-you-know> git bisect good <last-good-commit-you-know>
<test-your-code> git bisect bad|good [git bisect visualize]
git bisect log gitk
git bisect reset
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.
git remote add origin ssh://<username>@<server>/home/<username>/<repo-location>/<repo-name> git fetch origin git config branch.master.remote origin git config branch.master.merge refs/heads/master git push origin master
git clone ssh://<username>@<server>/home/<username>/<repo-location>/<repo-name> ... git push origin master
You're done, you can now do whatever you want with the distant and the local repositories (clone, push, pull…)
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 "reword" if you want to change message # set "edit" if you want to amend the commit (git commit --amend; git rebase --continue) # set "squash" if you want to merge with the previous commit # you can even reorder the commits!
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
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]"
http://help.github.com/removing-sensitive-data/
Then people must pull with git pull --rebase the first time.