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:
- Change n-1 commits from
picktosquash(ors) - Save file and add commit message
- 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:
- Make sure you are on the branch you want to apply the commit to:
git checkout master
- Execute cherry-pick:
git cherry-pick <commit-hash>
Quick Reference
| Task | Command |
|---|---|
| View last n commits | git log -<n> |
| Interactive rebase | git rebase -i HEAD~<n> |
| Amend last commit | git commit --amend |
| Force push | git push -f origin head |
| Cherry pick | git cherry-pick <hash> |
| View commit hash | git 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