Adding And Removing Remote Branches – Git Branch
Commands discussed in this section:
- git branch
- git checkout
- git push
- git remote
Creating Remote Branches
One way to add a new branch to the remote repository is to first add the branch to your local repository and then push that local branch to the remote repository. Let’s see what branches we have now:
amy$ git branch * master
We have just one branch. Not much to shake a stick at. So Amy creates a new branch named v0:
amy$ git branch v0
She then pushes the new branch named v0 to the remote repository named origin.
The git push syntax is: git push [remote-repository-name] [branch-or-commit-name]:
amy$ git push origin v0 Total 0 (delta 0), reused 0 (delta 0) To file:///home/gitadmin/project1.git * [new branch] v0 -> v0
Currently the master and v0 branches are identical, but they will diverge (the whole point of branches is to diverge)
as users make different commits to each branch.
Tracking The New Branch: Other Users
When other users git clone the shared repository, the git clone command will automatically:
- Create the a new local branch named v0
- Configure their local repository to correctly track changes in the new v0 branch. For example, when the users are on the new local v0 branch, The user can type git fetch, git pull, and git push without specifying the origin remote and v0 branch with every command.
Tracking The New Branch: Amy (The Creator of the Remote Branch)
Since Amy created the new remote branch, she has more configuration to do to make her local v0 branch configured correctly.
We can see that the v0 branch is not yet configured correctly for Amy:
amy$ git checkout v0 Switched to branch 'v0' $ git pull You asked me to pull without telling me which branch you want to merge with, and 'branch.v0.merge' in your configuration file does not tell me, either. Please specify which branch you want to use on the command line and try again (e.g. 'git pull '). See git-pull(1) for details. If you often merge with the same branch, you may want to use something like the following in your configuration file: [branch "v0"] remote = merge = [remote ""] url = fetch = See git-config(1) for details.
If she enjoys typing, she could always specify the remote repository (origin) and the branch (v0) to update every time she needs to pull or fetch:
amy$ git pull origin v0 From file:///home/gitadmin/project1 * branch v0 -> FETCH_HEAD Already up-to-date.
However, Amy recently chipped a fingernail while playing volleyball and prefers to minimize her typing. So she configures git to automatically pull/fetch from the new remote v0 branch, without having to specify the v0 repository and branch name every time she uses git pull or git fetch.
She’s running version 1.7.0 of git which has the –set-upstream flag:
amy$ git branch --set-upstream v0 origin/v0 Branch v0 set up to track remote branch v0 from origin.
With git versions earlier than 1.7.0 that don’t have the –set-upstream flag, you can use the following instead of using the above git branch –set-upstream v0 origin/v0 command:
$ git config branch.v0.remote origin $ git config branch.v0.merge refs/heads/v0
That does the job, and from now on, all she has to type is:
amy$ git pull Already up-to-date.
Zack plays with the new branch
The next time Zack retrieves the latest commits from the shared repository, he’ll be amazed to see the new v0 branch automatically created for him:
zack$ git pull From file:///home/gitadmin/project1 * [new branch] v0 -> origin/v0 Already up-to-date.
Zack can then switch to the new branch:
zack$ git checkout v0 Branch v0 set up to track remote branch v0 from origin. Switched to a new branch 'v0'
Zack adds to the new branch
zack$ echo A Zack change >> zack.file zack$ git commit -a -m'added a new line to zack.file' [v0 93c1568] added a new line to zack.file 1 files changed, 1 insertions(+), 0 deletions(-) zack$ git push Counting objects: 5, done. Delta compression using up to 2 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 302 bytes, done. Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. To file:///home/gitadmin/project1.git 05affb3..93c1568 v0 -> v0
Amy deletes the remote branch
The syntax for deleting branches isn’t exactly intuitive. To remove a remote branch, use the git push command like this:
amy$ git push origin :v0 To file:///home/gitadmin/project1.git - [deleted] v0
Another flavor of the same command that does the same thing, but maybe is a little more intutive:
amy$ git push origin --delete v0 To file:///home/gitadmin/project1.git - [deleted] v0
Well, maybe not much more intuitive. The git push origin portion of the command line tells git you want to change something in the remote repository you are calling origin, and the rest of the line “–delete v0” tells git what you want to do with the remote repository.
Zack removes tracking branches whose remote branches are removed
After someone deletes a branch from a remote repository, git will not automatically delete the local repository branches when a user does a git pull or git fetch. However, if the user would like to have all tracking branches removed from their local repository that have been deleted in a remote repository, they can type:
zack$ git remote prune origin Pruning origin URL: file:///home/gitadmin/project1.git * [pruned] origin/v0
Note the above pruning removed the tracking branch, but did not delete the v0 branch in Zack’s local repository. Nice pruning, Zack. Your shrubbery, I mean, project, is coming together.
Next: Git Remotes: “Tracking Branches” and “Remote-Tracking Branches”
Previous: Shared Repositories Should Be Bare Repositories
Git Remotes Example: Creating a Shared Repository; Users Sharing The Repository
Git Remotes Up Close: The Configuration File – “remote” section
Git Remotes Up Close: The Configuration File – “branch” section
Git Remotes: Fun Commands You Can Use