ID: I202607071558
Status: idea
Tags: git, Cheatsheets
Git Cheatsheet
Core Merge Concept
git merge combines two branches. The direction depends on which branch youāre currently on.
Rule: Git merges the specified branch INTO your current branch.
Basic Syntax
git checkout <target-branch>
git merge <source-branch>- target-branch: Where you are (the branch that will receive changes)
- source-branch: The branch being merged in
Direction Diagrams
Scenario 1: Merge Feature into Main
Before:
gitGraph commit id: "A" commit id: "B" branch feature checkout feature commit id: "C" commit id: "D" checkout main
Commands:
git checkout main
git merge featureAfter:
gitGraph commit id: "A" commit id: "B" branch feature checkout feature commit id: "C" commit id: "D" checkout main merge feature
Result: Featureās commits (C, D) flow INTO main. Main now contains all of featureās work.
Scenario 2: Merge Main into Feature (Sync)
Before:
gitGraph commit id: "A" commit id: "B" branch feature checkout feature commit id: "C" commit id: "D" checkout main commit id: "E" commit id: "F"
Commands:
git checkout feature
git merge mainAfter:
gitGraph commit id: "A" commit id: "B" branch feature checkout feature commit id: "C" commit id: "D" checkout main commit id: "E" commit id: "F" checkout feature merge main
Result: Mainās commits (E, F) flow INTO feature. Feature now has the latest from main, plus its own work.
Quick Reference Table
| Youāre On | Command | Result |
|---|---|---|
main | git merge feature | Featureās work goes INTO main |
feature | git merge main | Mainās work goes INTO feature |
develop | git merge hotfix | Hotfixās work goes INTO develop |
Merge vs Rebase
Both bring changes from one branch into another, but differently.
Merge: Before and After
Before:
gitGraph commit id: "A" commit id: "B" branch feature checkout feature commit id: "C" commit id: "D" checkout main commit id: "E" commit id: "F"
Command:
git checkout feature
git merge mainAfter:
gitGraph commit id: "A" commit id: "B" branch feature checkout feature commit id: "C" commit id: "D" checkout main commit id: "E" commit id: "F" checkout feature merge main commit id: "M (merge commit)"
Result: Creates a merge commit, keeping full history. Both branchesā commits are preserved in order.
Rebase: Before and After
Before:
gitGraph commit id: "A" commit id: "B" branch feature checkout feature commit id: "C" commit id: "D" checkout main commit id: "E" commit id: "F"
Command:
git checkout feature
git rebase mainAfter:
gitGraph commit id: "A" commit id: "B" commit id: "E" commit id: "F" branch feature checkout feature commit id: "C'" commit id: "D'"
Result: Replays your commits (C, D) on top of main. Creates new commits (Cā, Dā), so history is rewritten but stays linear and clean.
Fast-Forward Merge
When the source branch is directly ahead of your current branch (no diverging commits), Git can do a āfast-forwardā merge.
Before:
gitGraph commit id: "A" commit id: "B" branch feature checkout feature commit id: "C"
Command:
git checkout main
git merge featureAfter (Fast-Forward):
gitGraph commit id: "A" commit id: "B" commit id: "C" branch feature
What happens:
- Main pointer just moves forward to featureās latest commit
- No merge commit created
- History stays linear
Force a merge commit (even if FF is possible):
git merge --no-ff featureCherry-Pick
Apply specific commits from one branch to another without merging the entire branch.
Before:
gitGraph commit id: "A" commit id: "B" branch feature checkout feature commit id: "C" commit id: "D" checkout main
Command:
git checkout main
git cherry-pick CAfter:
gitGraph commit id: "A" commit id: "B" branch feature checkout feature commit id: "C" commit id: "D" checkout main commit id: "C' (new commit)"
Cherry-pick multiple commits:
git cherry-pick <commit1> <commit2> <commit3>Cherry-pick a range:
git cherry-pick <start-commit>..<end-commit>Pull Requests (PRs)
A PR is not a Git commandāitās a platform feature (GitHub, GitLab, etc.) for code review before merging.
Workflow:
- Push your feature branch:
git push origin feature-branch - Create PR on platform (main/develop ā feature-branch)
- Team reviews code
- Platform merges when approved (usually with a merge commit)
Benefits:
- Code review before integration
- Discussion and feedback
- Automatic testing/CI
- Clear audit trail
Tags
Mark specific points in history (usually for releases).
Create a tag
git tag v1.0.0
git tag v1.0.0 <commit-hash>
git tag -a v1.0.0 -m "Release 1.0.0"Push tags
git push origin v1.0.0
git push origin --tagsList tags
git tag
git tag -l "v1.*"View tag info
git show v1.0.0Annotated vs Lightweight:
- Annotated: Full metadata (date, author, message) - use for releases
- Lightweight: Just a reference to a commit - use for quick marks
Conflict Resolution
When merging causes conflicts:
- Identify conflicts (Git tells you)
- Edit files to resolve (look for
<<<<,====,>>>>markers) - Stage resolved files:
git add <file> - Complete merge:
git commit(orgit rebase --continueif rebasing)
Abort a merge:
git merge --abortMental Model
Think of merge like this:
āIām standing on branch X. I want to bring branch Yās changes here. So I merge Y into X.ā
The branch youāre on is the destination. The branch you specify is the source.