Heesung Yang

Git Commit Cheatsheet

Cancel commit

# cancel last commit. keep changes.
~$ git reset HEAD^

# cancel last 2 commits. keep changes.
~$ git reset HEAD~2

# remove last 2 commits with working tree
~$ git reset --hard HEAD~2

# cancel merged commit
~$ git reset --hard ORIG_HEAD

# undo commit as new commit
~$ git revert HEAD

# change last commit message
~$ git commit --amend

# show diff when commit
~$ git commit -v

# squash last 2 commits
~$ git rebase -i HEAD~2

# split commits
~$ git rebase -i HEAD~2

Cherry-pick

  • Merge specific commits from new-feature branch to master branch
# checkout to new-feature
~$ git checkout new-feature
~$ git log
f0607f24c104b01b9dd2b603bf3c4aa25f90386a commit meesage 2 # <- what I want to cherry-pick
10d84f621e027a59b0c27a255f891aa4c078b06c commit message 1

~$ git checkout master
~$ git cherry-pick f0607f24c104b01b9dd2b603bf3c4aa25f90386a

Change author

# change from `pick` to `edit` what you want to
~$ git rebase -i origin/master

# change the author every commit
~$ git commit --amend --author="Heesung Yang <someone@example.com>"
~$ git rebase --continue

Show commit diff

# git show <commit id>
~$ git show 07d2fca