Git Branches — Part II
Hey there! Hope you’re doing well.
In the previous part of Git Branches, we saw what a branch is, how to create branches, moving into that branch, and delete a branch. In this blog, we will learn how branches facilitate us in development and how we can merge two or more branches.
Change in files on switching branches
When you create a branch, say B, from an existing branch A, all the current files in A are copied into B. So B already contains some files. So when you change your current branch from A to B, in your file explorer(or using thels
command in Linux), you will see all the same files. But, now after this, if you create any new file in any branch, and change the current branch, accordingly files in your explorer window will change. Let’s see the below example to understand this clearly.
Say, we created a new branch b. And then add some files in the folder without changing the branch.
Now let's change to b, using git checkout b
and create a new file here, say file_in_b.txt, and commit the changes. (Remember how to do that?). Now, switch back to the main, and see the files there. You can notice that file_in_b.txt is not there in the main branch. So it establishes that switching branches changes current files in the folder.
Merging branches
Branches help us carry out development tasks without disrupting the normal /error-free flow of the application. As we discussed the example in the previous blog, where different teams are working on different components of an application. So let's say if the backend team is assigned to do some optimization task to reduce the server load, they can create a new branch, code, experiment, debug, repeat, all in the new branch without needing to commit in the main branch. And when the code is successful, they can merge the branch into the main branch, which will create a new commit in the main. Like I created file_in_b.txt in the new branch and file_in_main.txt in the main branch and already committed the file in the new branch. Let's see how it goes when I merge.
After committing a new file in b, see the log.
I already have a file in the main branch from before. Run the command to see the log of the commits before merging. (Remember the command?)
To merge two branches, you need to checkout to the branch you want another branch to be merged to. So in this case, we switch to the main before we merge.
git checkout maingit merge <branch name>
Now changes from b have been merged to main. As I said before, we can see the file in the folder.
Run the command git log
to see the list of commits.
Notice the HEAD pointer after the recent commit. (Try exploring about it, I will give details about it in a later blog. The arrow before b indicates that the most recent commit is in b).
Git log as a graph
Git provides an option to visualize commits in a better manner to understand which commits come from which branch, merged to whom, etc.
You can use the following command to do this:
git log --all --decorate --graph
Notice the * and dashed lines before the commits. * indicates a commit and lines just connect them.
A typical scenario of multiple merges and graph-view of the logs:
Don't go into the details of what's written in the image above. Just notice the * and dashes.
My name is Harsh Seksaria. I like to write about the topics simplifying them as much as possible for you to understand without unnecessary effort. Still, sometimes something may not be very clear. If you find any mistake, error, or you have a suggestion, just mail me. I’ll try to improve it asap.
My email: harsh20.freelance@gmail.com
My LinkedIn: www.linkedin.com/in/harshseksaria