Git reset specific file9/3/2023 ![]() Note: With git checkout -overlay HEAD - (Git 2.22, Q1 2019), files that appear in the index and working tree, but not in are removed, to make them match exactly.īut that checkout can respect a git update-index -skip-worktree (for those directories you want to ignore), as mentioned in " Why do excluded files keep reappearing in my git sparse checkout?".įor the case of simply discarding changes, the git checkout - path/ or git checkout HEAD - path/ commands suggested by other answers work great. ![]() If you have extra files in the working tree which don't exist in HEAD, a git checkout HEAD - won't remove them. git checkout HEAD - does a hard reset for a path, replacing both the index and the working tree with the version from the HEAD commit.Īs answered by Ajedi32, both checkout forms don't remove files which were deleted in the target revision.git checkout - doesn't do a hard reset: it replaces the working tree contents with the staged contents.Note (as commented by Dan Fabulich) that: That would replace both the index and working tree with HEAD content, like an reset -hard would, but for a specific path. With Git 2.23 (August 2019), you have the new command git restore (also presented here) git restore -source=HEAD -staged -worktree - aDirectory # How to make files a/* reappear without changing b and without recreating a/c? EDIT: And I also don't "know" b, or which other directories reside on the same level as a. Note that I do not want to explicitly restore a/a and a/b, I only "know" a and want to restore everything below. ![]() Insert the proper command below the How to make files comment - the current command will restore the file a/c/ac which is supposed to be excluded by the sparse checkout. The script below illustrates the problem. What is the proper Git command for this operation? I can reverse-patch the current state using git diff subdir | patch -p1 -R, but this is a rather weird way of doing this. Īgain: Why git can't do hard/soft resets by path? There is git reset -hard, but it won't let me do it for a subdirectory: > git reset -hard. adds directories excluded by sparse checkout Imagine the following use case: I want to get rid of all changes in a specific subdirectory of my Git working tree, leaving all other subdirectories intact. UPDATE: This will work more intuitively as of Git 1.8.3, see my own answer. The obvious case is when you make some changes, commit them, then later decide you don’t want them anymore.UPDATE²: With Git 2.23 (August 2019), there's a new command git restore that does this, see the accepted answer. But the state of your file will determine what changes you’re actually undoing. ![]() You may think of resetting a file as undoing changes. A file can have different contents in each of these locations. When you’re in the process of gathering changes, you stage files. Files that you commit ultimately end up in the repository. The files you edit locally belong to the working tree. The following terms are key to understanding the difference: working tree, staging area, and repository. On other occasions, you might want to roll back changes that you've already committed to the repository. Sometimes, you’ll want to undo local changes so a file is back in sync with the current repository. ![]() Also, the git reset command might not match your expectations for what resetting means. It’s important to note that resetting a file might mean different things to different people under different circumstances. ![]()
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |