Are you having trouble getting back to the “master” branch in Git? This article focuses on how you can use different branches in Git, view your branches, and easily go back to your master branch.
Using Git Branches
Here is some additional context about Git branches, and how you can use them in your project. Or, if you just want to know how to get back to master you can skip ahead to that part of the article.
Git allows for the creation of “branches” in your project, from which you can change files, create or delete files, and virtually change any aspect of your project without affecting the “master” branch. Changes from branches can be later “merged” into master, but until they are merged the changes remain completely separate from the status of the master branch.
Branches are highly valuable when it comes to testing out fixes or changes to the code of your project. If you want to experiment with changes, additions, deletions, or refactoring in a completely non-destructive environment, creating a divergent Git branch is a great way to accomplish that.
How To View Git Branches
At any time in the course of your work you can easily view branches by running the
git branch command:
In a new project with no additional branches, you will only see
* master after running the
git branch command. But, if you have multiple branches they will all be listed.
Be sure to check out our full guide on creating and switching between branches in Git.
How to Go To Master Branch in Git
No matter which branch you are currently working in, you can always run the same command to get back to master:
git checkout master
You will then see a success message indicating that you successfully switched back to master:
Switched to branch 'master'
Remember, if you have uncommitted changes in the divergent branch then they will carry over when you switch back to master–however still in their dangling, uncommitted state. To avoid this, make sure to commit your changes to the divergent branch before checking out master.
Well done! You now know how you can always get back to the master branch from any other branch in your Git project.