Configuration

Global Settings

Action

Command

show current configuration

git config --global --list

command line colors

git config --global color.ui auto

 

git config --global color.branch.auto

LF (LineFeed) on all platforms:

git config --global core.autocrlf input

use TextMate as default editor:

git config --global core.editor "mate -w"

use Sublime as default editor:

git config --global core.editor "subl -w"

Aliases

examples:

git config --global alias.s "status"
git config --global alias.l "log --stat"
git config --global alias.lpo "log --pretty=oneline --abbrev-commit --graph --decorate"

git config --global alias.sync "!git pull & git push"

(shell command begins with '!')

example configurations from .gitconfig

[alias]
  s = status
  lpo = log --pretty=oneline --abbrev-commit --graph --decorate
  orphank = !gitk --all `git reflog | cut -c1-7`&
  hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short

Commit Code Changes

git add

Action

Command

split changes into several commits

git add . -p

accept deleted into staging

git add -u .

shortcut to accept all changes

git add -A .

git commit

Action

Command

verbose display when committing

git commit -v

shortcut for commit message

git commit -a -m"This is my great message"

change last commit message while in staging

git commit --amend

Display Changes and Log

Action

Command

show log

git log

last commit of current branch

git diff HEAD

display log with rename and copy

git log --stat --C (for extended display use --find-copies-harder--)

difference to a specific repository

git diff HEAD remote/name/master

difference to a branch

git diff master feature1

show branch

git show-branch

display executed commands

git reflog

view local repository in browser

git instaweb --httpd webrick

tool to visualize git

gitk --all& (all branches)

Branches, Merges and Tags

Branches

Action

Command

show current branch

git branch

show all branches (including remote branches)

git branch -a

non-merged branches

git branch --no-merged

merged branches

git branch --merged

create branch

git branch name

go to a specific branch (also tags)

git checkout branchname

 

git checkout master

shortcut for creating a branch and checking it out

git checkout -b branchname

push branch changes

git push -u pushable branchname

get branch from repository

git checkout remotes/origin/branchname

change branch number of steps

git checkout master^^ (go back two steps)

Merges

git checkout master
git merge branchname

Tags

Action

Command

show tags

git tag -l

create tag

git tag tagname

remove specific tag

git tag -d tagname

tag with message

git tag -a tagname

push tags to (remote) repository

git push --tags (origin master) / git push (origin master) tagname / git push --all origin master

remove tag from (remote) repository

git push repo :tagname

Repositories Push, Fetch, Pull

Initialize Repository

create a directory and init with

git init
git add .
git commit

(git remote / git remote -v)

add repository to configuration

git remote add reponame https://schniebiwutz.git 

(goes to .git/config)

git pull/fetch reponame
git diff HEAD remotes/reponame/master

Difference Fetch / Pull

git pull: combination of git fetch and git merge, pulls and merges all changes to all your files

git fetch: fetches changes from the repository but does NOT update files changed locally

Rebasing

Several commits can be combined to one using the rebase command. Use this to clean up the commit history before pushing changes to a remote repository.

git checkout myfeaturebranch
git rebase master

Undo and Recovery

Restore accidentally deleted files

display deleted files

git ls-files -d

restore the deleted files

git ls-files -d | xargs git checkout -

Other Restore Commands

git reset HEAD first.java

revert to last commit / version

git reset --hard
git reset --hard 78827

discard last commit but keep code

git reset --soft

discard local changes

git remote prune name (origin)

go back to a specific version

git revert 2F66a

clean commands

git clean -n
git clean -fd

emergency rescue

git reflog
git reset --hard 2727 (taken from reflog, if not already killed by GC)

edit last commit message

git commit --ammend

add note

git notes add master^^^^

Misc

Stashing

stashing is only local

git stash
git stash list
git stash apply (this applies and leaves the changes in the stack as opposed to "pop")
git stash pop (back)
git stash branch newbranch
git stash branch newbranch stashname

Other commands

check all hashes

git fsck

check who created a file

git blame filename

Configure Specific Repositories

SSH

Take the content of the file

.shh/id-rsa.pub

and paste it to the repository configuration.

Test SSH connections:

Github: ssh git@github.com
Gitolite: ssh gitolite@localhost

get a verbose protocol by using

ssh -v

Github Repository

git remote add origin git@github.com:doofenschmerz/schniedel.git
git push origin master

Bitbucket Repository

git remote add origin git@bitbucket.org:doofenschmerz/wutz.git
git push origin master

Git Resources

3 Comments

  1. Looks great - why not make this public ?
    (although perhaps it's redundant with "THE" Git cheat sheet ? If specific tricks are here, perhaps they could go to Git tips and tricks ?)

    Also please see my general comment on the homepage of this site. I'd be in favor of sharing all these tips publicly !

    1. Yes, this is great. If it isn't public already, make it public!

  2. Ok, moved it. Maybe we can consolidate some of the Git stuff in the future.