![]() ![]() ![]() The other mode is "unsafe": if you use it, and it tells Git to wipe out some unsaved file, Git assumes that (a) you knew it meant that and (b) you really did mean to wipe out your unsaved file, so Git immediately wipes out your unsaved file. One mode is "safe": it won't accidentally destroy any unsaved work. These commands have been in Git all along.īut git checkout has, in effect, two modes of operation. From an end-user perspectiveĪll you need are git checkout, git reset, and git revert. I'll throw in one more, the misnamed git revert, as well. To add to VonC's answer, and bring into the picture all the relevant commands, in alphabetical order, I will cover: ![]() So now, this works: git restore -staged -worktree However, HEAD is also a reasonable default for -worktree when combined with -staged, so make it the default anytime -staged is used (whether combined with -worktree or not). (Due to an oversight, the -source requirement, though documented, is not actually enforced.) When -worktree and -staged are combined, -source must be specified to disambiguate the restore source, thus making it cumbersome to restore a file in both the worktree and the index. (Merged by Junio C Hamano - gitster - in commit 4c2941a, ) restore: default to HEAD when combining -staged and -worktreeīy default, files are restored from the index for -worktree, and from HEAD for -staged. See commit 088018e () by Eric Sunshine ( sunshineco). With Git 2.27 (Q2 2020), " git restore -staged -worktree" now defaults to take the contents out of "HEAD", instead of erring out. I'm not sure why the test here behaves differently than my out-of-suite reproduction, but what's here should catch either symptom (and the fix corrects both cases). One curiosity about the test: without this patch, it actually triggers a BUG() when running git-restore: BUG: cache-tree.c:810: new1 with flags 0x4420000 should not be in cache-treeīut in the original problem report, which used a similar recipe, git restore actually creates the bogus index (and the commit is created with the wrong tree). Note that we can't just hoist it out of checkout_worktree() that function needs to iterate over the CE_REMOVE entries (to drop their matching worktree files) before removing them. We can solve this by calling remove_marked_cache_entries() ourselves before writing out the index. Interestingly, they are dropped when we write out the index, but that means the resulting index is inconsistent: its cache-tree will not match the actual entries, and running " git commit" immediately after will create the wrong tree. That actually drops the entries from the index, as well as invalidating the cache-tree and untracked-cache.īut with -staged, we never call checkout_worktree(), and the CE_REMOVE entries remain. In the non-staged case, we end up in checkout_worktree(), which calls remove_marked_cache_entries(). When " git restore -staged " removes a path that's in the index, it marks the entry with CE_REMOVE, but we don't do anything to invalidate the cache-tree. (Merged by Junio C Hamano - gitster - in commit 09e393d, ) restore: invalidate cache-tree when removing entries with -staged See commit e701bab () by Jeff King ( peff). 2020), " git restore -staged" did not correctly update the cache-tree structure, resulting in bogus trees to be written afterwards, which has been corrected. ![]() Or the short form which is more practical but less readable: git restore -SW hello.c Or you can restore both the index and the working tree (this the same as using git-checkout) git restore -source=HEAD -staged -worktree hello.c To restore a file in the index to match the version in HEAD (this is the same as using git-reset) git restore -staged hello.c Git reset can also be used to restore the index, overlapping with git restore. This operation changes the commit history. git-reset is about updating your branch, moving the tip in order to add or remove commits from the branch.The command can also be used to restore files in the index from another commit. This command does not update your branch. git-restore is about restoring files in the working tree from either the index or another commit.git-revert is about making a new commit that reverts the changes made by other commits.There are three commands with similar names: git reset, git restore and git revert. one for branches ( git switch, as seen in " Confused by git checkout"), which deals only with branches, not files.Īs reset, restore and revert documentation states:.one for files ( git restore), which can cover git reset cases.It helps separate git checkout into two commands: I have presented git restore (which is still marked as "experimental") in " How to reset all files from working directory but not from staging area?", with the recent Git 2.23 (August 2019). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |