Back To Basics: Git Branching
Refresher on Git Branches
Over the past few weeks, I have been volunteering with a non-profit organization to work on educational web application. This is the first time I worked on a project with more than 3 people. There are about 10 ten people collaborating on this project. This has certainly been an intimidating but fun experience so far.
As I am working, I have become more aware of how important collaboration and GitHub flow are in the development of a project. Conflicts in git merges can be a headache. So for this blog, I want to revisit the core concept of branching.
What is Branching?
Almost every single version control system has some form of support of branching. Branching refers to the process of isolating development work from the main line development. You are able to build, experiment, and test new features without affecting the main project.
Default Branch on GitHub
When you create a new repository on GitHub, you are given a default branch that is automatically named main
. This branch is the initial branch that is base branch for pull request and commits.
The default branch should be your main line development with working deployable code. From this branch we can diverge to other branches where we can fix bugs and test new ideas.
Create a New Branch
To create a new branch from an already existing branch, you can use git branch
command:
$ git branch <enter branch name>
This will create a new branch with a name of your choosing. The name of the new branch should be a short description of its purpose to let others know what exactly is being worked on in that branch.
Rename a Branch
To rename a branch, you can use this command:
$ git branch -m <enter new name>
Switching to a Another Branch
To switch to another branch, you can use the git checkout
command:
$ git checkout <enter branch name>
Some Tips
There are plenty of shortcuts out there that can help you bounce around your repository with ease.
You can use this new command to create a new branch and switch to it at the same time:
$ git checkout -b <enter branch name>
You can return to a previous branch you were on by using this command:
$ git checkout -
You can see a list of all your branches with the following command (notice there are no arguments):
$ git branch
You can also see the last commit on each branch:
$ git branch -v
Finally, you can see what branches have been merged into your current branch by using this command:
$ git branch --merged
Deleting a Branch
To delete a branch we use the -d flag in this command:
$ git branch -d <enter branch name>
Merging Branches
When you want to merge your current branch (Branch A) into the main branch (or any other branch), you must first check out into that branch. Then you can run the git merge branch.
$ git checkout main
$ git merge <Branch A>
If you want to stop a merge because of conflicts, you can use:
$ git merge -—abort
Conclusion
These are all the important git commands that you should know when collaborating on a project. This is just a small cheat sheet that you can use for future reference. Hope this has helped you, thanks for reading!