As a part of GIT Tutorials End To End, we will learn about creating a remote branch in Git in this post.
I will expect that you are aware of the basic concepts and commands of GIT. But if you are a beginner in GIT then I strongly recommend you to refer GIT Basic Commands and Concepts section on my blog first.
Did you know that I have started a YouTube channel as well and I need your support to make it successful. Please do watch content then comment, like, share, and obviously subscribe.
In the previous post, I have explained Create, Switch And List Branches In Git. In this post, I have mentioned that “When we create a new branch, then those branches are local to you. These branches will not be available for others and remote repositories till we push it.“. Let’s understand this statement in more detail.
Creating remote branch
Suppose, You have cloned a remote repo in your personal computer which is called a “Local repository“. So you are given a feature to develop and you created a new branch from main (or whatever it is) and start writing codes for new features in the new branch. This new branch is in your local repository only as of now. If you check the remote repository (GitHub, GitLab or BitBucket) then you will not find a newly created branch.
Let’s clone one of my public repo to practice and learn easily. The same repo I have used in my previous posts. In this repo, I have only the default branch “main” and two commits.
I have cloned the above Git repo in a folder “gitRemoteBranchPractice“.
Let’s create a branch and list local and remote branches after that.
Before creating branch
Just ignore line “origin/HEAD -> origin/main” in the output of “git branch -r“. As of now, we have the same number and name of branches i.e. “main” in the local and remote repo.
After creating branch
You can see we have a new local branch but the same is not present in the remote.
This makes sense as you can create multiple branches locally and do different stuff. It is not impacting any other developers who are using the same repo. But if you want to store your local branch changes in a remote branch or you want to get your code reviewed by someone or if you want your code to be merged with the main branch then you must need to push your branch or branch changes to the remote repo branch.
To push a new branch to the remote repo you need to use the command “git push -u origin <branchName>“. Please note you can not push your new branch to my example repo. You should create your own repo in GitHub (or whatever you like) and then try. While pushing the new branch to remote it will ask you credentials as well.
Now go to your remote repo and see if you find your new branch. You can also verify using commands.
If you have created multiple branches then either you can push branches one by one or you can push all branches at once using “git push -all” command.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe to my YouTube channel.