Branching and Merging in Git

Share via:

Dear Readers,

In this article,we will see Branching and Merging in Git.

Implementation Steps

  1. Create a new branch “ktexperts-New-Branch” from branch “master”.
  2. Verify files/commits in branch “ktexperts-New-Branch”.
  3. Make a new file in branch “master”.
  4. Adding file to Staging Area.
  5. Commit the file into Local Repository.
  6. Verify files/commits in branch “Ktexperts-New-Branch”.
  7. Make a new file in branch “Ktexperts-New-Branch”.
  8. Adding file to Staging Area.
  9. Commit the file into Local Repository.
  10. Verify file/commit in branch “master”.

Make a file in branch “master” and verify file in workspace in both branches(before adding to staging area & after commit to central repository)

  1. Make a new file in branch “master”.
  2. Verify file in branch “Ktexperts-New-Branch”.
  3. Go to branch “master”.
  4. Adding file to Staging Area.
  5. Commit the file into Local Repository.
  6. Verify the file in branch “”Ktexperts-New-Branch”.
  7. Merging the commits from branch “Ktexperts-New-Branch” to “master”

What is Branch?

Inside the local repository there is one branch called master.
Master is the default branch.
By default commited files will there inside master branch in the form of commits.

To see the current branch
We can see here only one branch called master.
Currently you are being presented in master branch.
* represents the your current branch.
work space is pointing to branch “master”

Note

Currently i am in master branch.
Being presented in master we are going to create new branch.
The first time whatever commits are available inside master branch by default those will be copied to new branch.

To see the list of commits in a summarized manner

we can see all my commits which are there in master branch.
once create new branch all commits will copy to new branch.

Create a new branch “ktexperts-New-Branch” from branch “master”

Create a new branch “ktexperts-New-Branch”

To see the current branch
we can see 2 branches.
Currently,you are being presented in default branch “master”

Verify files/commits in branch “ktexperts-New-Branch”

Switch to branch “ktexperts-New-Branch”

To see the current branch
Currently,you are being presented in  “Ktexperts-New-Branch”

To see the list of commits in a summarized manner
We can see all the commits ,those copied from “master” branch to “Ktexperts-New-Branch”
This process will happen only first time.

If you create,add and commit any file in master that file belongs to “master” branch only.

We can’t see in the  “Ktexperts-New-Branch” branch and vice versa.

Make a new file in branch “master”
To see the current branch

Currently,you are being presented in  “Ktexperts-New-Branch”

Switch to branch “master”

To see the current branch

Currently,you are being presented in  “master”

Make a new file using cat command

To see the status of git

Adding file to Staging Area
Add

To see the status of git

Commit the file into Local Repository

Commit

To see the list of commits in a summarized manner

Verify files/commits in branch “Ktexperts-New-Branch”

Switch to Branch “Ktexperts-New-Branch”

To see the current branch

Currently,you are being presented in  “Ktexperts-New-Branch”

To see the list of commits in a summarized manner

we are unable to see the ktexperts file and commit.

we have created file,add and commit in branch “master”.
That file is belongs to master branch not Ktexperts-New-Branch.
Currently,you are being presented in Ktexperts-New-Branch that’s why it’s not showing the file/commit.

To see the list of files

Make a new file in branch “Ktexperts-New-Branch”

To see the current branch

Currently,you are being presented in  “master”

Switch to Branch “master”

To see the current branch

Currently,you are being presented in  “Ktexperts-New-Branch”

Make a new file using cat command

To see the status of git

Adding file to Staging Area

Add

Commit the file into Local Repository

Commit

To see the list of commits in a summarized manner

Verify file/commit in branch “master”
Switch to branch “master”

To see the current branch

Currently,you are being presented in  “master”

To see the list of commits in a summarized manner

we are unable to see the Ktexoerts-DevOps file.
That file is belongs to Ktexperts-New-Branch branch not master branch.
Currently,you are being presented in master branch that’s why it’s not showing the file “ktexperts”.

To see the list of files

Make a file in branch “master” and verify file in workspace in both branches(before adding to staging area & after commit to central repository)

Make a new file in branch “master”

We can see the file “test”

Verify file in branch “Ktexperts-New-Branch”
Switch to branch “Ktexperts-New-Branch”

To see the current branch

Currently,you are being presented in  “Ktexperts-New-Branch”

To see the list of files

In the branch “Ktexperts-New-Branch” we are able to see test file.

Files created in the work space will be visible in any of the branch workspace until you commit.

we didn’t adding to staging area,commit.

Once you commit then the file belongs to that particular branch.

Go to branch “master”

Switch to branch “master”

To see the current branch

Currently,you are being presented in  “master”

To see the status of git
[root@ip-172-31-42-20 mumbaigit]# git status

Adding file to Staging Area

Add

Commit the file into Local Repository

Commit

To see the list of commits in a summarized manner

The file “test” has been commited successfully.

Verify the file in branch “”Ktexperts-New-Branch”

Switch to branch “Ktexperts-New-Branch”

To see the current branch

Currently,you are being presented in  “Ktexperts-New-Branch”

To see the list of files
we are unable to see the test file.

we have committed the file in branch “master”.

Files created in the work space will be visible in any of the branch workspace until you commit.
That file is belongs to branch “master” not “Ktexperts-New-Branch”
Currently,you are being presented in master branch that’s why it’s not showing the file “test”.

Merging

Merging is a common practice for developers using version control systems.

Merging the commits from branch “Ktexperts-New-Branch” to “master”

You should be presented in branch “master”.

To see the list of commits in a summarized manner

we have merged the branch “Ktexperts-New-Branch” to branch “master”.
The commits has been copied from Ktexperts-New-Branch to master.

Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates

KTEXPERTS is always active on below social media platforms.

Facebook : https://www.facebook.com/ktexperts/
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform

Follow Me
Ramesh’s Linkedin : https://www.linkedin.com/in/ramesh-atchala/

Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

One thought on “Branching and Merging in Git

  1. Its a very nice article and easily understandable. Very good explanation.
    Thanks Ramesh for detailed explanation. It will really helpful to who’s learnign DevOps. Appreciated for the time you spent to prepare to make an article.

Add Comment