dm4
4/30/2011 - 8:08 AM

Git Tips

Git Tips

(Several of these git examples have been extracted from the book 'Pragmatic guide to GIT' of Travis Swicegood )

Git tips

Global git user

git config --global user.name "Fernando Guillen"
git config --global user.email "fguillen.mail+spam@gmail.com"

Repository git user

cd /develop/myrepo
git config user.name "Fernando Guillen"
git config user.email "fguillen.mail+spam@gmail.com"

Git editor

git config --global core.editor /usr/bin/vim

Init a local repo and first commit

cd /develop/myrepo
git init
git touch README.md
git add README.md
git commit -m "first commit"

Clone a remote repo

git clone git@gist.github.com:747678.git myrepo

Clone but taken only the las 50 commits

git clone --depth 50 git@gist.github.com:747678.git my_repo

Status

git status

Stagging

git add path/to/file
git add path/to/folder
git add .
git add -A|--all
git add -u|--update
git add -p|--patch [path/to/file]  # choose the changes
git add -e [path/to/file]          # edit the diff

Commiting

git commit -m "Message"
git commit -a -m "Message"         # adding all changed files
git commit                         # open editor for Message
git commit --amend
git commit --amend -C HEAD

Stashing

git stash
git stash apply
git stash pop                     # apply and remove from the stack
git stash list
git stash save --patch
git stash drop <stash_name>       # remove
git stash clear                   # remove all
git stash <branch_name> [<stash_name>]  # create a branch from an stash

Global ignore

echo ".DS_STORE" >> ~/.gitignore 
git config --global core.excludesfile ~/.gitignore

Unstage a file

git reset HEAD -- path/to/file

Undo all uncommited changes

git checkout -- path/to/file       # posible data lost!!

Git file commands

git mv path/to/file path/to/file2
git rm -- path/to/file
git rm -r -- path/to/folder
git rm -f -- path/to/file    

Branching

git branch <branch_name>
git branch <branch_name> 99a0de8    # from an starting point
git branch <branch_name> <other_branch>
git branch --track <branch_name>
git branch --no-track <branch_name>

git checkout <branch_name>
git checkout -b <branch_name>       # create branch and checkout into it

git branch                        # only locals
git branch -r                     # only remotes
git branch -a                     # all
git branch --merged
git branch --no-merged
git branch --contains 99a0de8     # branchs that contain this commit

git branch -d <other_branch>
git branch -D <other_branch>

git push <remote_repo> :<remote_branch>  # deleting a remote branch

Cherry picking

git cherry-pick 99a0de8
git cherry-pick --edit 99a0de8    # edit the message
git cherry-pick --no-commit 99a0de8

Tagging

git tag
git tag v1.0
git tag beta1 99a0de8

git push <remote_repo> v1.0
git push --tags <remote_repo>

git fetch --tags <remote_repo>

git tag -d <tag_name>                 # remove local tag
git push origin :refs/tags/<tag_name> # remove remote tag

Mergin

git merge <other_branch>
git merge --no-commit <other_branch>  # no create a commit
git merge --no-ff <other_branch>      # force create a merge commit
git merge --log <other_branch>        # one-line log for each merged commit
git merge -m "Message" <other_branch> # message for this merge commit log

Rebasing

git rebase <other_branch>
git rebase 99a0de8
git reset --hard ORIG_HEAD          # undo last rebase after it completes

git rebase --continue
git rebase --abort

git rebase -i 99a0de8
git rebase -i <other_branch>
git rebase -i HEAD~5
git rebase -i HEAD^^^^^

logging

git log
git log --oneline

git log -5
git log HEAD^^^^^..HEAD
git log HEAD~5..HEAD
git log -1 -p HEAD              # see changes in last commit

git log -- path/to/folder
git log -- path/to/file

git log --since="1 week"
git log --after="7 days"
git log --before="1 week"
git log --until="7 days"

git log --author="Some Author"

git log --grep="some [Rr]eg[Ee]x"
git log --grep="some [Rr]eg[Ee]x" --regexp-ignore-case

Find the commits that have modified a file

git log --raw --abbrev=40 --pretty=oneline | grep -B 1 `git hash-object <filename>`

Commits in branchA that are not in branchB

git log branchA ^branchB

Comparing

git diff
git diff --staged
git diff HEAD
git diff 99a0de8
git diff 99a0de8..67aab84

git diff -- path/to/file

Remote repositories

git remote add <remote_repo> git@gist.github.com:747678.git
git remote rm <remote_repo>

git fetch <remote_repo>
git fetch <remote_repo> <local_branch>:remotes/<remote_repo>/<remote_branch>
git fetch --all

git pull
git pull <remote_repo> <remote_branch>
git pull <remote_repo> <remote_branch>:<local_branch>
git pull -f
git pull --rebase <remote_repo> <remote_branch>  # fetch and rebase

git push
git push <remote_repo> <remote_branch>
git push <remote_repo> <local_branch>:<remote_branch>
git push -f

Statistics

git diff --stat HEAD~10
git diff --stat HEAD~10 HEAD
git diff --stat 99a0de8 67aab84

git diff --shortstat HEAD~10
git diff --numstat HEAD~10

git diff --stat -p HEAD

git log --stat
git log --shortstat

Fixing commits

git commit --amend
git commit --amend -C HEAD

git reset --hard HEAD^
git reset --soft HEAD^         # removing last commit without removing the changes

Reverting

git revert 99a0de8
git revert --no-commit 99a0de8

Removing commits

git reset --hard HEAD~3        # removing the last 3 commits
git reset --hard origin/master # reseting to the remote state
git rebase -i 99a0de8^         # using editor to remove the line with 99a0de8

Retrieving deleted commits

git reflog

Bisect

git bisect start
git bisect bad
git bisect good 99a0de8
# bisect process
git bisect reset

git bisect start HEAD 99a0de8
git bisect run /path/to/test/script
git bisect reset

Blame

git blame path/to/filename

Init a remote (bare) repository

git init --bare path/to/repo.git

Creating and Applying patches

Patch with diff

git diff --no-prefix > path/to/patch
patch -p0 < path/to/patch

Patch with format-patch

git format-patch -M 99a0de8                             # creating one patch file for each commit
git format-patch master --stdout > path/to/patch.patch  # creating one full patch file
git apply --stat path/to/patch.patch                    # check differences before appling
git apply --check path/to/patch.patch                   # checking problems before appling
git am path/to/patch.patch                              # apply the patch
git am --signoff path/to/patch.patch                    # apply the patch and sign off it
git apply path/to/patch.patch                           # apply the patch but not stage the changes