DEV Community

Cover image for GIT Branching (I): Explained Like I'm Five
Joel Olawanle
Joel Olawanle

Posted on • Edited on

GIT Branching (I): Explained Like I'm Five

Branching is a feature available in most modern version control systems. In Git, branches are a part of your everyday development process. When you want to add a new feature or fix a bug—no matter how big or how small—you create a new branch to hold your changes. This would give you an opportunity to compare to see if there is any conflict or issue that may arise suppose you merge. You can think of branching as a way to request a brand new working directory, staging area, and project history.

In clearer terms, when you make changes to your repository, it's best practice to push to a separate branch so you can compare changes before creating a pull request and finally merging a pull request. This is most important when collaborating with other people.

By default, your repository has one branch named main which is considered to be the definitive branch. We use branches to experiment and make edits before committing them to main. At the end of this section, I will show you how to change the default branch of your repository on GitHub so all your changes get merged there. Most people love making use of master as their default branch.


Pull request

Pull Requests are the heart of collaboration on GitHub. When you open a pull request, you’re proposing your changes and requesting that someone review and pull in your contribution and merge them into their branch. Pull requests shows differences, in the content from both branches. The changes, additions, and subtractions are shown in green and red.

Alt Text

You can either create a branch on GitHub or via git command, I will be explaining both. To create via GitHub, Visit your repository.

Alt Text

or using the git command. From now on I will only be making use of the git commands so you can know the git commands associated with branching.

  • git branch This command is used to know all branches available on a particular repository and also an asterisk(*) will be placed above the branch in which that repository is.

Alt Text

  • git checkout -b This command is used to create a new branch and automatically change your current branch to the new one.
$ git checkout -b name
Enter fullscreen mode Exit fullscreen mode

The name can be anything... You can also use the git branch command to see the changes.

Alt Text

  • git checkout This command is used to switch from one branch to another
$ git checkout branchName
Enter fullscreen mode Exit fullscreen mode

With a perfect understanding of what I just explained, you can now work with Github branching, push your codes to a new branch, create a pull request, check for conflicts, resolve conflicts if any, and finally merge your code together then delete the branch if you wish.


Here are the steps involved if you want to push to a new branch:

  1. Create a new branch and move to that branch using git checkout -b branchName
  2. Check if you are on the new branch using git branch
  3. Stage your files using git add .
  4. Commit new changes using git commit -m "message"
  5. Push to this branch on GitHub using git push origin branchName
  6. Finally, go to Github & create a pull request

When you visit GitHub and refresh you will see something like this indicating changes on a new branch

Alt Text

All you have to do is click the "compare & pull request" button. It will display a section that allows you to create a pull request

Alt Text

In the above image, the area marked with a red box clearly shows you that I am pulling a request for my codes on new-branch to be merged to previous codes on master branch. You can type in a comment so people understand why you are creating a PR, then finally click the button.

Once you have created a pull request. the owner of the repository, will get notified and now be able to review changes made and merge your codes, but if you own the repository or have access you can easily merge yourself.

Alt Text

Once it's merged the label changes from green to purple and you can now delete the branch if you wish to.


Finally, to wrap up this section, I will be showing you how to change your default branch on Github.
Step 1: Click on branches to see all branches available

Alt Text

Step 2: Click on change default branch

Alt Text

Step 3: Select the branch you want to change to and then click the update button.

Alt Text

With that, I believe you mush have gotten an idea of what branching is all about, how to create a pull request, and how to merge your codes.


In Conclusion

I have tried my best to explain a few areas of GitHub and even make use of pictorial examples to explain better. Thanks for reading!


As always, any questions or suggestions, please feel free to leave a response or tweet me 🤭! Be sure to connect with me on socials! 😎

Top comments (2)

Collapse
 
sorcery77 profile image
sorcery77

It would be really great if you'd bothered to address the problem of the remote branch changing while you're making your change. The git rebase and git merge master commands are two ways you can deal with this issue. Otherwise, this doc is only useful for single-user repos.

Collapse
 
olawanle_joel profile image
Joel Olawanle

Oh, thanks for pointing this out, it totally skipped my mind but will see a way of adding it and if my explanation is going to be too long then I would write a separate post for it.

Thanks.