-1

I was expecting a merge conflict when I tried to merge a branch into master. Both of these branches have a text file with different text in them.

# make project directory
mkdir projA
cd projA

# initialize git repo
git init

# make commit in master branch
echo "text 1" > fileA.txt
git add .
git commit -m "commit A"

# make commit in a new branch
git checkout -b branch1
echo "text 2" > fileA.txt
git add .
git commit -m "commit B"

# merge branch into master
git checkout master
git merge branch1

But the merge command simply does a fast-forward merge and keeps the text that was present in the txt file of brach1 and not the master branch.

Can somebody please explain to me why did git not throw a merge conflict?

n0obcoder
  • 649
  • 8
  • 24

1 Answers1

5

Merge conflicts occur when the commit histories of the branches you are merging cannot be automatically reconciled (via a fast-forward or a merge-commit).

Your commit history for master is: A
Your commit history for branch1 is: A - B
So merging branch1 into master causes no conflicts since all it needs to do is apply B on top of A.

Say you had another commit C such that:
Your commit history for master is: A - C
Your commit history for branch1 is: A - B
Then you would potentially get a merge-conflict since your branch says to apply C onto A while another branch says to apply B onto A. However, if B and C did not change the same files, you would still not get a merge-conflict since git can figure out how to merge them automatically.

Ali Samji
  • 479
  • 2
  • 7
  • I could not understand that completely. What do you mean by commit history? – n0obcoder May 19 '21 at 14:21
  • 1
    Commit history is the ordered list of commits applied to the branch — this is what you see when you run `git log`. Merge-conflicts occur when git cannot automatically resolve the differences in the commit histories of the branches you are trying to merge. – Ali Samji May 19 '21 at 16:17
  • 1
    @n0obcoder: this tends to become clearer when you run `git log --all --decorate --oneline --graph` in a repository with development going on in several branches. See also [Pretty Git branch graphs](https://stackoverflow.com/q/1057564/1256452). – torek May 20 '21 at 02:48