Search code examples
gitgit-remote

Git different history on multiple remotes


I'm using two remote servers.

Is it possible for create different history starting point?

For example:

Remote 1: commit1 -> commit2 -> commit3 -> commit4

Remote 2: commit3 -> commit4

In this case, I would like commit3 to be the history starting point on remote 2.


Solution

  • In the case you're describing, Repo 2 is a "shallow" repository. If you have Repo 1

    A -- B -- C -- D
    

    then you can create repo 2 as

    git clone --mirror --depth=2 --no-single-branch file://localhost/path/to/repo1 repo2
    

    and now Repo 2 is

    C -- D
    

    In this repo you can see that C has a parent, and you can see the SHA ID for B listed as the parent of C; but commit B itself (and its history) is not present.

    I used mirror because you described this as a second remote. So this makes it bare and sets up the branches as local branches instead of remote branch refs. You can create a shallow repo that isn't a mirror (with or without --bare), depending on your needs. The real key is the depth argment.

    By default depth only copies a single branch and configures the repo to only update that particular branch's ref on fetch. no-single-branch restores normal behavior of mapping all refs. That said, if there is a large or complex history in Repo1 then getting exactly what you want included may not be as easy as what I've shown here.

    Note that if you use a local path, depth would be ignored; which is why I use a file:// url above.

    This is definitely a special case. A commit's identity encodes its full history (which is one way to look at why C has to "know" its parent SHA ID), so generally having different histories would mean having different commits (that maybe happen to have similar TREEs) in Repo 1 than in Repo 2; so they could not coexist as remotes for a single, coherent local repo. But in the specific case where you just want to exclude the earliest history up to a point, modern git provides this solution.