Day 6
Merging branches
If branch happens, then change happens. You are going to want to incorporate the work from your separate branch back into your main line of development. git merge
and git mergetool
are two tools to deal with the consequence of a branch being so easy to do!
Typically main is the branch designated to accept all merges, but YMMV. A merge results in a commit that has two+ parent commits.
Commands in Play
- git merge
- git checkout [main]
- git diff main…bugfix -> § see below
- git diff —name-status main…bugfix -> §§
- git log -1 -> §§§
- git merge —abort
- git config
The output from
git diff main...bugfix
shows the difference between branches relative to when they became different. It’s a preview of what merge will do. The order is significant for it: “main” is listed first and appears on top (“the highway”), bugfix is listed second (“the on-ramp”).
This command is overkill,in the example, but it demonstrates that bash script ‘baz’ (from bugfix) will be merged into main by the identifying glyph ‘M’. For large repositories, this command gives a useful summary of the actions that will be performed on
merge
.
Another sampling of “what was done” (after
git merge
). At the CLI, we have the SHA1 IDs that made up the merge commit. It’s the same ingitk
, maybe a little easier to interpret\see.
10.3.1 Dealing with merge that needs human
Now we will refresh\restart and append a printf
statement to bash script ‘baz’ on branch main. Nothing has changed on branch bugfix. But we cannot auto-merge them via the tools.
Auto-merging baz
CONFLICT (content): Merge conflict in baz
Automatic merge failed; fix conflicts and then commit the
result.
Having gotten this error, bash script ‘baz’ on branch main has been altered, and it was altered with the glyphs denoting the local change <<<<<< HEAD
and the remote change >>>>>> bugfix
. We can see what happened and adjust bash script ‘baz’ to take the preferred code.
10.3.4 Aborting a merge
Short\Easy mode: If you are mid-merge and in a moment of realization that you have selected incorrectly, you can perform a git diff
to see the conflicted hunks and abandon it via git merge --abort
if necessary.
Chap 16 covers revert, or to take back, a merge.
10.4 Performing fast-forward merges
Definition: The fast-forward merge takes effect when the target branch is a descendant of the branch that it will merge with.
/Once Git detects that the branch being merged is a direct descendent of the current branch, it moves the local branch up on the remote branch–a fast forward to main
!/
10.6.2 Changing how conflicts are displayed (merge.conflictstyle)
Enable this configuration (using git config
) to subtly change the way conflicting hunks are displayed in a file.
Performing octopus merges
An octopus merge is a merge that consists of more than two parents.
References
How to use VS Code as your Git editor, difftool, and mergetool
/I had not configured a difftool nor mergetool in ~/.gitconfig
The blog post, above, ☝️ was a useful reference. I briefly considered using graphical comparison tool (i.e. bcomp4), too!/
HOW TO RESOLVE CONFLICTS from git merge command documentation
octopus-merge README.md
Kindly forked from code ocelot ☝️. This README as explainer for “famous octopus merge”.