Checking out remote branch using libgit2sharp

Viewed 3505

Consider the following, I have two branches on github: master and dev. I have a local repository that's a clone of and is pointing to the remote master but I'd like to switch it to the dev branch so that any changes I make will be committed to dev instead of master. What steps would be required to do that? I've tried multiple ways, but they never seem to work and it always commits to master. Please explain simply as I'm still new to Git and libgit2sharp so I'm still trying to wrap my head around it.

Edit: I'm using the libgit2sharp library to issue the git commands, not the Git shell

2 Answers

When you do not yet have a local branch named dev, but your remote does have a dev branch, then running git checkout dev will create a new dev branch locally, set up the remote tracking information and then switch to the newly created branch. You can emulate this behavior in LibGit2Sharp.

To create a local branch dev that tracks the remote dev branch (refs/remotes/origin/dev), first locate the remote branch:

Branch trackedBranch = repo.Branches["origin/dev"];

Then create a new local branch called dev that points to the same commit as the origin/dev branch:

Branch localBranch = repo.CreateBranch("dev", trackedBranch.Tip);

Then set up the tracking between the local and remote branches:

repo.Branches.Update(localBranch, b => b.UpstreamBranch = "refs/heads/dev");

Finally, switch to the new branch:

LibGit2Sharp.Commands.Checkout(repo, "dev");

If your dev branch exists then:

git checkout dev

If your dev branch does not yet exist, then you will need to create it and then switch to it with:

git checkout -b dev

Using libgit2sharp, there is no way to do a git checkout -b (that I know of), so you would need to use CreateBranch to create a branch and then Checkout to perform a checkout of the branch.

The code would look like this:

private void GetOrCreateThenCheckoutBranch(string myPath, string branchName)
{
    // NOTE: This code is for demo purposes only.  It is a bad idea
    // to create code that couples multiple functions/actions together

    using (var repo = new Repository("path/to/your/repo"))
    {
        var devBranch = repo.Branches["dev"];

        if (branch == null)
        {
            // Repository returns null object when branch does not exist
            // so, create a new, LOCAL branch
            repo.CreateBranch("dev");   // Or repo.Branches.Add("dev", "HEAD");
            devBranch = repo.Branches["dev"];

            // You will need more code _here_ if you want to synchronize/set 
            // an upstream branch...
        }

        // Now, checkout the branch
        Branch currentBranch = Commands.Checkout(repo , devBranch);

        // Do more stuff...
}

Git is a distributed version control system. When you make changes, you are making changes in the branch in your local copy of the repository. Once you have made your changes locally, you will then need to

  • add and commit the changes locally (possibly push them up to the remote repository, if you have a mapped, remote branch of the same name)
  • merge the changes back into master
  • push the changes up to the remote master repository (typically origin/master).

If the remote branch does not allow anyone to do a merge, you will need to issue a pull request.

Related