Saturday, February 16, 2013

Useful Git commands

In this post, I want to show some very useful Git commands for everyday use.

Show diff of single commit

git diff ^<commit>

git diff ^HEAD # Show diff of last commit

Generate git patch from single commit

git format-patch -1 <commit>

git format-patch -1 HEAD # Generate patch from last commit

Show history of a file

git log -p -- path/to/file

Commit as a different author

git commit --author <e-mail>

Edit last commit (not yet pushed)

git commit --amend

Remove last commit (not yet pushed)

Very useful for cases when someone pushed and you commited and didn't pull.
git reset --soft HEAD~1 # HEAD~2 for 2 commits, etc

Stash only some files

To stash files not staged for commit, as seen in picture:
git stash --keep-index

Delete untracked files

git clean -f