Skip to content

Branch checkout fails if there are untracked files in the working directory #296

Closed
@JustinBeckwith

Description

@JustinBeckwith

Using the git command line, there's nothing stopping me from checking out a branch if I have un-staged changes in my working directory. Libgit2sharp throws an exception if I try to do that using the branch.checkout method:

LibGit2Sharp.MergeConflictException was unhandled
  HResult=-2146233088
  Message=There are changes to files in the working directory that would be overwritten by a checkout.Please commit your changes before you switch branches.
  Source=LibGit2Sharp
  StackTrace:
       at LibGit2Sharp.Repository.CheckoutInternal(String commitIdOrCanonicalBranchName, CheckoutOptions checkoutOptions, CheckoutProgressHandler onCheckoutProgress) in c:\Users\justbe\SkyDrive\Code\libgit2sharpBugs\libgit2sharp\LibGit2Sharp\Repository.cs:line 529
       at LibGit2Sharp.Repository.Checkout(Branch branch, CheckoutOptions checkoutOptions, CheckoutProgressHandler onCheckoutProgress) in c:\Users\justbe\SkyDrive\Code\libgit2sharpBugs\libgit2sharp\LibGit2Sharp\Repository.cs:line 499
       at LibGit2Sharp.RepositoryExtensions.Checkout(IRepository repository, Branch branch) in c:\Users\justbe\SkyDrive\Code\libgit2sharpBugs\libgit2sharp\LibGit2Sharp\RepositoryExtensions.cs:line 251
       at LibGit2Sharp.Branch.Checkout() in c:\Users\justbe\SkyDrive\Code\libgit2sharpBugs\libgit2sharp\LibGit2Sharp\Branch.cs:line 196
       at libgit2sharpBugs.Program.Main(String[] args) in c:\Users\Justin\SkyDrive\Code\libgit2sharpBugs\libgit2sharpBugs\Program.cs:line 25
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Code used to find issue:

var path = @"c:\users\justin\test\ExpressStarter";            
var x = Repository.Clone("https://github.com/JustinBeckwith/ExpressStarter.git", path);

// create a new file in the directory with some content
using (var fs = new StreamWriter(Path.Combine(path, "bug.txt"))) 
{
    fs.WriteLine("hello there");
}

// create a branch
var branch = x.CreateBranch("newby1");
branch.Checkout();

The same issue pops up when you try to checkout a branch with any untracked file, such as a file that's in your .gitignore.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions