and it’s about time we explain what branches are. Branches are one of the most powerful features in Git.
There are many use cases for branches, for example, if you’re working on a software with different releases, development of the different versions shouldn’t be going on the same pipeline. Branching is basically diverging the flow of changes in a certain direction.
Another more general example is a production, testing, and development branches, production would only contain stable releases, certain development commits would have to diverge to testing before they’re merged with production and so forth. It’s a much-needed practice in the software world.
The reason why Git is so good at branching is that it’s using snapshots and calculating differences, the process of creating a branch then becomes much simpler and faster than it is in other systems like SVN where such operation is a headache.
So let’s start a new branch on our project, we can simply do so by using the git branch <branch name> command which will go ahead and create a new branch with the name we choose.
We can then enter that branch by using the following command:
git checkout "branch name"
I’ll go ahead and edit branch ‘testing’ with a change in the text file as follows, and I’ll commit this change. You can run
to commit all the unstaged files and commit them with the same command.
Here you can find some best practices for branching: Git Branching Model
Pull requests are used to discuss some piece of code with the other collaborators before finally merging the code. They can be useful if you are contributing to an open source project or have some people working remotely. You can specify which branch you’d like to merge your changes into when you create your pull request.
To create a pull request, first, you need to create a new branch from the branch you wish to merge into. Next, you need to commit some changes and push this branch to Github.
You will find the “New pull request” button on the repository page. On clicking it, Github will show you the following prompt:
Here, you can select the branches and leave a comment (optional). After a pull request has been created, Github gives you (the project owner) the option to merge or close the pull request.
If an automatic merge cannot be performed online, you will need to take a pull and merge them locally. Github has a “command line instructions” button to help you out.
Version control is a requirement of most software engineering and a daily practice for developers. It’s a tool that you’ll definitely need and the only way to get good with it is using it.
The official Pro Git book is free and it’s resourceful so check it out. There’s also a Cheat-Sheet found on the official GIT website that lists most of the git commands that you’ll be using, so it’s a good reference especially when your beginning your journey with Git.
Ready for in-depth Git tutorial? Here are the programming community best Git tutorials and courses: https://hackr.io/tutorials/learn-git?ref=blog