I completely thought it would automatically take whichever change was ahead given it's comparing it with a earlier commit already on that branch.
This is a perfectly reasonable assumption based on what git log
shows you. Unfortunately, git log
is lying to you by omission. By default git log
shows commits as being in a straight line ordered by time.
commit b8502a5b1efcb971f90d605517d3f93d0a0951de
Author: Michael G. Schwern <schwern@pobox.com>
Date: Sat Nov 14 12:00:00 2020 -0800
Add more foo
commit 20969b1e307efd6152ef43165ae0d11aceb2a2b5
Author: Michael G. Schwern <schwern@pobox.com>
Date: Fri Nov 13 12:00:00 2020 -0800
Fix foo
commit 83693247615e3f528d07f33d955c9a3fbdee2791
Author: Michael G. Schwern <schwern@pobox.com>
Date: Thu Nov 12 12:00:00 2020 -0800
Add more foo
commit 8b4a4381ebe79091ccdcf2ed1214a55c94189cbf
Author: Michael G. Schwern <schwern@pobox.com>
Date: Wed Nov 11 12:00:00 2020 -0800
Change foo
commit ad5f84597211909ff1f1adc3754271817556f7f7
Author: Michael G. Schwern <schwern@pobox.com>
Date: Tue Nov 10 12:00:00 2020 -0800
Add foo
All the commits are working on the same part of the code. If they change the same lines it should just choose the newest commits, right?
git log --graph --decorate --all
will show you the true history.
* commit b8502a5b1efcb971f90d605517d3f93d0a0951de (HEAD -> master)
| Author: Michael G. Schwern <schwern@pobox.com>
| Date: Sat Nov 14 12:00:00 2020 -0800
|
| Add more foo
|
| * commit 20969b1e307efd6152ef43165ae0d11aceb2a2b5 (feature)
| | Author: Michael G. Schwern <schwern@pobox.com>
| | Date: Fri Nov 13 12:00:00 2020 -0800
| |
| | Fix foo
| |
* | commit 83693247615e3f528d07f33d955c9a3fbdee2791
| | Author: Michael G. Schwern <schwern@pobox.com>
| | Date: Thu Nov 12 12:00:00 2020 -0800
| |
| | Add more foo
| |
| * commit 8b4a4381ebe79091ccdcf2ed1214a55c94189cbf
|/ Author: Michael G. Schwern <schwern@pobox.com>
| Date: Wed Nov 11 12:00:00 2020 -0800
|
| Change foo
|
* commit ad5f84597211909ff1f1adc3754271817556f7f7
Author: Michael G. Schwern <schwern@pobox.com>
Date: Tue Nov 10 12:00:00 2020 -0800
Add foo
Now we can see the changes happened in different branches. While b8502a happened after 20969 in time they happened in parallel in space. Branches really branch and that's fundamental to understanding Git. Unfortunately, Git hides that.
Git assumes work in branches was uncoordinated. This lets many people work on the same code independently, or one person work on many things at the same time, without having to keep track of who is changing what. That will be resolved by Git when merging.
Because of this, Git cannot preference changes in one branch vs another. While Git can assume that b8502a was written with knowledge of 836932, they're on the same branch, it cannot assume b8502a was written with knowledge of 20969b nor 8b4a43; even though b8502a is newer than both 20969b and 8b4a43 they're on different branches.
Even if the author is the same, Git does not assume it can preference newer changes on different branches. To do so would assume the author is keeping all their changes in all their branches in mind while they're working, and that defeats the point of branching.