English 中文(简体)
How to achieve `git rm --cached` and `git rm -f`with LibGit2Sharp?
原标题:

1) I tried using repo.Index.Remove(item.Filename);, but it s giving me:

Additional information: Unable to remove file file.txt . Its current status is Modified .

I need to find a way to do git rm file.txt -f (i.e. force removal).

2) I need a way to do git rm file.txt --cached so that the file is only removed from the repository (staged for removal), but still keeping it in the filesystem. Is there a way to do this in LibGit2Sharp?

3) Is it OK to call repo.Index.Remove() if the given path has never been part of the repository (i.e. the file is untracked)?

最佳回答

I need to find a way to do git rm file.txt -f (i.e. force removal).

There s no built-in way to do this. Might be worth opening a feature request in the issue tracker.

I need a way to do git rm file.txt --cached

Although the content check won t be performed, I think repo.Index.Unstage() should fit your need. The documentation of the method states Removes from the staging area all the modifications of a file since the latest commit (addition, updation or removal).

Is it OK to call repo.Index.Remove() if the given path has never been part of the repository (i.e. the file is untracked)?

No, it s not ;) Similarly to git, LibGit2Sharp will throw if one is trying to Remove a file with one of the following status:

  • FileStatus.Nonexistent
  • FileStatus.Removed
  • FileStatus.Modified
  • FileStatus.Untracked

Description of what those status mean can be found here.

问题回答

Looks like this will do for git rm -f:

File.Delete(RepositoryFullPath + "/" + item.Filename);

if (!item.Status.HasFlag(LibGit2Sharp.FileStatus.Untracked))
{
    repo.Index.Stage(RepositoryFullPath + "/" + item.Filename);
}

I don t know how to do --cached yet.





相关问题
git confusion - cloning a repo is returning a past version

Im having some confusion with my git usage. I cloned a repo from one comp to the other, and the new clone is the state of the original that was active some time ago. So its cloning a past version. ...

Appropriate strategy for tagging and hotfixing with git

I was wondering if the strategy I m using for tagging and hotfixing tags (which then I use for deploying rails applications) with git is appropriate. For tagging I just tag a commit of the master ...

Tips on upgrading CVS to git/hg?

We still use CVS, I use git and hg for my personal use though I m still a novice at both, but I realize they re much more modern and better, faster, distributed, etc. It s just everyone is so ...

Using Git in a TFS shop

Using Git at home has spoiled me - I now find using TFS at work to be a bit of a drag and want to explore the possibility of using Git locally and syncing somehow with TFS. I figure there are a few ...

热门标签