Git - Merge Strategies Flashcards
Fast forward merge
Fast forward merge works with a linear path from the current branch tip to the target branch. Instead of merging all git needs to do to integrate the histories is move (I.e. fast forward) the current branch tip up to the target branch tip.
This effectively combines the histories since all of the commits reachable from the target branch are now available through the current one.
This branch workflow is common for short lived topic branches with smaller changes.
If a master has not diverged instead of creating a new commit, it will just point point master to the latest commit of the hotfix branch. All commits from hotfix branch are now available in master
Pulling via rebase
The —rebase option can be used to ensure a linear history by preventing unnecessary merge commits.
It is like saying “I want to put my changes on top of what everyone else has done”
Pulling with —rebase is such a common workflow that there is a dedicated configuration option for it:
git config - - global branch.autosetuprebase always
After running that command all git pull commands will integrate via git rebase instead of git merge.
Git pull in remotes
git checkout new_feature
git pull
This example first performs a checkout and switches to the branch.
Following that, the git pull is executed with being passed.
This will implicitly pull down the newfeature branch from .
Once the download is complete it will initiate a git merge.
Git pull rebase instead of merge
git checkout master
git pull - - rebase origin
The following example demonstrates how to synchronise with the central repository’s master branch using a rebase.
This simply moves your local changes into the top of what everybody has already contributed
What is a merge
Merging takes the endpoints and merges then together.
When to rebase over merge
Rebase local changes you’ve made but haven’t shared yet before you push them in order to clean up your story but never rebase anything you’ve pushed somewhere
Preparing to merge
Let’s assume you want to merge branch hotfix into your master branch
- Check your local repo is up to date with the latest changes from the remote server with a git fetch
- Once fetch is complete git checkout master
- Ensure the master branch has the latest updates by executing git pull
- Checkout to the branch that should receive the changes, in our case that is master
Merging after preparations
Once prep is completed, you can start the merge with git merge hotfix command
Three way merge for diverged branches
When there is no linear way to a target branch, git has no choice but to combine them via three-way merge. This merge uses an extra commit to tie together the two branches.
How to deal with merge conflicts
A merge conflict can arise when two branches you’re trying to merge both changed the same part of the same file. Git won’t be able to figure out which version to use.
e.g. if the file example.rb was edited on the same lines in different branches of the same git repository or if the file was deleted you will get a merge conflict error when you try to merge these branches.
Before you continue the merge conflict has to be resolved with a new commit.
Merge conflicts will only ever occur in the event of a 3 way merge.
How to carry out a merge conflict
- Generate a list of the files which need to be resolved
git status
- When the conflicted line is encountered git will edit the content of the affected files with visual indicators that mark both sides of the conflicted content with:
««< conflict marker
===== divides your changes from the changes in the other branch
»»> end of conflicted lines
- Then decide if you want to keep only your hotfix or master changes or write a completely new code. Delete the conflict markers before merging your changes
- When your ready to merge all you have to do is run for add command on the conflicted files to tell git they are resolved.
- Commit your changes with git commit to generate the merge commit
Git fetch
Only downloads new data from a remote repository but it doesn’t integrate any of this new data into your working files.
Fetch is best for getting a fresh view on all the things happened in a remote repository.
Fetch will never manipulate or destroy anything.