Monday, May 30, 2022

Git Merge Rebase and Cherry Pick - deep dive

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.

No comments:

Categories

Kubernetes (24) Docker (20) git (13) Jenkins (12) AWS (7) Jenkins CI (5) Vagrant (5) K8s (4) VirtualBox (4) CentOS7 (3) docker registry (3) docker-ee (3) ucp (3) Jenkins Automation (2) Jenkins Master Slave (2) Jenkins Project (2) containers (2) create deployment (2) docker EE (2) docker private registry (2) dockers (2) dtr (2) kubeadm (2) kubectl (2) kubelet (2) openssl (2) Alert Manager CLI (1) AlertManager (1) Apache Maven (1) Best DevOps interview questions (1) CentOS (1) Container as a Service (1) DevOps Interview Questions (1) Docker 19 CE on Ubuntu 19.04 (1) Docker Tutorial (1) Docker UCP (1) Docker installation on Ubunutu (1) Docker interview questions (1) Docker on PowerShell (1) Docker on Windows (1) Docker version (1) Docker-ee installation on CentOS (1) DockerHub (1) Features of DTR (1) Fedora (1) Freestyle Project (1) Git Install on CentOS (1) Git Install on Oracle Linux (1) Git Install on RHEL (1) Git Source based installation (1) Git line ending setup (1) Git migration (1) Grafana on Windows (1) Install DTR (1) Install Docker on Windows Server (1) Install Maven on CentOS (1) Issues (1) Jenkins CI server on AWS instance (1) Jenkins First Job (1) Jenkins Installation on CentOS7 (1) Jenkins Master (1) Jenkins automatic build (1) Jenkins installation on Ubuntu 18.04 (1) Jenkins integration with GitHub server (1) Jenkins on AWS Ubuntu (1) Kubernetes Cluster provisioning (1) Kubernetes interview questions (1) Kuberntes Installation (1) Maven (1) Maven installation on Unix (1) Operations interview Questions (1) Oracle Linux (1) Personal access tokens on GitHub (1) Problem in Docker (1) Prometheus (1) Prometheus CLI (1) RHEL (1) SCM (1) SCM Poll (1) SRE interview questions (1) Troubleshooting (1) Uninstall Git (1) Uninstall Git on CentOS7 (1) Universal Control Plane (1) Vagrantfile (1) amtool (1) aws IAM Role (1) aws policy (1) caas (1) chef installation (1) create organization on UCP (1) create team on UCP (1) docker CE (1) docker UCP console (1) docker command line (1) docker commands (1) docker community edition (1) docker container (1) docker editions (1) docker enterprise edition (1) docker enterprise edition deep dive (1) docker for windows (1) docker hub (1) docker installation (1) docker node (1) docker releases (1) docker secure registry (1) docker service (1) docker swarm init (1) docker swarm join (1) docker trusted registry (1) elasticBeanStalk (1) global configurations (1) helm installation issue (1) mvn (1) namespaces (1) promtool (1) service creation (1) slack (1)