Adding and Removing Remote Branches

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 v0Total 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 v0Switched to branch 'v0'$ git pullYou asked me to pull without telling me which branch youwant to merge with, and 'branch.v0.merge' inyour configuration file does not tell me, either. Pleasespecify which branch you want to use on the command line andtry again (e.g. 'git pull ').See git-pull(1) for details.If you often merge with the same branch, you may want touse 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 v0From file:///home/gitadmin/project1 * branch v0 -> FETCH_HEADAlready 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/v0Branch 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 pullAlready 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 pullFrom file:///home/gitadmin/project1 * [new branch] v0 -> origin/v0 Already up-to-date.

Zack can then switch to the new branch:

zack$ git checkout v0Branch 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.filezack$ 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 pushCounting 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 :v0To 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 v0To 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 originPruning originURL: 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.

Recent Articles

What is the deal with the Git Index? What is the Git Index?

The git "index" is where you place files you want committed to the git repository. Learn more about the git index and git commit by selecting this link.

Visualising Branches And Their Commits | Git Show-branch

We explain and discuss the use of the git show-branch, gitg, gitk and qgit in this Tutorial. Complete with examples.

“Tracking Branches” And “Remote-Tracking Branches”

Both Tracking Branches and Remote Tracking Branches are created on the git client when a git clone command is used.

Temporarily Stashing Your Work

Git Stash is used when you want to record the current state of the working directory and index, without committing, to come back to complete later.

Shared Repositories Should Be Bare Repositories

Bare Repositories - When creating a central repository that will be shared by other users, the repositories should be created with the --bare option.

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Stay on op - Ge the daily news in your inbox