Git

Repository

  • Normal

git init
  • Bare repository (without working tree / used to clone)

git init --bare

Commit

  • Append to last commit

git commit --amend
  • Revert a commit

git revert <version>

Edit a commit

git rebase -i <version>
  • This opens an editor -> change the pick into edit

git add -A
git commit -C HEAD
git rebase --continue
git push

Logging

  • Show changes

git log -p
  • Show changes for one file or dir

git log -- *.py
  • Show only the last 3 entries

git log -3
  • Show only commits from the last 2 weeks

git log --since=2.weeks
  • Unpushed commits

git log origin..
  • Show log for one file

git log -- [filename]
  • Show files of a Commit

git show --name-only <revision>

Branching

  • Create branch

git checkout -b <branch>
git push origin <branch>
  • Checkout a branch

git pull
git checkout <branch>
  • Delete branch

git push origin :branch
  • Show diff between two branches

git diff master..<branch> --raw
  • List all branches on remote

git branch -a

Merging

  • Merge everything

git checkout <target-branch>
git merge <source-branch>
  • Merge just one commit

git cherry-pick <commit-id>
  • Checkout a file from another branch

git checkout <branch> <file>
  • Show all merge conflicts

git status --short | grep "^UU "

Tagging

  • Create a tag

git tag <tag_name>
  • Create a tag with a comment

git tag -m <comment> <tag_name>
  • Show all tags

git tag
  • Show one tag

git show <tag_name>
  • Delete a tag

git tag -d <tag_name>

Working with older versions

  • Get latest version of one file

git checkout <file>
  • Show specific version of one file

git show <version>:<file>
  • Get specific version of one file

git checkout <version> <file>
  • Delete all changes over a specific version

git reset --hard <version>
  • Delete just the changes of a specific commit

git revert <commit-id>

Using the stash

  • Save changes to the stash

git stash
  • Show stashes

git stash list
  • Show changes of a stash

git stash show stash@{0}
  • Apply latest stash changes and delete the stash

git stash pop
  • Apply a specific stash without deleting it

git stash apply stash@{0}
  • Delete a stash

git stash drop stash@{0}
  • Wipe all stashes

git stash clear

Handling remote repositories

  • Add a remote

git remote add origin git://domain.tld/repo.git
  • Show infos about remotes

git remote show
git remote show origin

Sign commits

  • Edit ~/.gitconfig

[user]
      name = Bastian Ballmann
      email = balle@codekid.net
      signingkey = 0xcodedeadbeef
[commit]
      gpgsign = true

Have a different git config per directory

[includeIf "gitdir:~/src/some/dir/"]
    path = ~/.gitconfig_some_dir

Ignore existing file (if gitignore doesnt ignore)

git update-index --assume-unchanged <file>

Check consistency

git fsck --progress

Dangling commits

  • A commit that is not linked to a branch or tag

git reflog expire --expire=now --all
git gc --prune=now --aggressive

Convert normal repo to bare

git clone --bare -l <normal_repo> <bare_repo>

Git over HTTP

git clone --bare /git/test
touch git-daemon-export-ok                                                                                                             │
git config --file config http.receivepack true                                                                                         │
git config core.sharedRepository                                                                                                       │
chown apache:apache -R /git/test

Apache config for gitweb

<VirtualHost *:80>
  ServerName git.server.net
  ServerAlias git

  DocumentRoot "/var/www/git"
  Timeout 2400

  LogFormat   combinedssl
  LogLevel    info
  ErrorLog    /var/log/httpd/git-error.log
  TransferLog /var/log/httpd/git-access.log

  RewriteEngine On
  RewriteLog "/var/log/httpd/git-rewirte.log"
  RewriteLogLevel 5
  RewriteCond %{QUERY_STRING} ^.*p=(.*?)(\.git|;|&|=|\s).*
  RewriteRule (.*)/$ http://git.server.net$1?

  SetEnv GIT_PROJECT_ROOT /git
  SetEnv GITWEB_CONFIG /etc/gitweb.conf
  Alias /git/static/ /var/www/git/static/
  AliasMatch ^/git/(.*/objects/[0-9a-f]{2}/[0-9a-f]{38})$          /var/www/git/$1
  AliasMatch ^/git/(.*/objects/pack/pack-[0-9a-f]{40}.(pack|idx))$ /var/www/git/$1
  ScriptAliasMatch \
                  "(?x)^/git/(.*/(HEAD | \
                  info/refs | \
                  objects/info/[^/]+ | \
                  git-(upload|receive)-pack))$" \
                  /usr/bin/git-http-backend/$1
  ScriptAlias /git/ /var/www/git/gitweb.cgi/
</VirtualHost>

Subversion over git

  • You can use a subversion repo like a remote git repo

  • Clone it

git svn clone <svn-url>
  • Pull and push changes

git pull origin master
git svn push origin master

Misc