SAGARFIVE

1. Introduction

2. Git Basics

3.  Git Terminology

4.  Git Branching

5.  Git - Github

6.  Git Add (Git staging)

7.  Git Commit

8.  Github Advanced

9.  Git Remote

4.5 Git branch Merge

(i) Git merge :

  • combining the branch commits on git. This works as a merging child branch → master/parent branch.
  • All commits in the child branch are moved to the parent branch in a linear(time creation basis one after one) way.
  • This Merge helpful in a way that once feature branch is fully ready, then we need that feature on main application code (which is master), merge helps to copy all files and commits from feature branch to master/main(Dev) branch where we have entire code

Steps to merge :

–> First we have to checkout to a branch where we need child/feature branch files and commits

git checkout master

–> Run the merge command

git merge feature-2


Observations  :

–> Git merge will create new commit and add all files from committed branch

–> it wont deletes the files from the old branch

(ii) Git merge single commit :

Task : Copy a commit from feature-2 branch → master branch

  • Copy the commit hash from feature-1
  • Checkout to the master branch
  • Run the merge command
  • git merge commit-hash

(iii) Git rebase merge branch:

  • while developing a project , there are a lot of commits that may be in the main branch or child branch. We don’t know when the branch with commit is developed .
  • If we perform a merge, it adds commits in time linear (time sort) basic,
  • so it will effect in testing, moving to production that particular commit ,
  • so git rebase will merge child branch commits to main branch as latest commits.

** feature-1 is developed long back** after feature-1 , master got added another features** if we directly merge child(feature ) branch to master branch all commits are added in time linear manner** if we need feature-1 commits as latest commits, we need to use git rebase

Steps to merge :

–> Move to child branch or any branch  :

git checkout feature-1


–> Rebase the master :

git rebase master

→ command grab the master commit history , it will help full add child commits as latest , rebase won’t add commits to master branch , we need to run merge commands once again to add.

→ copies files from master and commits

—> Merge child with master :

git checkout master
git merge feature-1

Observations :
We can see all the commits from master branch are moved to last and all commits from feature-1 came top/ as latest

Actual time linear commit history :