In Part 1 you learned the daily routine. As soon as two people change the same script, you’ll meet a merge conflict. It looks scary; it isn’t. This guide shows what’s happening and how to fix it calmly.
What a branch really is
A branch is just a label pointing to a commit. When you commit, the label moves forward. main is a branch like any other – your team just agrees it’s the trusted one.
git branch # list local branches (* = current)
git branch -a # include remote branches
git switch -c fix-disk-threshold # create and switch
git log --oneline --graph --all # see how branches relate
Merge vs rebase
Both bring changes from one branch into another:
- Merge keeps history exactly as it happened and adds a merge commit. Safe and simple.
- Rebase replays your commits on top of the latest
main, giving a straight line of history.
Rule of thumb for admin teams: merge through pull requests; use rebase only on your own branch before you share it. Never rebase a branch others are working on.
Bring the latest main into your branch:
git switch fix-disk-threshold
git fetch origin
git merge origin/main # or: git rebase origin/main (only if the branch is yours alone)
Why conflicts happen
Git merges automatically when changes touch different lines. A conflict happens when two branches changed the same lines differently. Git refuses to guess and asks you.
Resolve a conflict, step by step
-
Git tells you which files conflict:
CONFLICT (content): Merge conflict in scripts/Get-DiskReport.ps1 -
Open the file. You’ll see markers:
<<<<<<< HEAD [int] $WarnPercent = 15 ======= [int] $WarnPercent = 20 >>>>>>> origin/main- Between
<<<<<<<and=======– your version - Between
=======and>>>>>>>– the incoming version
- Between
-
Edit it to the correct final result and delete all three marker lines:
[int] $WarnPercent = 20 - Test the script still works.
-
Mark it resolved and finish:
git add scripts/Get-DiskReport.ps1 git commit # for a merge # or: git rebase --continue (for a rebase)
Changed your mind? git merge --abort (or git rebase --abort) takes you back to before you started.
VS Code shows conflicts with buttons – Accept Current, Accept Incoming, Accept Both – which is easier than editing by hand. Read the result anyway.
Avoid conflicts in the first place
- Keep branches short-lived – merge within a day or two.
- Pull
maininto your branch often. - One topic per branch; don’t reformat whole files in the same change.
- Split big scripts into smaller files so people rarely edit the same lines.
Stash: put work aside for a moment
git stash push -m "half-done threshold change"
git switch main # do something urgent
git switch fix-disk-threshold
git stash pop # get your changes back
Recover “lost” work with reflog
Git rarely loses anything. reflog lists every position your branches have been at:
git reflog
git switch -c rescue abc1234 # create a branch at the commit you want back
FAQ
I committed to main by mistake. What now?
If you haven’t pushed: create a branch there (git branch my-change), then reset main to the remote (git reset --hard origin/main) – only if you’re sure the branch holds your work. If you’ve pushed, use git revert instead.
What’s next
Next: Pull requests and protecting main.
Practise this topic
Test yourself with free questions on this topic – instant answers and explanations.
Start practice questionsGitBranchingMergeIntermediate