-1

I'm searching a way to discard all changes of file with path.

in libGit2Sharp there is exist method

CheckoutOptions options = new CheckoutOptions { CheckoutModifiers = CheckoutModifiers.Force };

repo.CheckoutPaths("49916", checkoutPaths, options);

but I see no similar method in libgit2 API

How can I reset file to start state of head's commit?


I do not need unstage changes -- I need exactly revert SINGLE file to state of head commit.

Andrew_STOP_RU_WAR_IN_UA
  • 9,318
  • 5
  • 65
  • 101

1 Answers1

0

Use git_checkout_tree. Pass NULL for the treeish to reset to the HEAD commit. Pass the path(s) you want to reset in opts.paths and set opts.checkout_strategy to GIT_CHECKOUT_FORCE. You probably also want to or GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH into checkout_strategy to disable wildcard matching.

Jason Haslam
  • 2,617
  • 13
  • 19
  • already tried this and it isn't worked for me =( for both git_checkout_tree and git_checkout_index – Andrew_STOP_RU_WAR_IN_UA Jun 29 '21 at 07:25
  • (setting `opts.path` + `checkoutStrategy` to `.Force` ); Didn't did `DISABLE_PATHSPEC_MATCH`. As result - it's delete all of files that was changed, but not files written into `opts.path` – Andrew_STOP_RU_WAR_IN_UA Jun 29 '21 at 07:27
  • I have added code comments for being sure that you will understand swift https://i.stack.imgur.com/ZVIOh.png – Andrew_STOP_RU_WAR_IN_UA Jun 29 '21 at 15:51
  • `git_checkout_index` will only do what you want if the index matches the HEAD. If the file is already staged, it won't do anything. I don't understand swift well enough to know if you've initialized `opts` and `opts.paths` correctly. – Jason Haslam Jun 29 '21 at 17:39
  • I use this func only with unstaged paths. As you see, there is comment above function "discardChanges" that tells that we can do this only with unstaged files :) – Andrew_STOP_RU_WAR_IN_UA Jun 29 '21 at 17:50
  • I use this func only with unstaged paths. If you look on the screenshot one more time, you will see comment above function "discardChanges" that tells that we can call it only with unstaged files :) – Andrew_STOP_RU_WAR_IN_UA Jun 29 '21 at 18:01
  • I use the same way initialisations in another places and there all works fine. So my problem of the code that it discards all changes (except files that wasn't added to repository earlier) instead of discard only files configured inside of opts.paths. Generally all you saw on the screenshot is mix of Swift and C code. This is not clean swift. – Andrew_STOP_RU_WAR_IN_UA Jun 29 '21 at 18:01
  • and about that `opts.paths` can be assigned incorrectly, you can look on the following screenshot: https://i.stack.imgur.com/63ys6.png . It is shows that they are assigned correctly - only one path in array, path is relative. And absolutely sure path is existing in repo. And instance of options created as well. Because of checkout can be done :) No error was throwned. – Andrew_STOP_RU_WAR_IN_UA Jun 29 '21 at 18:11