Skip to main content

Command Palette

Search for a command to run...

Learn about Branching,Pull Requests,Fork & Rebase!

Git Branches and the Importance of Pull Requests in Collaborative Development

Updated
7 min read
Learn about Branching,Pull Requests,Fork & Rebase!
K

I Like to Learn and share new things to you guys! I will update my Tech Stacks over Time

Git Branches:

Git branches are essential tools in software development that allow multiple developers to work on a project simultaneously. A branch is a separate line of development that diverges from the main codebase or master branch.

Why do we need branches?

  • Consider a scenario where two developers, Shanks and Luffy, are working on the same project for a startup.

  • The master branch contains the initial codebase or starter file.

  • Shanks creates a new branch called "navbar" to work on the navigation bar component, while Luffy creates a new branch called "footer" to work on the footer component.

  • By using separate branches, they can work independently without interfering with each other's work.

Once they have completed their work, they can merge their changes back into the master branch, ensuring that the final product is a combination of both their contributions.

Branches also facilitate collaboration among team members by enabling them to work on different features simultaneously, reducing development time and allowing developers to experiment with new ideas without affecting the main codebase.


see it in Action:

In the main branch, it looks like this

you may think why it looks like this right?


  • To start working on a new feature, such as the Navbar component, it is recommended to create a new branch from the main branch to avoid making changes directly to the main branch.

  • This can be done using the "git branch" command followed by the name of the new branch. For instance, Shanks can create a new branch called "navbar" using the command "git branch navbar".

  • Once the new branch is created, Shanks needs to switch to the newly created branch using the "git checkout" command. This will ensure that he is working on the Navbar branch and not the main branch.

  • The command "git checkout navbar" will switch the current working branch to the Navbar branch, and the "*" symbol will now point towards the Navbar branch.

While working on the Navbar branch, Shanks can make changes


First, I commit some changes in the main branch itself like (Starter File) after that

  • To start working on a new feature, such as the Navbar component, it is recommended to create a new branch from the main branch to avoid making changes directly to the main branch.

  • This can be done using the "git branch" command followed by the name of the new branch. For instance, Shanks can create a new branch called "navbar" using the command "git branch navbar".

  • Once the new branch is created, Shanks needs to switch to the newly created branch using the "git checkout" command. This will ensure that he is working on the Navbar branch and not the main branch.

  • The command "git checkout navbar" will switch the current working branch to the Navbar branch, and the "*" symbol will now point towards the Navbar branch.

    While working on the Navbar branch, Shanks can make changes

  • Shanks and Luffy began working on the project. As part of his contribution, Luffy created a new branch called "Footer" by branching off from the main branch.

  • The Footer branch contained the starter files that were present in the main branch.

  • Luffy started working on the Footer branch and made changes to the code. He then committed these changes to his branch. Consequently, two branches, Navbar and Footer, were created in addition to the main branch.

  • The ultimate goal was to combine the features of both branches to create a functional website. However, before doing so, the team needed to familiarize themselves with the appropriate command to merge the branches.

Git merge:

  • Git merge is a powerful command that allows you to integrate changes from one branch into another.

  • You can use this command to bring all the changes from a specific branch into another one.

  • To execute this command, you need to specify which branch you want to merge.

  • In order to merge the Navbar branch into the Main branch, you need to first switch to the Main branch by using the command "git checkout main".

  • Once you are in the Main branch, you can then use the command "git merge navbar" to merge the Navbar branch into the Main branch.

  • It is important to note that when merging branches, you should consider the order in which you merge them. In this case, since Navbar contains the main navigation features of the website, it would be best to merge it into the Main branch first before merging any other branches.

  • This will ensure that the main navigation is properly integrated into the final version of the website.

  • After successfully merging the Navbar branch into the Main branch, the next step is to merge the Footer branch.

  • To do this, you need to switch back to the Main branch using the command "git checkout main". Once you are in the Main branch, you can then use the command "git merge footer" to merge the Footer branch into the Main branch.

  • Assuming that there are no conflicts between the branches, the Footer branch will merge seamlessly into the Main branch, resulting in a complete version of the website that includes both the main navigation and footer features.


Why do we need Pull Request (PR)?

In branches, we are working on one single project in that they have permissions to the particular repo they are working right?

Let's have some assumptions let's visualize this

  • You are maintaining your website

  • I saw your website online and I feel If I add some changes to the website It will become better in visual

  • I'll somehow find your website repo on GitHub. For example, we are taking the Kubernetes website repo.

  • in this, there is a button named Fork just click that what it will do is

  • It creates a copy of that entire Kubernetes website repository to your own GitHub account

Here we are talking about PR but why do we need Fork to let's answer this

  • we can permit others to edit our repo by making some extra branches after we can combine them (we can send invites to others 'Hey there can you make some changes to our website only if we know about the email id registered with the GitHub account)

  • There is another way to do that the Fork way.

  • In this way, we don't need to know any of the e-mail addresses

we can clone this Kubernetes website to our Computer

  • Here I will add some Improvements or Delete some guidelines in the Readme. md file

  • After that, I will commit my changes in a new branch named Feature/readme-enhancement like this

  • I will check the original Kubernetes website or Repo My changes is not there yet because we don't have permission to change anything

  • We have to tell the Project maintainers (In this case Kubernetes original one) How to tell this we have to make a PR first from our Kubernetes repo(The changes in Readme!)

In your version of Kubernetes, you can see Compare & Pull Request just click that

  • when you scroll through this you can see your commits, changes to the files

  • In this way, they can easily check whether It can be added to their original Kubernetes website or not!

    once they will feel happy! about the changes I made They can merge the changes If not they will Reject it!

Rebase:

Let's understand how Rebase works

We can create a branch and do some 10 commits after that we want to merge that branch to some another branch or we need those 10 commits to make it one single commit in this way we can achieve the results

Here I make 5 commits to the test branch that I created In the Git log

You need to pick the hash Id that you want to merge the commits into one commit

  • once you save the commits it will be updated

  • After that, you can check with the git log if it is updated or not

  • can you see the 2,3,4,5.txt files are in a single commit we can pick the main commit that you want to merge the other commits

  • others are you can choose squash option.

Conclusion:

Git branches allow developers to work on different features of a project simultaneously without interfering with each other's work. Branches can be merged using the "git merge" command to create a final product. Pull requests are used to request permission to merge changes from one branch to another, allowing for collaboration among multiple developers. The forking method can also be used to make changes to a repository without having to ask for permission directly.