8

i try to Checkout a Remotebranch via LibGitSharp. In git itself you use this comands:

git fetch origin
git checkout -b test origin/test

in newer Versions it is just:

git fetch
git checkout test

So i tried this Code:

repo.Fetch("origin");
repo.Checkout("origin/" + Name);

The Fetch and Checkout runs without any problems but there is no copy of the Remotebranch.

Does anyone have an idea to Checkout the Remote with other methods?

My alternative would be to create the Branch in the Repository and push it into Remote:

Branch newBranch = repo.Branches.Add(Name, repo.Branches["master"].Commits.First());
repo.Network.Push(newBranch);

but i get this Exception:

The branch 'Test1' ("refs/heads/Test1") that you are trying to push does not track an upstream branch.

maybe i could set the Branch to an upstream Branch, but i don't know how.

Edit: I haven't explained it properly, so I try to describe it better what the Fetch and Checkout does in my program. The Fetch command is performed correctly. Now if i use the checkout command it should be create an local Branch of the Remotebranch, but it doesn't. I've also tried repo.Checkout(name), without "origin/" ,but it cast an Exception: No valid git object identified by '...' exists in the repository.

user3541236
  • 171
  • 2
  • 12
  • *"there is no copy of the Remotebranch."* -> Can you please update the question to explain further what you expect by "copy of RemoteBranch"? – nulltoken Apr 28 '14 at 10:39
  • "The Fetch and Checkout runs without any problems" -> So the workdir is correctly updated with the content of the commit pointing at by the remote tracking branch? – nulltoken Apr 28 '14 at 10:41

1 Answers1

31

If I correctly understand your question, you're willing to create a local branch which would be configured to track the fetched remote tracking branch.

In other words, once you fetch a repository, your references contains remote tracking branches (eg. origin/theBranch) and you'd like to create a local branch bearing the same name (eg. theBranch).

The following example should demonstrate how to do this

const string localBranchName = "theBranch";

// The local branch doesn't exist yet
Assert.Null(repo.Branches[localBranchName]);

// Let's get a reference on the remote tracking branch...
const string trackedBranchName = "origin/theBranch";
Branch trackedBranch = repo.Branches[trackedBranchName];

// ...and create a local branch pointing at the same Commit
Branch branch = repo.CreateBranch(localBranchName, trackedBranch.Tip);

// The local branch is not configured to track anything
Assert.False(branch.IsTracking);

// So, let's configure the local branch to track the remote one.
Branch updatedBranch = repo.Branches.Update(branch,
    b => b.TrackedBranch = trackedBranch.CanonicalName);

// Bam! It's done.
Assert.True(updatedBranch.IsTracking);
Assert.Equal(trackedBranchName, updatedBranch.TrackedBranch.Name);

Note: More examples can be found in the BranchFixture.cs test suite.

Martin Costello
  • 9,672
  • 5
  • 60
  • 72
nulltoken
  • 64,429
  • 20
  • 138
  • 130
  • 1
    Was this answer helpful? I'd be happy to update it would I have misunderstood your question. – nulltoken Apr 29 '14 at 08:11
  • Jep, that's really good. I did not notice the Updatefunction in repo.Branches before. :-) – user3541236 Apr 29 '14 at 11:07
  • That very last line where it says TrackedBranch.Name won't compile. Name does not exist. Perhaps it should be TrackedBranch.CanonicalName? Other properties are FriendlyName, RemoteName, and UpstreamBranchCanonicalName. – Triynko Mar 16 '20 at 22:29
  • Just used and worked for me too. Thank you. – Rogério Silva Feb 11 '23 at 15:14