Alister West

home is where your code is ...

Git Tips 'n' Tricks

Git log, show, etc.

# Show commit history
git log --graph --oneline --decorate --all

# Explain a commit
git show --stat <sha>

# Show commits in branch that are not in master
git log HEAD ^master

# Show commits in master that are not in new-feature
git log master ^new-feature --no-merges

# commits in master that aren't in origin (yet)
git log master ^origin/master --no-merges

# Tags
git tag -a v1.0.0 -m "Creating the first official version."

Working with remotes

# Error on push
$ git push
warning: You did not specify any refspecs to push, and the current remote
warning: has not configured any push refspecs. ...
$ git config push.default current

# push tags.
$ git push --tags

# setup new remote
$ git remote add testing

# push local branch to remote
$ git push origin feature_x
* [new branch]       feature_x > feature_x

# delete remote branch
$ git push origin :feature_x
- [deleted]        feature_x 

# Undo a commit
# HEAD^  short for "-r(HEAD-1)" or "HEAD~1"
# HEAD^^ short for HEAD~2
$ git reset HEAD^ --hard

# Ignore existing file
# - svn propset svn:ignore myfile.txt etc/
$ git update-index --assume-unchanged etc/myfile.txt

# Delete a staged file
# - you accidentally added a file but what you want to do is delete it.
$ git rm --cached file/i/dont_want_to.keep

# Git pull with alwaysrebase failed?
$ git branch
    * (no branch)

# create patches for last 2 commits
git format-patch HEAD^^..HEAD

git checkout master
git apply --stat  0001-SetOwner-staged.patch
git apply --check 0001-SetOwner-staged.patch
git am  --signoff 0001-SetOwner-staged.patch 
# git apply <patch> will need you to commit those patch changes

# Keep changes at the HEAD of a branch
# - perhaps some dev-env setup stuff
# [master] --a--b          =>    --a--b--d
# [dev]          `c--d     =>             `c

git checkout master
git cherry-pick 0a129c7    # (d)
git checkout dev
git rebase master

# Show specific revision of file
git show <treeish>:<filename>
By Alister West