Skip to main content
Chapter 8 of 61 · 6 sections · Page 54

Git: Merging

Perform automatic merges, fast-forward merges, no-ff commits, merge strategies, and abort merges.

Original chapter reference: Chapter 8: Merging. The command options and documentation recipes below are fully transcribed from Git Notes for Professionals.

Parameter Details -m Message to be included in the merge commit -v Show verbose output --abort Attempt to revert all files back to their state --ff-only Aborts instantly when a merge-commit would be required --no-ff Forces creation of a merge-commit, even if it wasn't mandatory --no-commit Pretends the merge failed to allow inspection and tweaking of the result --stat Show a diffstat after merge completion -n/--no-stat Don't show the diffstat --squash Allows for a single commit on the current branch with the merged changes

Section 8.1: Automatic Merging

When the commits on two branches don't conflict, Git can automatically merge them:

Source example · PDF page 54
~/Stack Overflow(branch:master) » git merge another_branch
Auto-merging file_a
Merge made by the 'recursive' strategy.
 file_a | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Section 8.2: Finding all branches with no merged changes

Sometimes you might have branches lying around that have already had their changes merged into master. This finds all branches that are not master that have no unique commits as compared to master. This is very useful for finding branches that were not deleted after the PR was merged into master.

Source example · PDF page 54
 for branch in $(git branch -r) ; do
   [ "${branch}" != "origin/master" ] && [ $(git diff master...${branch} | wc -l) -eq 0 ] && echo -
e `git show --pretty=format:"%ci %cr" $branch | head -n 1`\\t$branch
 done | sort -r

Section 8.3: Aborting a merge

After starting a merge, you might want to stop the merge and return everything to its pre-merge state. Use --abort:

Source example · PDF page 54
git merge --abort

Section 8.4: Merge with a commit

Default behaviour is when the merge resolves as a fast-forward, only update the branch pointer, without creating a merge commit. Use --no-ff to resolve.

Source example · PDF page 54
git merge <branch_name> --no-ff -m "<commit message>"

Section 8.5: Keep changes from only one side of a merge

During a merge, you can pass --ours or --theirs to git checkout to take all changes for a file from one side or the other of a merge.

Source example · PDF page 55
$ git checkout --ours   -- file1.txt # Use our version of file1, delete all their changes
$ git checkout --theirs -- file2.txt # Use their version of file2, delete all our changes

Section 8.6: Merge one branch into another

Source example · PDF page 55
git merge incomingBranch

This merges the branch incomingBranch into the branch you are currently in. For example, if you are currently in master, then incomingBranch will be merged into master.

Merging can create conflicts in some cases. If this happens, you will see the message Automatic merge failed; fix conflicts and then commit the result. You will need to manually edit the conflicted files, or to undo your merge attempt, run:

Source example · PDF page 55
git merge --abort
Buy me a coffee
Daily Practice
0/0