Dear Readers,
In this article,we will see Branching and Merging in Git.
Implementation Steps
- Create a new branch “ktexperts-New-Branch” from branch “master”.
- Verify files/commits in branch “ktexperts-New-Branch”.
- Make a new file in branch “master”.
- Adding file to Staging Area.
- Commit the file into Local Repository.
- Verify files/commits in branch “Ktexperts-New-Branch”.
- Make a new file in branch “Ktexperts-New-Branch”.
- Adding file to Staging Area.
- Commit the file into Local Repository.
- 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)
- Make a new file in branch “master”.
- Verify file in branch “Ktexperts-New-Branch”.
- Go to branch “master”.
- Adding file to Staging Area.
- Commit the file into Local Repository.
- Verify the file in branch “”Ktexperts-New-Branch”.
- 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”
1 2 |
[root@ip-172-31-42-20 mumbaigit]# git 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.
1 2 3 4 5 6 7 8 9 10 11 12 |
[root@ip-172-31-42-20 mumbaigit]# git log --oneline 055e453 (HEAD -> master) commited by using * 3228067 3files commited d3f38fc .gitignore 98a8f74 (origin/master) Delete myfile1 3f8608d ramesh 48435c6 6th commit from mumbai 098682e 5th commit from london c3406c4 3rd commit from mumbai 731a447 1st commit from london f7e5d55 2nd commit from mumbai cbb38f2 1st commit from mumbai |
Create a new branch “ktexperts-New-Branch” from branch “master”
Create a new branch “ktexperts-New-Branch”
1 |
[root@ip-172-31-42-20 mumbaigit]# git branch Ktexperts-New-Branch |
To see the current branch
we can see 2 branches.
Currently,you are being presented in default branch “master”
1 2 3 |
[root@ip-172-31-42-20 mumbaigit]# git branch Ktexperts-New-Branch * master |
Verify files/commits in branch “ktexperts-New-Branch”
Switch to branch “ktexperts-New-Branch”
1 2 |
[root@ip-172-31-42-20 mumbaigit]# git checkout Ktexperts-New-Branch Switched to branch 'Ktexperts-New-Branch' |
To see the current branch
Currently,you are being presented in “Ktexperts-New-Branch”
1 2 3 |
[root@ip-172-31-42-20 mumbaigit]# git branch * Ktexperts-New-Branch master |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
[root@ip-172-31-42-20 mumbaigit]# git log --oneline 055e453 (HEAD -> Ktexperts-New-Branch, master) commited by using * 3228067 3files commited d3f38fc .gitignore 98a8f74 (origin/master) Delete myfile1 3f8608d ramesh 48435c6 6th commit from mumbai 098682e 5th commit from london c3406c4 3rd commit from mumbai 731a447 1st commit from london f7e5d55 2nd commit from mumbai cbb38f2 1st commit from mumbai |
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”
1 2 3 |
[root@ip-172-31-42-20 mumbaigit]# git branch * Ktexperts-New-Branch master |
Switch to branch “master”
1 2 |
[root@ip-172-31-42-20 mumbaigit]# git checkout master Switched to branch 'master' |
To see the current branch
Currently,you are being presented in “master”
1 2 3 |
[root@ip-172-31-42-20 mumbaigit]# git branch Ktexperts-New-Branch * master |
Make a new file using cat command
1 2 3 4 5 6 7 8 |
[root@ip-172-31-42-20 mumbaigit]# cat > ktexperts Ktexperts is a knowledge sharing platform!! [root@ip-172-31-42-20 mumbaigit]# cat ktexperts Ktexperts is a knowledge sharing platform!! [root@ip-172-31-42-20 mumbaigit]# ls devops.class file1.txt file2.txt file3.txt ktexperts.java myfile file1.java file2.java file3.java ktexperts ktexperts.php file1.php file2.php file3.php ktexperts.class ktexperts.txt |
To see the status of git
1 2 3 4 5 6 7 8 |
[root@ip-172-31-42-20 mumbaigit]# git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) ktexperts nothing added to commit but untracked files present (use "git add" to track) |
Adding file to Staging Area
Add
1 |
[root@ip-172-31-42-20 mumbaigit]# git add . |
To see the status of git
1 2 3 4 5 6 |
[root@ip-172-31-42-20 mumbaigit]# git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: ktexperts |
Commit the file into Local Repository
Commit
1 2 3 4 |
[root@ip-172-31-42-20 mumbaigit]# git commit -m "ktexperts has been commited" [master 1ed4eda] ktexperts has been commited 1 file changed, 1 insertion(+) create mode 100644 ktexperts |
To see the list of commits in a summarized manner
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[root@ip-172-31-42-20 mumbaigit]# git log --oneline 1ed4eda (HEAD -> master) ktexperts has been commited 055e453 (Ktexperts-New-Branch) commited by using * 3228067 3files commited d3f38fc .gitignore 98a8f74 (origin/master) Delete myfile1 3f8608d ramesh 48435c6 6th commit from mumbai 098682e 5th commit from london c3406c4 3rd commit from mumbai 731a447 1st commit from london f7e5d55 2nd commit from mumbai cbb38f2 1st commit from mumbai |
Verify files/commits in branch “Ktexperts-New-Branch”
Switch to Branch “Ktexperts-New-Branch”
1 2 |
[root@ip-172-31-42-20 mumbaigit]# git checkout Ktexperts-New-Branch Switched to branch 'Ktexperts-New-Branch' |
To see the current branch
Currently,you are being presented in “Ktexperts-New-Branch”
1 2 3 |
[root@ip-172-31-42-20 mumbaigit]# git branch * Ktexperts-New-Branch master |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
[root@ip-172-31-42-20 mumbaigit]# git log --oneline 055e453 (HEAD -> Ktexperts-New-Branch) commited by using * 3228067 3files commited d3f38fc .gitignore 98a8f74 (origin/master) Delete myfile1 3f8608d ramesh 48435c6 6th commit from mumbai 098682e 5th commit from london c3406c4 3rd commit from mumbai 731a447 1st commit from london f7e5d55 2nd commit from mumbai cbb38f2 1st commit from mumbai |
To see the list of files
1 2 3 4 |
[root@ip-172-31-42-20 mumbaigit]# ls devops.class file1.txt file2.txt file3.txt ktexperts.php file1.java file2.java file3.java ktexperts.class ktexperts.txt file1.php file2.php file3.php ktexperts.java myfile |
Make a new file in branch “Ktexperts-New-Branch”
To see the current branch
Currently,you are being presented in “master”
1 2 3 |
[root@ip-172-31-42-20 mumbaigit]# git branch Ktexperts-New-Branch * master |
Switch to Branch “master”
1 2 |
[root@ip-172-31-42-20 mumbaigit]# git checkout Ktexperts-New-Branch Switched to branch 'Ktexperts-New-Branch |
To see the current branch
Currently,you are being presented in “Ktexperts-New-Branch”
1 2 3 |
'[root@ip-172-31-42-20 mumbaigit]# git branch * Ktexperts-New-Branch master |
Make a new file using cat command
1 2 3 4 5 6 7 8 |
[root@ip-172-31-42-20 mumbaigit]# cat > ktexperts-DevOps DevOps is a updated technology!!! [root@ip-172-31-42-20 mumbaigit]# ls devops.class file1.txt file2.txt file3.txt ktexperts.java myfile file1.java file2.java file3.java ktexperts.class ktexperts.php file1.php file2.php file3.php ktexperts-DevOps ktexperts.txt To see the status of git |
To see the status of git
1 2 3 4 5 6 7 8 |
[root@ip-172-31-42-20 mumbaigit]# git status On branch Ktexperts-New-Branch Untracked files: (use "git add <file>..." to include in what will be committed) ktexperts-DevOps nothing added to commit but untracked files present (use "git add" to track) |
Adding file to Staging Area
Add
1 2 3 4 5 6 7 8 9 |
[root@ip-172-31-42-20 mumbaigit]# git add . To see the status of git [root@ip-172-31-42-20 mumbaigit]# git status On branch Ktexperts-New-Branch Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: ktexperts-DevOps |
Commit the file into Local Repository
Commit
1 2 3 4 |
[root@ip-172-31-42-20 mumbaigit]# git commit -m "Ktexperts-DevOps has been commited" [Ktexperts-New-Branch 9dbaaf8] Ktexperts-DevOps has been commited 1 file changed, 1 insertion(+) create mode 100644 ktexperts-DevOps |
To see the list of commits in a summarized manner
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@ip-172-31-42-20 mumbaigit]# git log --oneline 9dbaaf8 (HEAD -> Ktexperts-New-Branch) Ktexperts-DevOps has been commited 055e453 commited by using * 3228067 3files commited d3f38fc .gitignore 98a8f74 (origin/master) Delete myfile1 3f8608d ramesh 48435c6 6th commit from mumbai 098682e 5th commit from london c3406c4 3rd commit from mumbai 731a447 1st commit from london f7e5d55 2nd commit from mumbai cbb38f2 1st commit from mumbai [root@ip-172-31-42-20 mumbaigit]# ls devops.class file1.txt file2.txt file3.txt ktexperts.java myfile file1.java file2.java file3.java ktexperts.class ktexperts.php file1.php file2.php file3.php ktexperts-DevOps ktexperts.txt |
Verify file/commit in branch “master”
Switch to branch “master”
1 2 |
[root@ip-172-31-42-20 mumbaigit]# git checkout master Switched to branch 'master' |
To see the current branch
Currently,you are being presented in “master”
1 2 3 |
[root@ip-172-31-42-20 mumbaigit]# git branch Ktexperts-New-Branch * 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”.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[root@ip-172-31-42-20 mumbaigit]# git log --oneline 1ed4eda (HEAD -> master) ktexperts has been commited 055e453 commited by using * 3228067 3files commited d3f38fc .gitignore 98a8f74 (origin/master) Delete myfile1 3f8608d ramesh 48435c6 6th commit from mumbai 098682e 5th commit from london c3406c4 3rd commit from mumbai 731a447 1st commit from london f7e5d55 2nd commit from mumbai cbb38f2 1st commit from mumbai |
To see the list of files
1 2 3 4 |
[root@ip-172-31-42-20 mumbaigit]# ls devops.class file1.txt file2.txt file3.txt ktexperts.java myfile file1.java file2.java file3.java ktexperts ktexperts.php file1.php file2.php file3.php ktexperts.class ktexperts.txt |
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”
1 2 3 4 5 6 7 8 |
[root@ip-172-31-42-20 mumbaigit]# cat > test This is test file [root@ip-172-31-42-20 mumbaigit]# cat test This is test file [root@ip-172-31-42-20 mumbaigit]# ls devops.class file1.txt file2.txt file3.txt ktexperts.java myfile file1.java file2.java file3.java ktexperts ktexperts.php test file1.php file2.php file3.php ktexperts.class ktexperts.txt |
Verify file in branch “Ktexperts-New-Branch”
Switch to branch “Ktexperts-New-Branch”
1 2 |
[root@ip-172-31-42-20 mumbaigit]# git checkout Ktexperts-New-Branch Switched to branch 'Ktexperts-New-Branch' |
To see the current branch
Currently,you are being presented in “Ktexperts-New-Branch”
1 2 3 |
'[root@ip-172-31-42-20 mumbaigit]# git branch * Ktexperts-New-Branch master |
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.
1 2 3 4 |
[root@ip-172-31-42-20 mumbaigit]# ls devops.class file1.txt file2.txt file3.txt ktexperts.java myfile file1.java file2.java file3.java ktexperts.class ktexperts.php test file1.php file2.php file3.php ktexperts-DevOps ktexperts.txt |
Go to branch “master”
Switch to branch “master”
1 2 |
[root@ip-172-31-42-20 mumbaigit]# git checkout master Switched to branch 'master' |
To see the current branch
Currently,you are being presented in “master”
1 2 3 |
[root@ip-172-31-42-20 mumbaigit]# git branch Ktexperts-New-Branch * master |
To see the status of git
[root@ip-172-31-42-20 mumbaigit]# git status
1 2 3 4 5 6 7 |
On branch Ktexperts-New-Branch Untracked files: (use "git add <file>..." to include in what will be committed) test nothing added to commit but untracked files present (use "git add" to track) |
Adding file to Staging Area
Add
1 2 3 4 5 6 7 8 9 |
[root@ip-172-31-42-20 mumbaigit]# git add . To see the status of git [root@ip-172-31-42-20 mumbaigit]# git status On branch Ktexperts-New-Branch Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: test |
Commit the file into Local Repository
Commit
1 2 3 4 |
[root@ip-172-31-42-20 mumbaigit]# git commit -m "test" [Ktexperts-New-Branch f67ffbc] test 1 file changed, 1 insertion(+) create mode 100644 test |
To see the list of commits in a summarized manner
The file “test” has been commited successfully.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[root@ip-172-31-42-20 mumbaigit]# git log --oneline f67ffbc (HEAD -> Ktexperts-New-Branch) test 9dbaaf8 Ktexperts-DevOps has been commited 055e453 commited by using * 3228067 3files commited d3f38fc .gitignore 98a8f74 (origin/master) Delete myfile1 3f8608d ramesh 48435c6 6th commit from mumbai 098682e 5th commit from london c3406c4 3rd commit from mumbai 731a447 1st commit from london f7e5d55 2nd commit from mumbai cbb38f2 1st commit from mumbai |
Verify the file in branch “”Ktexperts-New-Branch”
Switch to branch “Ktexperts-New-Branch”
1 2 |
[root@ip-172-31-42-20 mumbaigit]# git checkout Ktexperts-New-Branch Switched to branch 'Ktexperts-New-Branch' |
To see the current branch
Currently,you are being presented in “Ktexperts-New-Branch”
1 2 3 |
'[root@ip-172-31-42-20 mumbaigit]# git branch * Ktexperts-New-Branch master |
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”.
1 2 3 4 |
[root@ip-172-31-42-20 mumbaigit]# ls devops.class file1.txt file2.txt file3.txt ktexperts.java myfile file1.java file2.java file3.java ktexperts ktexperts.php file1.php file2.php file3.php ktexperts.class ktexperts.txt |
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”.
1 2 3 4 5 6 |
[root@ip-172-31-42-20 mumbaigit]# git merge Ktexperts-New-Branch Updating 2a87d1c..c911ee4 Fast-forward test| 1 + 1 file changed, 1 insertion(+) create mode 100644 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[root@ip-172-31-42-20 mumbaigit]# git log --oneline 2a83536 (HEAD -> master Merge branch 'Ktexperts-New-Branch' into master f67ffbc test 9dbaaf8 Ktexperts-DevOps has been commited 1ed4eda (master) ktexperts has been commited 055e453 commited by using * 3228067 3files commited d3f38fc .gitignore 98a8f74 (origin/master) Delete myfile1 3f8608d ramesh 48435c6 6th commit from mumbai 098682e 5th commit from london c3406c4 3rd commit from mumbai 731a447 1st commit from london f7e5d55 2nd commit from mumbai cbb38f2 1st commit from mumbai |
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/
santhi
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.