User Tools

Site Tools


git

git

Setup

# aptitude install git git-gui git-doc
$ git     # CLI
$ gitk    # GUI for revisions

Settings

Config file:

  • ~/.gitconfig (global ⇒ when using git config --global)
  • .git/config (repo-local ⇒ when using git config)

Set global username and email:

$ git config --global user.name "John Doe"
$ git config --global user.email "john.doe@gmail.com"

View config value (e.g. global user.name):

$ git config --global user.name
John Doe

List all config values:

$ git config [--global] -l

Misc settings

Workaround a firewall blocking Git port:

git config --global url."https://".insteadOf git://

Create a new repository

$ mkdir testrepo
$ cd testrepo
$ git init

bare repository

$ git init --bare

Clone a repository

$ git clone git://github.com/CyanogenMod/android_vendor_cyanogen.git
$ cd android_vendor_cyanogen

Usual workflow

Add and commit files:

$ touch test{1,2,3}                # create 3 testfiles
$ git add test{1,2,3}              # add the files initially to the index
$ git commit [-m 'first commit']   # commit the changes

Diff/Status/Revert of Index/File changes

# Show local changes (not yet in index via "git add")
$ git diff
 
# Show local changes which are already in the index (if commit w/o -a)
$ git diff --cached
 
# Show all changes  (if commit with -a)
$ git diff HEAD
 
# Diff-Stat
$ git diff --stat
 
# Brief summary of all change-information
$ git status
 
# Revert index (e.g. undo "git add")
$ git reset HEAD <file>
 
# Revert local changes
$ git checkout -- <file>      # restore to version in index
$ git checkout HEAD <file>    # restore to version in HEAD
 
# Add changes of (already present) files to the index and commit
$ git commit -a
 
 
# Diff between two versions (branch, tag, revision)
$ git diff master <branchname>
$ git diff release_2.0..release_1.0
$ git diff a3b79c..b3b7f9

Branches

# List branches
$ git branch
 
# Create a new (local) branch of current branch (e.g. "master" is current)
$ git branch <branchname>
 
# Switch working copy to created branch
$ git checkout <branchname>
 
# Shortcut: Create branch and switch
$ git checkout -b <branchname>
 
# Merge changesets from branch <branchname> into current branch (e.g. "master" is current)
$ git merge <branchname>
 
# Delete branch (Option -d checks for merge status, -D ignores it)
$ git branch -d <branchname>
 
# Revert whole branch to HEAD (resets index and changes)
$ git reset --hard HEAD

Log

# Output complete log
$ git log
 
# Output log since tag / commit (hashsum or part of it)
$ git log release_1.0..     # note the 2 dots
$ git log d7b3a1..
 
# Output log with diff/patches
$ git log -p
 
# Include Diff-Stat
$ git log --stat

Distributed workflows

# helper clones leader's project and does his changes
helper$ git clone /home/leader/project project
helper$ # changes and commits...
 
# helper sends leader a pull request...
 
# leader pulls the master-branch of helper into leader's current branch (=merging)
leader$ git pull /home/helper/project [master]

Remotes are aliases for remote repositories.

# Add shorthand for helper's repository
leader$ git remote add helper /home/helper/project
 
# List remotes
leader$ git remote -v
 
# Fetch changes but do not merge
leader$ git fetch helper
 
# Show changes since helper branched leader's repository
leader$ git log -p master..helper/master
 
# Merge helper's changes into leader's master-branch
leader$ git merge helper/master    # alternatively: $ git pull . remotes/helper/master
# List all remote-tracking branches
leader$ git branch -r
 
# Delete remote branch (and after that the local branch)
$ git push repo :<branchname>    # {no-local-name}:<remotename> will write "nothing" into the branch (=deleting)
$ git branch -d <branchname>

Tags

# Create a lightweight tag ("branch that never moves"; can't have a comment!)
$ git tag <tagname> [<commit>]
 
# Create an object tag
$ git tag -a <tagname> [<commit> | <object>]
 
# Delete a tag
# -> (see branches)
 
# Push all tags at once (refs)
$ git push --tags <remote>

GitHub example

1st, create or fork a repository on GitHub

# Clone
$ git clone git@github.com:username/Spoon-Knife.git
 
# Add remote for upstream-repository (read-only)
$ cd Spoon-Knife
$ git remote add upstream git://github.com/octocat/Spoon-Knife.git
$ git fetch upstream
 
# Push commits into GitHub-Repository
$ git push origin master
 
# Pull in upstream-changes
$ git fetch upstream
$ git merge upstream/master
# alternatively auto-merge: $ git pull upstream

Environment variables

# Set different location for .git directory
GIT_DIR=/root/etc.git
 
# Set working tree
GIT_WORK_TREE=/etc
 
# Let cURL be verbose; useful for debugging
GIT_CURL_VERBOSE=1
 
# Ignore invalid/unknown SSL certificate; TESTING ONLY!
GIT_SSL_NO_VERIFY=true
 
# Use specified root-certificate for validating SSL certificates, e.g. when using own Certificate authority
GIT_SSL_CAINFO=/path/to/root-ca.pem

Git and SSL with own Certificate authority

See also: http://repo.or.cz/h/rootcert.html

  1. Variant: Global config for specific host (requires Git >= 1.8.5)
    git config --global http.https://git.example.com.sslCAInfo /path/to/root-ca.pem
  2. Variant: Global config for all hosts (which won't likely work with all other hosts…)
    git config --global http.sslCAInfo /path/to/root-ca.pem
  3. Variant: Using temporary environment variable or runtime config
    git -c http.sslCAInfo=/path/to/root-ca.pem ls-remote https://git.example.com/playground.git
     
    GIT_SSL_CAINFO=/path/to/root-ca.pem ls-remote https://git.example.com/playground.git
  4. Variant: Repository config (prerequisite: already cloned repository, e.g. with variant 3)
    git config http.sslCAInfo /path/to/root-ca.pem
     
    # Example: 1) clone  2) set sslCAInfo in repository config  3) normal operation
    git -c http.sslCAInfo=/path/to/root-ca.pem clone https://git.example.com/playground.git
    cd playground
    git config http.sslCAInfo /path/to/root-ca.pem
    ...
    git push
git.txt · Last modified: 2014-01-11 01:15 CET by dominik