GIT WORKFLOW - BRANCHES, MERGING, FORKS AND PULL REQUESTS
GIT WORKFLOW - BRANCHES, MERGING, FORKS AND PULL REQUESTS
GIT WORKFLOW - BRANCHES, MERGING, FORKS AND PULL REQUESTS
Hello there to you developers, hope you have been doing good. In the last article we discussed the basic Git workflow which includes the commands and processes to add, commit and push files to Github and you can check it out here. The previous articles were designed to prepare you for basic Git operations when working on personal projects or for an organization at large.
Today we will be discussing some intermediate git concepts, they are considered intermediate because you will start to encounter their importance when working on more complex projects as an intermediate developer with more experience. Do not let this scare you though, they are quite easy to understand and practice. Let us take a look at the topics for today;
TOPICS
- What is a branch?
- Importance of a branch
- How to create a branch
- How to switch between branches
- How to merge branches to the original
- How to fork a repository and why it is important
- What is a pull request
- How to create a pull request after forking a repository
WHAT IS A BRANCH?
A branch is simply a version of your repository that you create in order to develop other features independently without disrupting the structure of the repository. We use branches to isolate development work without affecting other branches in the repository, each repository has one default branch and can have other multiple branches. You can merge a branch into the main one or another branch by creating a pull request.
When a repository contains multiple branches it simply means there are multiple versions of the repository currently on Github.
As a developer there are two scenarios where a branch is used;
- When a group is working on one project and does not want to disrupt the repository with excess commits.
- When you are working on your personal project and want to add multiple features without causing merge conflicts, a merge conflict is a difference in code which the computer identifes conflicts can cause errors or bring about new features by another developer.
Branches are important because they enable you to develop features independently without disrupting the main branch, they enable multiple developers to collaborate on open source projects seamlessly and easily.
As a result, you really need to learn to create branches, luckily it's quite simple so follow the lines of code. Open your Git Bash and use the command
git checkout -b name_of_branch
It is very simple, with this you can work on the current branch seamlessly. Let's say you want to switch between branches to the old branch to confirm something really important in your code. All you have to do “git checkout name_of_branch”
git checkout name_of_branch
HOW TO MERGE BRANCHES
Merging is the opposite of creating branches or forks to a repository, it means joining back two or more branches together for production or formal purposes.
When working on a personal project it's easy because all you have to do is to merge with the origin branch and everything will be normal, however when working with friends or colleagues on a group project you will be required to create a pull request to notify them before a merge is carried out.
To merge a repository all you have to to is to go into to secondary branch you created using the checkout command as shown below;
HOW TO FORK A REPOSITORY AND ITS IMPORTANCE
As earlier mentioned, you will be required to work with other developers a lot in your career. This also means you will learn to collaborate effectively with all of them.
When working on a repository with a group or with any other private repository that you do not have access to, forks help you collaborate easily.
When you fork a repository it creates a branch version on your github account which you can then clone to your local machine and build features before creating a pull request for a merge. This is a concept just like branching and merging repositories, let us see how to fork a repository.
I know you were expecting something elaborate but it is simple, all you have to do is click the fork button at the top right corner of the repository you want to and it will be forked to your repository. Check out this article for more information on forks .
WHAT IS A PULL REQUEST?
A pull request is a message to notify other collaborators about changes you have made to a branch in a repository on Github. Once a pull request is opened, you can discuss and review the potential changes with collaborators and add follow-up commits before your changes are merged into the original branch.
Just like forking repositories, creating pull requests isn't difficult at all and requires no coding, all you have to do is click the “pull request” button at the top left corner on the repository and the author will accept it. All these features were embedded into Github in order to facilitate building together in teams with as little roadblocks as possible.
In the near future, practical assignments and classes will be created but in the meantime let us look at a typical Git workflow using the commands and concepts we learnt today.
EXAMPLE
Imagine you are working on a project for a company to add an edit button feature, here is how branches and forks would help you.
- First of all you locate the repository on Github and confirm it is the correct one, this is very important in official organizations.
- The next step is to fork the repository, this will create a branch on your Github account for you.
- Then you clone the repository to your local machine and work on it.
git clone https://github.com/vector-10/alx-zero_day.git
- Once you are done push your work to the branch on your account on Github.
git push
- Create a pull request by clicking the button at the top left corner and it should notify your colleagues of your changes.
- The author or senior worker approves your pull request and you are good to go.
Congratulations you have successfully added a new feature using the Git workflow that you have been taught. This brings us to the end of this article, practical classes or merges and pull requests will soon commence so as to master commands and read head for efficiency.
Meanwhile join our new telegram coding group here for more resources. “https://t.me/+r9yr4nxMvpoxMDBk”
What's Your Reaction?