cheatsheet

Git Commands Cheatsheet

Essential Git commands for everyday use - squash commits, amend, cherry-pick, and more advanced workflows.

Published: December 10, 2024

Git Commands Cheatsheet

1. Squash Multiple Git Commits into One

When you want to combine multiple commits into a single commit.

Check how many commits to squash

git log -<n>

Rebase head with particular commit

git rebase -i HEAD~<n>

Steps:

  1. Change n-1 commits from pick to squash (or s)
  2. Save file and add commit message
  3. Force push head branch
git push -f origin head

2. Amend a New Commit in Last Commit

Add changes to your last commit without creating a new one.

# Stage your changes first, then:
git commit --amend

# Push the amended commit
git push -f origin head

3. Cherry Pick

Choose a commit from one branch and apply it onto another.

Steps:

  1. Make sure you are on the branch you want to apply the commit to:
git checkout master
  1. Execute cherry-pick:
git cherry-pick <commit-hash>

Quick Reference

TaskCommand
View last n commitsgit log -<n>
Interactive rebasegit rebase -i HEAD~<n>
Amend last commitgit commit --amend
Force pushgit push -f origin head
Cherry pickgit cherry-pick <hash>
View commit hashgit log --oneline

Common Workflows

Cleaning Up Commits Before PR

# See your commits
git log --oneline -10

# Squash last 3 commits
git rebase -i HEAD~3

# In editor: change 'pick' to 'squash' for commits to combine
# Save and write new commit message

# Force push (if already pushed)
git push -f origin head

Moving a Commit to Another Branch

# Get the commit hash from source branch
git log --oneline

# Switch to target branch
git checkout target-branch

# Cherry pick the commit
git cherry-pick abc123

# Push
git push origin target-branch

Tags

gitversion-controldevopscommands

Found this useful?

Subscribe to get more cheatsheets and resources.