This is an old revision of the document!


GIT

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

  • ~/.gitconfig
    [user]
      name = Name Firstname
      email = <firstname.name@laas.fr>
    [core]
      editor = /usr/bin/myeditor

Local repository management

  • % git init : create a local git repository for your project in your folder
  • % git add <files|dirs> (or just git add . for all files) : add new or modified files to the index
  • % git commit [-m <msg>] : commit the changes of the index to the repository
  • % git commit -a : commit the changes of the working files to the repository
  • % git tag -a <tag-name> [-m <msg>] : add a tag
  • % git reset –hard [<commit-hash>] : restore the working files to the latest commit, or given commit
  • % git 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 version
  • % git checkout <commit> : switch working files to the commit version, use git checkout master to go back to head

Branches

  • % git checkout -b <branch-name> : create a new branch (copy of the current branch)
  • % git checkout <branch-name> : switch to the given branch
  • % git branch [-a] : list available branches (-a to list all branches, including distant branches)
  • % git branch -D <branch-name> : delete the given branch
  • % git merge <branch-name> : merge current branch with the given branch
  • % git cherry-pick <commit-hash> : apply the changes introduced by a given commit to the current branch
  • % git rebase master : rebase the branch on top of master, in order to merge master 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)

Collaborative repository management

  • % git –bare init : create a git repository in the folder without working files, used for remote repositories, see howto for more details
  • % git 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 push <repo-addr> : send to the distant repository (merge) (needs ssh clone and write permissions)
  • % git checkout -t -b <local-branch> origin/<dist-branch> : create a local branch to track a distant branch (so that you can pull/push with the distant branch)

Info

  • % git status : show the status of the repository and the index
  • % git log [–stat|-p] [<branch-name>] : show the history of modifications
  • % git diff [-w] : show differences between working files and index (-w ignores whitespace changes)
  • % git diff –cached : show differences between index and repository
  • % git diff <commit-hash> <commit-hash> : show differences between two arbitrary commits
  • % git diff [options] > <file-name> : save the patch to a file
  • % git blame <file> : show what revision and author last modified each line of the file

Howtos

Creating a remote repository

Create the remote repository (on the server):

cd <repo-location>
mkdir <repo-name> && cd <repo-name>
git --bare init [--shared=false|group|all|0xxx]
echo "My project\nExtensive description" > description

Create the local repository, if not already existing (on your machine):

cd <folder-to-versionize>
git init
git add *
git commit

Associate repositories together and push (in local repository):

git remote add origin ssh://<username>@<server>.laas.fr/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

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

Editing and collapsing commits

Edit the latest commit message:

git commit --amend

Edit any past commit message:

git rebase -i <latest-commit-to-keep>
#change "pick" to "edit" for all commits you want to change
#repeat for each commit to change :
git commit --amend # to change commit message
git rebase --continue # to go to next commit to change

Collapse the latest commits:

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

Collapse any past commits :

git rebase -i <latest-commit-to-keep>
# change "pick" to "squash" for commits you want to collapse with the previous one

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

Now, you can optionally (recommended) create and configure a git hook that will send an e-mail each time someone pushes something in the repository:

[kador]% cd hooks
[kador]% cat <<EOF >post-receive
#!/bin/bash
. /git/robots/laas-git-hooks.git/post-receive
EOF
[kador]% chmod a+x post-receive
[kador]% git config --add hooks.mailinglist robots-source@laas.fr
[kador]% git config --add hooks.emailprefix ''
software/git.1270023601.txt.gz · Last modified: 2013/09/19 16:43 (external edit)
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0