Tutorial 5 : Git merge -part 1
Missed previous tutorial ? — click here.
We will cover following topics in this tutorial :
Part 1 : Adding the file in master branch
Part 2 : Modifying same file and adding it in another branch
Part 3 : Accommodating the changes of branch1 in master branch by merging branch1 in master branch
Part 4: Pushing the master branch to Github repository to see the merged branch
Objective : Suppose there is a sample file in a master branch. And then one decides to make some temporary changes in the same sample file while keeping the modified file in other branch. Later if one decides to accept those changes, one has to merge that branch into master branch so that changes made in that file will be made final in master branch by merging.
We have already created a trial folder and stored sample.html file in it in our first tutorial. I use VS code IDE.
<html><body><h1>This is a first heading.</h1></body></html>
Run all the following commands in command prompt.
Part 1 : Adding the file in master branch
Let’s create an empty Git repository in trial folder.
git init
Output:
Initialized empty Git repository in C:/Users/Gaurav/OneDrive/Desktop/trial/.git/
As we have sample.html file in this folder, let’s check the status :
git status
Output:
On branch masterNo commits yetUntracked files:
(use “git add <file>…” to include in what will be committed)
sample.htmlnothing added to commit but untracked files present (use “git add” to track)
You can see sample.html (in Red color), indicating the file is yet to be added in local repository.
git add .
Here dot in “git add .” means all the files in this folder to add. Alternatively you can also write “git add sample.html”.
Now let’s check the status again to see if the file is added in the local repository.
git status
Output:
On branch masterNo commits yetChanges to be committed:
(use “git rm — cached <file>…” to unstage)
new file: sample.html
You can see sample.html (in Green color), indicating the file is added in local repository but yet to be committed. So let’s commit it.
git commit -m “sample.html file added in master branch”
Output:
[master (root-commit) fb944f5] sample.html file added in master branch
1 file changed, 5 insertions(+)
create mode 100644 sample.html
Now check the branch we are currently in :
git branch
Output:
* master
So there’s only one branch-master branch. Asterisk(*) indicates the branch we are in.
Part 2 : Modifying same file and adding it in another branch
Now make some changes in same sample.html as given below, I use VS code IDE :
<html><body><h1>This is a second heading.</h1></body></html>
Now that we have modified the sample.html file by changing the heading text, let’s check the status.
git status
Output :
On branch master
Changes not staged for commit:
(use “git add <file>…” to update what will be committed)
(use “git restore <file>…” to discard changes in working directory)
modified: sample.htmlno changes added to commit (use “git add” and/or “git commit -a”)
See it clearly says that this file is modified(in red color) in command prompt.
Now let’s create branch say “branch1”, to add this modified file in that branch,
as done below:
git checkout -b branch1
Output :
Switched to a new branch ‘branch1’
Thus we created a new branch and switched into it, meaning we moved from master branch to branch1.
Now we can add this modified file in newly created branch1.
git add .
Status can be checked again by running :
git status
Output:
On branch branch1
Changes to be committed:
(use “git restore — staged <file>…” to unstage)
modified: sample.html
Now we can see modified sample.html (in Green color) in command prompt.
Commit this changes using following command :
git commit -m “modified sample file added in branch1”
Output :
[branch1 d139f12] modified sample file added in branch1
1 file changed, 1 insertion(+), 1 deletion(-)
It clearly says that modified file is added in branch1.
Part 3 : Accommodating the changes of branch1 in master branch by merging branch1 in master branch
Let’s check the branch, we are currently in :
git branch
Output:
* branch1
master
Asterisk(*) before branch1 indicates our current branch.
Now let’s switch to master branch to merge branch1 with master branch.
git checkout master
Output:
Switched to branch ‘master’
Output is quite obvious.
Now we merge the branch1 with master branch (current branch) by running following command in command prompt :
git merge branch1
Output :
Updating b619694..d139f12
Fast-forward
sample.html | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Thus we have merged the branches successfully.
But how do we know if that really worked ?
You know that in modified html file, we simply made the heading as “This is a second heading.” If only there was something to check that. Guess what, there is.
Let’s push this merged master branch in our newly created github repository and check the content of the file.
Part 4: Pushing the master branch to Github repository to see the merged branch
First create a new repository.
Then run following command in command prompt :
git remote add origin https://github.com/<username>/<repo_name>.git
I have created a git repository by name : git-merge1
As we have seen in previous tutorial, we add new remote for remote repository.
git remote add origin https://github.com/gaurav1patil/git-merge1.git
Then we push merged master branch where modified file resides.
git push -u origin <branch_name>
So we run following command in command prompt :
git push -u origin master
Output:
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 569 bytes | 284.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/gaurav1patil/git-merge1.git
* [new branch] master -> master
branch ‘master’ set up to track ‘origin/master’.
Now check your github repository by refreshing the page.
In my case it looked like this :
Modified heading text is clearly visible. Thus we have successfully merged two branches into one.
Congratulations !!!
Now you know how merging works in git if we have same file modified in different branches.
I know what you are thinking !!
What if we have two or more different files in different branches and want to merge all those files in master branch. How do we do that ? That will be unfolded in our next tutorial .