Sharing branches

By default, the branches we create locally, stay local. If we try to push a local branch onto the remote repository, Git will throw an error saying that the current branch has no upstream branch. If we run

git branch -vv

Git will show us our local and remote tracking branches, and we can see which ones have an upstream and which ones don't.


You can see all remote tracking branches by running

git branch -r
Sharing branches 1
Sharing branches 2

To link a local branch to a remote branch, the first time we push, we have to run

git push -u remote branch_name

where -u is the short version of --set-upstream, remote is the name of the remote repository we want to push to, and branch_name is the name of the branch. If we want to push to the GitHub repository, then remote should be set to origin.

Notice that, in this case, we pushed the branch, without anything new to commit. We just used the push command to tell GitHub that there's a new pointer that we are going to be keeping track of.


We can see this by checking the log with

git log --oneline --all --graph

or by checking the branches with

git branch -vv

or by checking our remote tracking branches with

git branch -r
Sharing branches 3
Sharing branches 4

From now on, whenever we want to push local commits onto the remote tracking branch, we just use the git push command, the same as we did with the master branch.


When we push, Git will tell GitHub to store our commits and to move the remote feature pointer to the new commit. It will also update the status of the remote tracking branch on our local repository.

The same holds true for mergers. Once you are done working locally and merge your changes onto the local master branch, you can simply push, and Git and GitHub will sync up and move the pointers accordingly (assuming no one else pushed to the remote between the time you pushed the branch and the time you are trying to push the merge. If that is the case, there might be conflicts for you to resolve).

Sharing branches 5
Sharing branches 6

At some point, once you are done and merged your changes, you're going to need to delete your branch. To delete the remote branch, we run

git push -d remote branch_name

But our local branch is still "linked" to the remote (it's not...that's why Git is telling us that the remote is gone and it's not showing up on our list of remote tracking branches, but the local branch still thinks it's there). Just remember to delete local branches too by running

git branch -d branch_name

When collaborating on a branch, any person can create the remote branch from their local repository and push it like we just saw. Another way is to create them directly on GitHub.


All you have to do is to click on the button that says main and type the name of the new branch. When you press Enter, GitHub will create the new branch.

Sharing branches 7
Sharing branches 8

At this point, our local Git repository has no idea that a new branch was created on the remote repository. If we pull changes from the remote, Git will create a new remote tracking branch, but not a local branch. To do that we run

git switch -C branch_name remote/branch_name

where branch_name is the name of our local branch and remote/branch_name is the name of the remote and the name of the branch that we wish to track in that remote. To make this a little bit more clear, take a look at the Git Bash image. Here what we are telling Git that the local branch called bugfix should be paired with the remote tracking branch origin/bugfix.

At some point, you and your teammates are going to finish working on that branch. Someone should now remove that branch from the origin (and his/her local). If you are in charge of doing it, it's just as what we saw before.


If it's not you who's in charge of deleting the remote branch then: first, remember to switch to master on your local repository and bring in the changes with a pull. This will sync up your local repository. It should now show the merge commit your teammate did before deleting the remote branch.


Second, delete your local branch by running git branch -d branch_name just as before. If you now check your remote tracking branches, you'll see that the remote is still there. You need to prune it. To do so, run

git remote prune origin

Notice that the above command does not mention any particular remote tracking branch. This is because it will get rid of all branches with no upstream set. To make sure everything went OK, run git branch -r, and the remote tracking branch should be gone now.