8

I am working on a project and I have a central git repo. This project is a skeleton to be a baseline for a number of forks.

Is it possible to configure my local working repository for a fork to track the central for the project as origin and track the skeleton's master as a separate branch named upstream tracking the master of the skeleton to cherry pick changes to the skeleton?

I guess I want my workflow to be something like:

Create Skeleton >> Fork Skeleton >> Skeleton Pulls Changes from Fork 2 >> Fork 1 Pulls Changes from Skeleton

Is there a better process to do what I have described?

Steve Buzonas
  • 5,300
  • 1
  • 33
  • 55

2 Answers2

10

Read the "Step 3: Configure remotes" of the GitHub "Fork a Repo" page (I know you didn't mention GitHub, but it is still relevant)

  • origin is the remote address of your fork, that your local clone can pull from/push to
  • upstream is the remote address of your original repo Skeleton (you can add it with a git remote add upstream https://..../Skeleton.git)

So upstream isn't a branch.

But you can define a local branch which will have for upstream branch the remote tracking branch master from upstream repo, with git branch:

git branch --set-upstream upstream_master upstream/master

However, you don't need a local branch, especially if you won't ever make new commits on it: you can compare your master with upstream/master directly, after a git fetch upstream, cherry-picking what you need from upstream/master.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • That's along the lines of what I was trying to do that prompted this question. The command `git branch --set-upstream upstream_master upstream/master` will not work directly after a `git remote add upstream /srv/repos/git/skeleton.git`. Why is that? I accidentally corrected the issue trying to troubleshoot. I didn't exactly want to run `git fetch upstream` in my master work tree, but using that to troubleshoot then allows me to add a remote tracking branch to upstream/master. I suppose it is because the remote branch upstream/master isn't in my repo until then. – Steve Buzonas Jun 13 '12 at 12:54
  • directly after `remote add`? You need a `git fetch upstream` first, in order for the remote tracking branch `upstream/master` to be fetched and put in your local repo. A `git fetch` doesn't modify any of your local files. – VonC Jun 13 '12 at 12:56
1

According to your description, you should rebase the forks on the skeleton whenever it changes, using

$ git rebase upstream

This will transform the situation as follows

initially:
1 - 2 - 3 <- upstream
        \- 4 <- fork

upstream changes:
1 - 2 - 3 - 5 - 6 <- upstream
        \- 4 <- fork

after rebase:
1 - 2 - 3 - 5 - 6 <- upstream
                \- 4 <- fork

In other words, your fork will look like as if it were forked from the most recent version of the skeletton.

The disadvantage of this approach is that it changes the history of the forks... If you don't want this, you can just merge upstream into fork (no need to cherry-pick I think).

Sjlver
  • 1,227
  • 1
  • 12
  • 28
  • If files were removed in upstream after the fork and before the rebase, what will happen when git applies changes to those files? – Steve Buzonas Jun 13 '12 at 12:59
  • If you didn't modify the files, they will get removed during the rebase. If you did modify them, you will have a conflict that you have to resolve manually. – Sjlver Jul 22 '12 at 11:39