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 feature

After:

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 main

After:

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 OnCommandResult
maingit merge featureFeature’s work goes INTO main
featuregit merge mainMain’s work goes INTO feature
developgit merge hotfixHotfix’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 main

After:

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 main

After:

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 feature

After (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 feature

Cherry-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 C

After:

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:

  1. Push your feature branch: git push origin feature-branch
  2. Create PR on platform (main/develop ← feature-branch)
  3. Team reviews code
  4. 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 --tags

List tags

git tag
git tag -l "v1.*"

View tag info

git show v1.0.0

Annotated 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:

  1. Identify conflicts (Git tells you)
  2. Edit files to resolve (look for <<<<, ====, >>>> markers)
  3. Stage resolved files: git add <file>
  4. Complete merge: git commit (or git rebase --continue if rebasing)

Abort a merge:

git merge --abort

Mental 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.


References