Git Branching, Merging, Rebase and Resolving Conflicts

DevOps Certification 20 min min read Updated: Nov 17, 2026 Beginner
Git Branching, Merging, Rebase and Resolving Conflicts
Beginner Topic 2 of 3

What Is a Branch?

A branch is a movable pointer to a commit. Creating a branch is instant and cheap, so you can try an idea or build a feature without touching main.

text main: A---B---C \ feature: D---E

Branch Commands

bash git branch # list local branches git branch -a # include remote branches git switch -c feature/login # create and switch (modern) git checkout -b feature/login # older way, same result git switch main # go back git branch -d feature/login # delete a merged branch git branch -D feature/login # force delete git push origin --delete feature/login # delete remote branch git branch -m old-name new-name # rename
Naming: Use short, clear names: feature/login-page, fix/null-pointer, chore/update-deps, release/1.4.

Merging

Merging brings the work of one branch into another. Switch to the receiving branch first.

bash git switch main git merge feature/logi

Fast-forward merge

If main has no new commits since you branched, Git simply moves the main pointer forward. No merge commit is created.

Three-way merge

If both branches have new commits, Git creates a merge commit with two parents.

text Before: After merge: main: A---B---C A---B---C-------M \ \ / feature: D---E D-------E
bash git merge --no-ff feature/login # always create a merge commit (keeps branch history visible) git merge --squash feature/login # combine all feature commits into one staged change

Merge Conflicts

A conflict happens when both branches changed the same lines. Git cannot choose, so it asks you.

bash git merge feature/login # Auto-merging config.yaml # CONFLICT (content): Merge conflict in config.yaml # Automatic merge failed; fix conflicts and then commit the result. git status # shows "both modified: config.yaml"

Open the file. Git marks the conflict:

text <<<<<<< HEAD replicas: 3 ======= replicas: 5 >>>>>>> feature/logi

The part between <<<<<<< and ======= is your current branch. The part below is the incoming branch. Edit the file to the final correct content and delete the marker lines.

yaml replicas: 4
bash git add config.yaml git commit # completes the merge (message is prefilled) # or give up and return to the state before merging git merge --abort
Reduce conflicts: Pull often, keep branches short-lived, make small pull requests and avoid reformatting whole files.

Rebase

Rebase replays your commits on top of another branch, giving a straight, linear history without merge commits.

text Before rebase: After git rebase main (on feature): main: A---B---C main: A---B---C \ \ feature: D---E feature: D'---E' (new commits, same changes)
bash git switch feature/login git fetch origin git rebase origin/main # replay D and E on top of the latest main # if a conflict appears: fix files, then git add <file> git rebase --continue # or stop git rebase --abort
MergeRebase
HistoryKeeps true history with merge commitsLinear, cleaner history
Commit idsUnchangedRewritten (new ids)
ConflictsResolved onceMay be resolved per commit
Safe on shared branchesYesNo
Best forMerging into main, preserving contextUpdating your own branch before a pull request
Golden rule of rebase: Never rebase commits that other people have already pulled. Rewriting shared history forces everyone to repair their clones. Use git push --force-with-lease only on your own branches.

Interactive Rebase: Clean Up Before a Pull Request

bash git rebase -i HEAD~4 # edit the last 4 commits
text pick 1a2b3c4 feat: add login form squash 5d6e7f8 fix typo squash 9a0b1c2 wip reword 3d4e5f6 add tests # pick = keep, squash = merge into previous, reword = change message, # drop = remove, edit = stop to change the commit

Cherry-Pick

Copy one specific commit from another branch, for example to apply a hotfix to a release branch.

bash git switch release/1.4 git cherry-pick 3f2a1bc git cherry-pick A..B # a range

Typical Feature Flow

  1. git switch -c feature/x from an updated main
  2. Commit small changes while you work
  3. git fetch and git rebase origin/main to catch up
  4. git push -u origin feature/x
  5. Open a pull request, get review and passing CI
  6. Merge (squash, merge commit or rebase, per team rule)
  7. Delete the branch

Practice Tasks

  1. Create two branches that change the same line and resolve the conflict
  2. Do the same merge once with --no-ff and once as a fast-forward and compare git log --graph
  3. Make five messy commits, then squash them into one with git rebase -i
  4. Cherry-pick a bug fix from main to a release/1.0 branch
  5. Abort a conflicted rebase and a conflicted merge safely

Conclusion

Branches let you work in parallel. Merge keeps history exactly as it happened, rebase makes it linear, and conflicts are just two edits you need to combine. Remember never to rebase shared commits.

Quick Summary: branch = pointer; merge (ff or 3-way); rebase for your own branch; conflict markers; cherry-pick; never rebase shared history.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators