Git Branches
You might have heard of git branches, but perhaps you aren't sure what they are, what they do or why they are even used?
By default, your git commits are saved to the master
branch. Github recently changed its default branch name to main. So you may find your branch is called this instead. The rest of this post assumes your default branch name is master
. But you can replace it with main
if needed.
When working solo on a project, using branches is a great way to try things out.
Let's say you have a half-finished project on your master
branch and one of your features isn't working and you want to try a different approach. You can take a copy of your project into a new branch called feature/featurename
fiddle around with the feature to try and get it working. But the original version of your site stays untouched in your master branch and you can return to it if you want to.
You can call your branches whatever you want, most projects have a develop
branch where the majority of coding is done. If working on a feature or bugfix, it is best practice is to prefix the branch name with the type of branch before the name, so feature/featurename
or bugfix/bugname
for example.
If you get your feature working, you can then merge your feature/featurename
branch into master
branch, so that it is live on the deployed site. Or if it didn't work you can disregard that branch and just go back to master and continue onward easily.
When you come to work in a team on a project, branches become an easy way for team members to work on their section of the project without negatively affecting any of the code the rest of the team is working on. A team member will work on their own branch, and when they push their code to the GitHub repository for the whole project they do so as that branch. Then they make a pull request to have their branch merged with the master branch. Another team member will check the changes thoroughly before allowing the new code to go live.
- To create a new git branch, first, make sure you have committed all changes in your current branch using the usual git add git commit sequence.
- Then, to create the new branch, in the terminal:
git checkout -b mybranchname
This command will also move you into that branch. The code in the new branch will be identical to the old branch until you make changes.
- To save changes you made in your new branch, use the same git add git commit sequence as you normally do. You can also git push your branch to GitHub.
- To switch from one branch to another (again, make sure you git add and commit before switching, it will save you headaches)
git checkout mybranchname
- To see which branches you have and which one you are on
git branch
The output in the terminal will put a * next to the branch you are on.
To merge one branch with another.
For this example, the branch with our new code that we want to merge is called feature/carousel
and we are merging it into master
- Make sure all your code on the
feature/carousel
branch has been committed git checkout master
will take you back to the master branch and the code as it was beforegit merge feature/carousel
will merge the code from your develop branch into the master branchgit add .
git commit -m "merge feature/carousel branch into master"
git push
to synch up all changes with GitHub
If you committed all changes on master before creating your new branch, you should not hit any merge conflicts. However, if you do hit a conflict:
git status
to get a list of the files that have conflicts in them- Then check those files and look for places marked with
####
or<<<<
. These are the areas that differ between your two branches and git doesn't know which bits you want to keep and which to get rid of. Open these files and correct these areas until the code is how you need it to be. - Save all the files you changed and then git add and commit them.
For more info on using git branches, these resources are excellent:
https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow
https://nvie.com/posts/a-successful-git-branching-model/