
Let's decode branching in Git
28 November, 2022
3
3
1
Contributors
Introduction
Open Source Contribution is one of the most verified ways to become a good developer. But to start with open source contributions it is necessary that you understand the concept of branching in git very well. In this blog, we are going to master the concepts of Git Branching. So are you excited? Let's begin:
The task of a branch is to point at the latest commit. Let's see the types of branches in git:
Types Of Branches in Git
📌Local Branches
📍Non-Tracking Local Branches
📍Local Tracking Branches
📌Remote Branches
📍Remote Tracking Branches
🎉Local Branches => A local branch is a branch that only you (the local user) can see. It exists only on your local machine. This is a branch on your machine that you can work in and add commits to.
🎊Remote Branches => A remote branch is a branch in a remote location (in most cases
origin
). You can push the newly created local branch myNewBranch
to origin
. Now other users can track it.The
git push -u <remote> <branch name>
command uploads content from a local repository to a remote repository. It is generally used to upload modifications in a local repository with remote team members.-u
: The -u
flag creates a tracking reference for every branch that you successfully push onto the remote repository. The local branch you push is automatically linked with the remote branch. This allows you to use commands such as git pull without any arguments.✨Remote Tracking Branches => A remote-tracking branch is a local copy of a remote branch. When
myNewBranch
is pushed to origin
using the command above, a remote tracking branch named origin/myNewBranch
is created on your machine. This remote-tracking branch tracks the remote branch myNewBranch
on origin
. You can update your remote-tracking branch to be in sync with the remote branch using
git fetch
or git pull
.Running git fetch
will update the remote-tracking branches to reflect the state of the corresponding remote branches.✨Local Tracking Branches =>A local tracking branch is a local branch that is tracking another branch. This is so that you can push/pull commits to/from the other branch. Local tracking branches in most cases track a remote tracking branch. When you push a local branch to
origin
using the git push
command with a -u
option (as shown above), you set up the local branch myNewBranch
to track the remote tracking branch origin/myNewBranch
. This is needed to use git push
and git pull
without specifying an upstream to push to or pull from. Tracking also causes git status
to inform you when your branch is ahead or behind the remote.Suppose I create a branch foo. foo will be a local branch (not tracking any remote) until you push it. Once you push it it will create an origin/foo and track it.
Always Remember,
•
Local tracking branches point to the local head.
•
Remote Tracking branches point to the local copy of the remote head.
Git Branching Commands:
Now, I am going to share a list of commands which I prepared when I started my open-source contribution journey, and believe me this list will make you master in git branching.
Note:
origin
is merely the conventional name for your remote repo. upstream
is another conventional name (for a remote repo that your repo was forked from).

Table showing all commands for git branches
Note:
origin
is merely the conventional name for your remote repo. upstream
is another conventional name (for a remote repo that your repo was forked from).
Renaming a Branch
When we make an open-source contribution or use git for personal projects, sometimes occurs a case that we need to rename a branch. So let's discuss this renaming part in detail:
To rename a local branch:
🎁If it's your current branch: git branch -m new_name
🎁If it is another branch you want to rename: git branch -m old_name new_name
If your branch was pushed, then after renaming you need to delete it from the remote Git repository and ask your new local to track a new remote branch:
•
To delete the branch on remote: git push origin --delete <old-name> Or you can shorten the process of deleting the remote branch like this: git push origin:<old-name>
•
Then you should push the new branch to remote: git push origin <new-name>
•
To reset the upstream branch for the new-name local branch use the -u flag with the git push command: git push origin -u <new-name>
Conclusion
So this sums up the blog. I hope you must have learned something new by going through this blog. Now you have to practice these commands so that they can stay with you for a longer time. And you know the best way will be to start contributing to open source. Believe me, once you will start contributing you will find these commands at your fingertips.
Do share this blog with your friends because you will receive good only when you do good. Keep Contributing!!
git
github
opensource
delevate