Merging takes the work from one branch and integrates it into another.
When the target branch hasn't diverged, Git simply moves the pointer forward.
git switch main
git merge feature/signupUpdating a1b2c3d..d5e6f7g
Fast-forward
signup.js | 5 +++++
1 file changed, 5 insertions(+)
create mode 100644 signup.js
Before:
main: A --- B
\
feature/signup: C --- D
After:
main: A --- B --- C --- D
🧠 What happened? No merge commit was created. Git simply moved main forward to where feature/signup was.
When both branches have new commits, Git creates a merge commit.
# Set up: both branches have diverged
git switch main
echo "main work" >> app.js && git add . && git commit -m "Work on main"
git switch feature/login
echo "login work" >> login.js && git add . && git commit -m "Work on login"
git switch main
git merge feature/loginMerge made by the 'ort' strategy.
login.js | 1 +
1 file changed, 1 insertion(+)
git log --oneline --graph* h8i9j0k Merge branch 'feature/login'
|\
| * f7g8h9i Work on login
* | e4f5a6b Work on main
|/
* a1b2c3d Initial commit
Conflicts happen when both branches changed the same lines.
# On main
echo "version = 2" > config.txt && git add . && git commit -m "Set version 2"
# On feature branch
git switch -c feature/config
echo "version = 3" > config.txt && git add . && git commit -m "Set version 3"
# Try to merge
git switch main
git merge feature/configAuto-merging config.txt
CONFLICT (content): Merge conflict in config.txt
Automatic merge failed; fix conflicts and then commit the result.
cat config.txt<<<<<<< HEAD
version = 2
=======
version = 3
>>>>>>> feature/config
Edit the file to keep what you want:
version = 3
Then:
git add config.txt
git commit -m "Merge feature/config: use version 3"🧠 What happened? Git couldn't automatically decide which version to keep, so it put both versions in the file with markers. You manually resolved it, then committed the result.
If you're in the middle of a conflict and want to start over:
git merge --abortEverything goes back to the state before you ran git merge.
git merge --no-ff feature/signupThis creates a merge commit even when fast-forward is possible. Useful for preserving the fact that work happened on a branch.
git merge --squash feature/signup
git commit -m "Add signup feature"🧠 What happened? All commits from the branch are combined into a single set of changes in the staging area. You then commit them as one commit. The branch's individual commits don't appear in main's history.