Posted by & filed under Web Development.

A “remote branch” is a branch that you are not in charge of maintaining, and is not a part of your local repository. It would be rather chaotic if we just let people have free access to write what they want to our branches. This would make accounting for changes at least somewhat difficult! Therefore, Git provides a way to add remote branches to your repository to “track” changes and merge them in with your own.

A “remote branch” is a branch that you are not in charge of maintaining, and is not a part of your local repository. It would be rather chaotic if we just let people have free access to write what they want to our branches. This would make accounting for changes at least somewhat difficult! Therefore, Git provides a way to add remote branches to your repository to “track” changes and merge them in with your own.

Managing Remote Branches of the Origin Repository

If your repository happens to be a clone of an original (created with the “git clone” command), information about its origin is embedded into Git’s internal metadata (located in the “.git” folder at the root of your project). To confirm this, you can do the following:

# git branch -r
  origin/HEAD -> origin/master
  origin/master

Here, we see that the “origin” project contains a branch called “master” (the standard name issued to the default and original branch). If you would like to be able to merge in changes from the origin project, you can do so by “tracking” the original into a branch of your own. This branch contains a copy of the origin branch which you can update manually at any time. We don’t want the branch automatically updating on its own based on changes made by the owner of the origin branch, and we especially don’t want changes being made to our primary working branch – hence the copy.

To create a copy of the origin branch “master” for tracking:

git checkout --track -b <branchcopyname> origin/master

Substitute “branchcopyname” with whatever you want to call this new branch copy, and “origin/master” with whatever branch in parent origin that you wish to track.

Once you have created this new branch you can check it out, issue a “git pull” to download changes however frequently you wish, and you can then switch back to your branch and merge in the changes via a “git merge” as if it were just any old local branch.

Managing Branches of Other Repositories

If you project you would like to merge changes with is not an origin project, providing you have the necessary filesystem-level permissions to read from the remote project you can add this project’s branches to your own repository. This technique can also be an alternative to “git push” if you don’t want to give this person the ability to push to your project (say, you don’t want to give the person the filesystem permissions he/she needs, you don’t want to give this person SSH access to your repository, etc.)…

To add a remote branch to your repository:

git remote -t <newbranchname> <remotebranchname> <path to repos>

Substitute the name you’d like to give this branch in your repository for “newbranchname”, the name of the branch as it exists in the remote repository for “remotebranchname”, and the path to the repository for “path to repos”. This path can be local or the URL for a remote machine in the format of:

ssh://hostname/path/to/repos

Once you have told Git about this remote branch (i.e. added this metadata to your Git repository), create a branch you’d like to use to track the remote branch (using the name you gave for “newbranchname” makes the most sense here), check this branch out, and pull in the changes with a “git pull” command. One catch though, you’ll have to issue the name of the repository you wish to pull from as follows:

git pull <remotebranchname>

Substitute the name you provided for remotebranchname above for “remotebranchname”.

Once you have this working branch you can pull from at your leisure you can likewise treat it like any other local branch and merge changes to your own branches via the “git merge” command.