Hello guys 🙂 this post I would like to share with you about the "
git merge" experiments.
We have multiple code lines in the project, once the developer completes his task branch is ready to move to the next branch for testing.
|
Git Merge and git rebase |
There are multiple ways to combine the code lines. If the HEAD pointed to the tip of the branch then the best way to merge from the other branches into the current branch.
- FF fast forward merge
- 3 Way merge
You will be on the target-branch (most of the time it will be main/master or release or production) and then you will run the following command with a source-branch.
git merge [branch-name]
The following example for the Git Merge 'ort' Strategy, let's begin
clear
mkdir merge_test; cd merge_test; git init
# on main branch two commits
touch test1 ; git add . ; git commit -m "test1"
touch test2 ; git add . ; git commit -m "test2"
# branch out feature and do 2 commits
git checkout -b feature
touch test3 ; git add . ; git commit -m "test3"
touch test4 ; git add . ; git commit -m "test4"
# switch back to main branch 2 commits
git switch main
touch test5 ; git add . ; git commit -m "test5"
touch test6 ; git add . ; git commit -m "test6"
graph
#confirm which branch
git branch
# merge the feature branch to main
git merge feature
After merge
|
Git Merge 'ort" Strategy |
The git merge execution example is given below:
# clone a project repo from GitHub/BitBucket
# Create branch a-branch do a commit
git checkout -b a-branch
touch a ; git add ; git commit -m"a changes"
git status -s
# Get the main branch on top it create b-branch
git switch main
git checkout -b b-branch
# b changes commit
touch b ; git add ; git commit -m"b changes"
git status -b # assuming that you are on the b-branch
# get all the changes in a-branch get into the b-branch
# HEAD pointing to b-branch
git merge a-branch
git log --oneline --graph -all
You can also clearly mention on the that what is source and target branch as shown below:
git merge [source branch] [target branch]
Merging a branch newbranch into a target branch as master.
git branch
git merge newbranch master
git log --oneline --graph -all
There is one more thing here "git rebase". Let's deep dive into rebase experiment now.
As per my understanding rebase will be used when we have few changes on a feature branch from there you worked on some more changes in a latest branch and those changes you wish to include into the master then rebase is best choice and when you merge the third level branch to master it will only considers the latest branch commits and it does not consider it's parent commits during master branch merge which is actual fast forward. Here is my understanding
|
Before git rebase |
Now let's do the rebase from the feature2 branch to master branch as shown below
|
Example of feature2 branch rebase to master from feature1 branch |
The rebase and fast-forward merge execution sample is given below:
|
Example of rebase here rebased to from feature2 to master, that means excluding feature1 |
After the rebase of a branch it will be fast-forward merge only.
|
Git fast-forward merge automatically after rebase |
Fast-forward merge experiment
We can use the --ff and --ff-only options when doing the merging a branch. here is the each one option meaning from the git help
--ff
When the merge resolves as a fast-forward, only update the branch pointer, without creating a
merge commit. This is the default behavior.
--ff-only
Refuse to merge and exit with a non-zero status unless the current HEAD is already up-to-date or
the merge can be resolved as a fast-forward.
|
git merge fast forward |
master) B
c313a34 A
scmuser@localhost ~/merging (master)
$ git checkout -b "feature"
Switched to a new branch 'feature'
scmuser@localhost ~/merging (feature)
$ touch C; git add .; git commit -m"C"
[feature 76e64d8] C
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 C
scmuser@localhost ~/merging (feature)
$ touch D; git add .; git commit -m"D"
[feature 2c421a7] D
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 D
scmuser@localhost ~/merging (feature)
$ git log --oneline --graph --all
* 2c421a7 (HEAD -> feature) D
* 76e64d8 C
* 2548379 (master) B
* c313a34 A
scmuser@localhost ~/merging (feature)
$ git switch master
Switched to branch 'master'
scmuser@localhost ~/merging (master)
$ git status -b -s
## master
scmuser@localhost ~/merging (master)
$ git merge feature
Updating 2548379..2c421a7
Fast-forward
C | 0
D | 0
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 C
create mode 100644 D
scmuser@localhost ~/merging (master)
$ git branch -d feature
Deleted branch feature (was 2c421a7).
scmuser@localhost ~/merging (master)
$ git log --oneline --graph --all
* 2c421a7 (HEAD -> master) D
* 76e64d8 C
* 2548379 B
* c313a34 A
scmuser@localhost ~/merging (master)
$ ls
A B C D
What is Merge Conflicts?
If two collaborators use the same file for editing on the code lines. When it's pushed to the remote repository there is a chance to conflict.
mkdir mergeconflict; cd mergeconflict
git init
vi hello.py
# Entered line as - print("Hello World!")
git add .; git commit -m"Initial commit"
# takeout a new branch from master
git checkout -b vastav
# Edit the file replaced with - print("Hello Vastav!")
vi hello.py
git add .; git commit -m"vastav commit"
#switch back to master branch
git switch master
# modify the same line with - print("Hello Dihitha")
git add .; git commit -m"Dihitha commit"
# Double check you are on master branch and ready for merge vastav branch
git merge vastav
# Observe there is merge conflict
|
Git merge conflict |
You can open the file in vi or vs code at your convenience. The conflict is represented by ">>>" and "===" symbols. If two teammates discuss the conflicts and then decide what code lines should be preserved and what lines can be removed.
|
Git merge conflict and its resolution |
Once you save the conflict file, it will be committed to the local repo.
Cherry-pick Merge
How does cherry-pick works?
We want to pick up a specific commit from a different branch and apply to the current branch. Git provided a subcommand 'cherry-pick'
Syntax:
git cherry-pick commit-id
When can I use the Cherry-pick?
- To make it correct when a commit is made in a different branch accidentally.
- Preferable way is traditional merges only
- To apply the changes in an existing commit urgently
- Duplicate commits
- Bug fixing in production
|
Cherry-pick example branches |
Example of git cherry-pick
mkdir check-pick; cd cherry-pick; git init .
alias graph="git log --oneline --graph --all" #if it is added to .bashrc you can skip this
touch A; git add .; git commit -m "A"
touch B; git add .; git commit -m "B"
# branch out feature
git checkout -b feature'
touch C; git add . ; git commit -m "C"
touch D; git add .; git commit -m "D"
touch F; git add .; git commit -m "F"
# check git work-tree
graph
git checkout master
touch E; git add .; git commit -m "E"
graph
git cherry-pick 6353156 [select D commit-code]
graph
Execution output as follows:
|
git cherry-pick example |
Here we conclude now with branches and their corresponding merging is easy. we know how to merge the code from another branch. Also know how to
be specific.
Have fun with Git/SCM tools!!
Please write your comment while working with this merge and rebase post along with cherry-pick concept and their corresponding execution examples.