Branches
1 View Current Branch Location
Lists all branches in your local repo and highlights the one you’re currently on with an asterisk (
*
). Useful for making sure you’re working in the right place before committing or merging.
git branch
2 Create a New Branch
Branches let you safely experiment with new ideas. This command creates a new branch and switches you to it.
git checkout -b branch-name
Branches are local by default — others won’t see your new branch unless you push it to GitHub.
git push origin branch-name
This uploads your branch so others can collaborate.
3 Switch to Main and Merge
Once your feature is ready, switch back to the
main
branch and merge your changes in. This brings your work into the main codebase.
git checkout main
git merge branch-name
If both branches edited the same part of a file, Git can’t automatically merge. You’ll need to manually fix the conflict, then commit the result.
4 Delete the Feature Branch
After merging, you can delete the branch to keep your workspace clean. Don’t worry — the changes are still in
main
.
git branch -d branch-name