A Step by Step Guide to Git Branches
📣 Sponsor
In my previous guide on getting started with git, I briefly covered how git branches work. You can create new branches using the git branch
command, for example. In this guide, we’ll go in depth on branches, and how to use git branch
effectively.
What are branches in Git?
Branches in git are basically pointers to a piece of development work. They let you branch off from the current code base, so that you can work independently from what is already there. Below, each circle represents a commit. Each branch can have its own commits, and they can all be developed independently from each other:
Above, we have two branches, both branched from different commits on the main branch. Your main branch is the first branch you start with in your git project. Branches basically let you take your work, copy it, and work on it separately. A branch can be made at any point in your commit history by running git branch x
, where x
is the name of the branch:
git branch my-new-branch
At some point in your development, you may end up with a lot of branches. In these situations, you can find out all of your branches by running git branch --list
:
git branch --list
NOTE that by creating a branch, you will not be automatically set onto that branch. To switch to a branch, you have to explicitly switch to it using git switch
:
git switch my-new-branch
Deleting branches in git
When it comes down to it, you might realise you no longer need a branch anymore. To delete a branch, use the -d
option. For example, git branch -d my-branch
will delete my-branch
.
git branch -d my-branch
Renaming a branch in git
You can rename a branch in git using the -m
command. This will rename the current branch to whatever comes after -m
. For example, git branch -m new-branch
renames your current branch to new-branch
.
Setting and deleting remote branches
When you create a new branch and try to use git push
to push to your upstream, you may run into issues as no upstream branch has been set. You can do two things here:
- add a new upstream branch to push your branch to on your remote repository.
- push changes from your local computer to your upstream on a branch that already exists.
Either option may be fine for your situation. To create a new branch on your remote repository, simply run git remote add
:
git remote add my-remote-branch https://github.com/smpnjn/effective-guacamole.git
This will create a branch called my-new-branch
on your remote repository. If the branch on your local computer was called my-branch
, you can push my-branch
to your remote branch, my-remote-branch
, by running the following command:
git push --set-upstream my-remote-branch my-branch
You can also delete a remote branch by running the following:
git push origin --delete my-remote-branch
Merging branches into your main branch
Once you have made a number of commits to your branch, you may want to merge it back into master. To do that, you should first commit any changes to your branch, using git add
and git commit
:
git add -A
git commit -m "My commit"
Then, switch or checkout back to main:
git checkout main
You may want to pull anything from remote, using git pull
first, but ultimately once on main
we can merge code using git merge
. If your branch was my-branch
, you could do this:
git merge my-branch
This could result in some merge conflicts, which you will need to review and manually fix, but most modern code editors like Visual Studio Code will have a GUI to help you do this. Otherwise, you can run git diff
, should you run into merge conflicts to diagnose the issue. There are a number of ways to resolve merge conflicts, which we’ll cover in git merge
. If you’re interested in resolving git merge conflicts, you can learn more about that here.
More Tips and Tricks for Git
- Git blame - How to find out who modified a line with Git
- How to make Git forget a tracked file now in .gitignore
- Git: Renaming a Branch
- Git Merge: Merging Changes from other Branches
- Git Stash - Everything about stashing changes in git
- How to move your Githooks to your Repo
- How to undo a git pull
- Resolving Git Merge Conflicts
- How to make Git ignore file permission (chmod) changes
- How to amend and update a git commit