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.
You can either create a branch on GitHub or via git command, I will be explaining both. To create via GitHub, Visit your repository.
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.
- 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
The name can be anything... You can also use the git branch
command to see the changes.
- git checkout This command is used to switch from one branch to another
$ git checkout branchName
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:
- Create a new branch and move to that branch using
git checkout -b branchName
- Check if you are on the new branch using
git branch
- Stage your files using
git add .
- Commit new changes using
git commit -m "message"
- Push to this branch on GitHub using
git push origin branchName
- 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
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
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.
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
Step 2: Click on change default branch
Step 3: Select the branch you want to change to and then click the update button.
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)
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.
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.