Skip to content

Keeping branches in synch

bjorng edited this page Sep 13, 2010 · 4 revisions

Creating a remote

To keep your branches in synch with my repository (bjorng’s), you will first need to
add a remote name to access my repository:

git remote add bjorng git://github.com/bjorng/wings.git

(The bjorng part of the name could be anything, for instance bg if you want to type less.)

You only need to do this once.

Fetching my branches

To fetch the latest content of my branches:

git fetch bjorng

The branches will be created in your repository with the prefix bjorng.
Branches fetched from another repository are called remote remote.
Remote branches can be listed with the command:

git branch -r

For instance:

  bjorng/dgud/vertex_arrays
  bjorng/dgud/wxwidgets
  bjorng/maint
  bjorng/master
  bjorng/next
  bjorng/pu
  origin/HEAD -> origin/master
  origin/dgud/vertex_arrays
  origin/dgud/wxwidgets
  origin/master

You cannot directly work on a remote branch. You will need to create a local branch
that follows the remote branch.

Creating a local branch that follows a remote branch

To create a local branch that follows, for instance, my branch maint,
the following command is used:

git branch maint bjorng/maint

There will be a message like this shown:

Branch maint set up to track remote branch maint from bjorng.

Resetting a branch

It is sometimes necessary to reset one of your branches to again follow
my branch. For instance, I could have pushed changes to my next branch at
about the same time that you pushed changes to your next branch.

To the synch our next branches, first make sure that I have copied
your changes to my next branch. When your changes are on my next
branch, first make sure that the next is current:

git checkout next

Fetch my branches to your repository:

git fetch bjorng

In particular, bjorng/next should now contain the latest version of my next
branch. Now you can reset your next branch:

git reset --hard bjorng/next

(The --hard option will overwrite any files in the working tree with the contents of
the files on the next branch — make sure that you don’t have any unsaved changes
in the working tree.)

Avoiding that our next branches diverge

You have been working on a feature for two days and checked it in your next branch.
But you suspect that I might have checked a lot changes to my next branch. What
can you about it?

First get my latest changes:

git fetch bjorng

Use the command git status. If the branches have diverged, the output could look
like this:

# On branch next
# Your branch and 'bjorng/next' have diverged,
# and have 1 and 2 different commit(s) each, respectively.
#

Yes, they have diverged, so what can be done about it?

Clone this wiki locally