Skip to content

Instantly share code, notes, and snippets.

@eeichinger
Last active June 29, 2016 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eeichinger/3d1c3ab0a0a30c94d21027414c3690f3 to your computer and use it in GitHub Desktop.
Save eeichinger/3d1c3ab0a0a30c94d21027414c3690f3 to your computer and use it in GitHub Desktop.
Git Cheatsheet - git commands I found useful every day

Reference sheet by topic: http://ndpsoftware.com/git-cheatsheet.html

Standard Development Tasks

# create new branch off current master/HEAD
git checkout -b <branch> # create new branch off current branch's HEAD
git push -u origin <branch> # push local branch to remote "origin" and track in local branch

# commit changes locally
git add -A :/ # state all local changes, incl. new (=untracked) files
git commit -m "JIRA-XXXX, your message"

# push current branch to server
git push

# delete local & remote branch
git branch -d <branch>
git push origin --delete <branch>

# update remote branch info (refresh all remotes' branches, add new ones and removing deleted ones)
git remote update --prune

Full Pull Request Workflow

## Ensure repo is in clean state
a) either clone
git clone https://....

b) or clean working dir
git clean -dfx

# bring master + branch up2date
git checkout <branch> && git pull
git checkout master && git pull

# merge master into branch -> branch MUST be up2date! (hence --ff-only !)
git merge --ff-only origin/master

# if desired merge branch into master, ensure branch info is preserved (hence --no-ff)
git checkout master
git merge --no-ff <branch> # merge branch into master

# run build+tests
e.g. "mvn clean verify -Pjenkins"

# push changes back to remote
git push # push changes back

# delete remote+local branch
git push origin --delete <branch>
git branch -d <branch>

Other useful

# Git "Pickaxe" to search for commits that introduce or remove a particular line of source code
# (from https://www.atlassian.com/git/tutorials/git-log/filtering-the-commit-history)
git log -S"Hello, World!"

# History of specific file (incl. renames)
git log --follow filename

# History of folder (incl. renames)
git log --follow -- /path/to/folder

# Save "dirty" working folder temporarily with stashing
git stash save [<name>]

# restore saved stash
git stash apply [<name>]

# list stash stack
git stash list

# Configure global settings
git config --global merge.ff false

# checkout a remote branch and track local (assumes remote branch exists)
git checkout -b JIRA-xxx origin/JIRA-xxx

# fetch latest remote repository info for currently tracked branchs
git fetch

# fetch all branches
git fetch --all

# stage all changes (file adds/modify/removes, inkl untracked) in the local copy
git add -A :/

# discard local changes to tracked files+dirs
git reset --hard

# remove untracked files, incl ignored files + dirs
git clean -dx

# create new local branch directly off remote branch
git fetch --all # get latest changes
git checkout -b JIRA-xxxx origin/master
git pull

# display commit log incl changed files:
git log --stat --pretty

# display local commits not pushed yet on current branch:
git log origin/master..master

# display any local commits on any branch not pushed yet:
git log --branches --not --remotes

# delete remote branch
git push origin --delete <branch>

# update list of remote branches
git remote update -p

# display branch graph
git log --graph --all --oneline
git log --oneline --all --graph --decorate

# display what changed between current branch and other branch
git whatchanged ..origin/master

# clean all local branches except master (from https://coderwall.com/p/x3jmig/remove-all-your-local-git-branches-but-keep-master)
git config --global alias.clean-branches "!git branch | grep -v master | xargs git branch -d"

Code review for merging onto master

# make sure master is up2date
git checkout master
git pull --rebase

# merge the feature branch, but don't commit -> shows the changes as uncommitted changeset
git merge --no-commit --no-ff origin/JIRA-xxxx
# reset changes
git reset --hard

Git Configuration

configure individual flags

# Turn off fast-forward for merges
git config --global merge.ff false # use --global for personal settings

#If you are using a windows box, use
git config --global core.autocrlf true

#Check your git config using
git config -l

# If your username and email are not set, use
git config --global user.name first.last
git config --global user.email first.last@your.email.com

# create useful aliases
git config --global alias.timeline "log --oneline --graph --decorate"
git config --global alias.timeline-all=log --oneline --all --graph --decorate

set global .gitignore file

# core.excludesfile=~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global

Git configuration file example

either $repo/.gitconfig or ~/.gitconfig (=--global)

https://gist.github.com/fubhy/6156279
user.email=your.email@example.com
user.name=Max Mustermann
mergetool.keepbackup=true
core.excludesfile=~/.gitignore_global
core.filemode=false
core.ignorecase=false
merge.ff=false # turn off fast-forward for merging
push.default=current
credential.helper=osxkeychain
# branch.autosetuprebase=always # use rebase by default - not recommended
http.postbuffer=524288000 # fur pushing large files
format.pretty=%h, %ad, %s%x09(%an) # change git log output to your liking
log.date=iso

Git via proxy

via HTTP(s)

env vars

GIT_SSL_CAINFO=/usr/ssl/certs/ca-bundle.crt
GIT_SSL_NO_VERIFY=true
GIT_CURL_VERBOSE=1
HTTP_PROXY=http://user:password@129.168.10.12:3140/

or ~/.gitconfig

[http]
sslCAinfo = /home/radium/certs/cacert.pem
proxy = http://user:password@192.168.10.12:3140/
sslverify = false

via ssh tunnel

in ~/.ssh/config

Host bitbucket.example.com
     ProxyCommand ssh <user>@hop.server.com -q -W %h:%p

Configure Merge Tool

# p4merge
git config --global diff.tool p4mergetool
git config --global difftool.p4mergetool.cmd "/Applications/p4merge.app/Contents/Resources/launchp4merge \$LOCAL \$REMOTE"
git config --global merge.tool p4mergetool
git config --global mergetool.p4mergetool.cmd "/Applications/p4merge.app/Contents/Resources/launchp4merge \$PWD/\$BASE \$PWD/\$REMOTE \$PWD/\$LOCAL \$PWD/\$MERGED"
git config --global mergetool.p4mergetool.trustExitCode false
git config --global mergetool.keepBackup false

# SourceTree
git config --global diff.tool sourcetree
git config --global difftool.sourcetree.cmd opendiff "$LOCAL" "$REMOTE"
git config --global difftool.sourcetree.path
git config --global mergetool.sourcetree.cmd=/Applications/SourceTree.app/Contents/Resources/opendiff-w.sh "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED"
git config --global mergetool.sourcetree.trustexitcode=true

# DiffMerge
git config --global diff.tool diffmerge
git config --global difftool.diffmerge.cmd 'diffmerge "$LOCAL" "$REMOTE"'
git config --global merge.tool diffmerge
git config --global mergetool.diffmerge.cmd 'diffmerge --merge --result="$MERGED" "$LOCAL" "$(if test -f "$BASE"; then echo "$BASE"; else echo "$LOCAL"; fi)" "$REMOTE"'
git config --global mergetool.diffmerge.trustExitCode true

Merge Conflicts

# merge branches
git merge <sourcebranch> (<targetbranch>)
# find merge conflicts:
git status
git ls-files -u
# merging changes using merge tool in case of conflicts:
git mergetool
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment