Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is equivalent of git checkout branch_name on LibGit2Sharp

I have 2 branches in git server which are master and develop. I clone the repository to my machine using the code below.

var options =
    new CloneOptions()
    {
        CredentialsProvider = this.credentialsHandler,
    };

return
    Repository.Clone(
        gitHttpUrl,
        localPath,
        options);

The default branch is master. I want to switch to develop branch with the following code.

using (var repo = new Repository(localPath))
{
    var branch = repo.Branches[branchName];

    repo.Checkout(branch);
}

but branch is null.

How can I switch the branch using LibGit2Sharp?

Update
The default branch is master and below are the steps to reproduce the problem.

  1. Pull
  2. Checkout develop branch
  3. Pull, it throws an exception here.

This is the code:

private void Checkout(string localPath, string branchName)
{
    using (var repo = new Repository(localPath))
    {
        var branch = repo.Branches[branchName];

        if (branch == null)
        {
            branch = repo.CreateBranch(branchName, "origin/" + branchName);
        }

        repo.Checkout(branch);
    }
}

private MergeResult Pull(string path)
{
    const string NAME = "test-user";
    const string EMAIL = "[email protected]";

    using (var repo = new Repository(path))
    {
        var merger =
            new Signature(
                NAME,
                EMAIL,
                DateTimeOffset.UtcNow);

        var options =
            new PullOptions
            {
                FetchOptions = new FetchOptions()
                {
                    CredentialsProvider = this.credentialsHandler
                }
            };

        return repo.Network.Pull(merger, options);
    }
}
like image 843
Anonymous Avatar asked Sep 24 '14 09:09

Anonymous


People also ask

What is the command for git checkout?

New Branches Git checkout works hand-in-hand with git branch . The git branch command can be used to create a new branch. When you want to start a new feature, you create a new branch off main using git branch new_branch . Once created you can then use git checkout new_branch to switch to that branch.

Is git switch the same as checkout?

Difference between git checkout and git switch It can also be used to restore changes from a certain commit. But git checkout does more than that. It allows you to copy files from any branch or commit directly into your working tree without switching branches.

What is the opposite of git checkout?

The git checkout command can be used in a commit, or file level scope. A file level checkout will change the file's contents to those of the specific commit. A revert is an operation that takes a specified commit and creates a new commit which inverses the specified commit.

What is the difference between git checkout and Branchname checkout?

git checkout -b BRANCH_NAME creates a new branch and checks out the new branch while git branch BRANCH_NAME creates a new branch but leaves you on the same branch. In other words git checkout -b BRANCH_NAME does the following for you.


1 Answers

Similarly to Git, when cloning, LibGit2sharp will only create a local branch for the remote HEAD

All others branches will be fetched as well and accessible as remote tracking branches, but no local branch will be automatically created for them.

Thus, in order to achieve your goal, you can either checkout the remote tracking branch (eg. origin/master). Note, that, in this case, the HEAD will be automatically set in detached mode.

repo.Checkout["origin/my_branch"];

Or, create a local branch from the remote tracking one, then checking out the local one.

var branch = repo.CreateBranch("my_branch", "origin/my_branch");

repo.Checkout(branch);
like image 175
nulltoken Avatar answered Sep 24 '22 00:09

nulltoken