Search code examples
gitlibgit2sharprefspec

Git refspec for fetching everything from remote but keep local commits (LibGit2Sharp)


I'm managing git repos with LibGit2Sharp. I have a bare git repository, cloned from remote A. Now I'd like to fetch all changes (i.e. all heads and tags) from remote B and do so:

repository.Network.Fetch("git-url", new[] { "+refs/*:refs/*" })

Or the equivalent (if I'm not mistaken) in git command line:

git fetch "git-url" +refs/*:refs/*

As it happens however remote B is identical to A apart from two new commits not being there (which commits are on two branch heads). This causes the fetch operation to remove those two commits in the clone, basically yielding the same if I'd have cloned from remote B.

What is the correct refspec (or otherwise the correct method) of doing a fetch that will keep local commits intact while allowing new commits to be fetched from remote B? I also tried +refs/*:+refs/* but this will add branch labels to older commits while refs/*:refs/* won't fetch new changes either.


Solution

  • Usually refs from a remote repository go to refs/remotes/<remote_name>/... Thus you can separate references, fetched from different remotes and/or created locally.

    That's why a typical fetch reference map looks like: +refs/heads/*:refs/remotes/origin/*

    When you choose to map the references directly, one to one, you essentially create a complete mirror of a given remote repo.