标签云

微信群

扫码加入我们

WeChat QR Code


Starting with Git v1.8.4, all the answers below that use HEAD or head can now use in place of HEAD instead. See this answer (last section) to learn why you can do that.

2018年10月22日02分09秒

I made a little summery which shows all ways to unstage a file: stackoverflow.com/questions/6919121/…

2018年10月22日02分09秒

Why not git checkout?

2018年10月22日02分09秒

ErikReppen git checkout does not remove staged changes from the commit index. It only reverts un-staged changes to the last committed revision - which by the way is not what I want either, I want those changes, I just want them in a later commit.

2018年10月22日02分09秒

If you use Eclipse, it is as simple as unchecking the files in the commit dialogue box

2018年10月23日02分09秒

Of course, this is not a true undo, because if the wrong git add overwrote a previous staged uncommited version, we can't recover it. I tried to clarify this in my answer below.

2018年10月22日02分09秒

git reset HEAD *.ext where ext is the files of the given extension you want to unadd. For me it was *.bmp & *.zip

2018年10月22日02分09秒

Jonny, the index (aka staging area) contains all the files, not just changed files. It "starts life" (when you check out a commit or clone a repo) as a copy of all the files in the commit pointed to by HEAD. So if you remove a file from the index (git rm --cached) it means you are preparing to make a commit that deletes that file. git reset HEAD <filename> on the other hand will copy the file from HEAD to the index, so that the next commit won't show any changes being made to that file.

2018年10月22日02分09秒

I just discovered that there is a git reset -p just like git add -p. This is awesome!

2018年10月22日02分09秒

You actually can recover overwriten previously staged but uncommited changes but not in a userfriendly way and not 100% secure (at least none I had found): goto .git/objects, search for files created at the time of git add you want to recover (61/3AF3... -> object id 613AF3...), then git cat-file -p <object-id> (might be worth it to recover several hours of work but also a lesson to commit more often...)

2018年10月22日02分09秒

Hah. I followed this same process. Except I gave up and said rm -rf .git, git init because I didn't trust git rm --cached to keep my working copy. It says a little for how git is still overly complex in some places. git unstage should just be a stock standard command, I don't care if I can add it as an alias.

2018年10月22日02分09秒

For me git says git reset HEAD <File>...

2018年10月22日02分09秒

git rm --cached <file> is actually the correct answer, if it is the initial import of <file> into the repository. If you're trying to unstage a change to the file, git reset is the correct answer. People saying that this answer is wrong are thinking of a different question.

2018年10月22日02分09秒

This will actually work, but only on the first commit, where the file didn't exist before, or where the git add command added new files, but not changes to existing files.

2018年10月22日02分09秒

just goes to show how unintuitive and convoluted git is. instead of having parallel "undo" commands, you have to find out how to undo them. Like trying to free your leg in quick sand, and then getting your arm stuck, then getting your other arm stuck... every command should be done through GUI, with dropdown menus items for the options... Think of all the UI, productivity gains we've had, but we have this mess of a retro command line interface. It's not like the git GUI programs make this any more intuitive.

2018年10月22日02分09秒

The message will be different depending on whether the added file was already being tracked (the add only saved a new version to the cache - here it will show your message). Elsewhere, if the file was not previously staged, it will display use "git rm --cached <file>..." to unstage

2018年10月22日02分09秒

Great! The git reset HEAD <file> one is the only one that will work in case you want to unstage a file delete

2018年10月22日02分09秒

My git version 2.14.3 says git reset HEAD to unstage.

2018年10月22日02分09秒

"moves"? This would indicate it has gone from the working directory. That's not the case.

2018年10月22日02分09秒

Why is it obvious?

2018年10月22日02分09秒

Actually, git stage is the alias for git add, which is the historic command, both on Git and other SCM. It has been added in december 2008 with commit 11920d28da in the "Git's git repository", if I can say.

2018年10月22日02分09秒

Strictly speaking there is a way to recover an already staged file that was replaced with git add. As you mention git add creates an git object for that file that will become a loose object not only when removing the file completely but also when being overwritten with new content. But there is no command to automatically recover it. Instead the file has to be identified and extracted manually or with tools written only for this case (libgit2 will allow this). But this will only pay out if the file is very important and big and could not be rebuild by editing the previous version.

2018年10月22日02分09秒

To correct myself: Once the loose object file is found (use meta-data like creation date/time) git cat-file could be used to recover its content.

2018年10月22日02分09秒

Another way to recover changes that were staged but not committed and then overwritten by e.g. another git add is via git fsck --unreachable that will list all unreachable obj, which you can then inspect by git show SHA-1_ID or git fsck --lost-found that will >Write dangling objects into .git/lost-found/commit/ or .git/lost-found/other/, depending on type. See also git fsck --help

2018年10月22日02分09秒

I wasn't looking to un-add everything, just ONE specific file.

2018年10月22日02分09秒

Also helpful if you don't have any previous commits. In absence of previous commit, git reset HEAD <file> would say fatal: Failed to resolve 'HEAD' as a valid ref.

2018年10月22日02分09秒

No, this adds a deletion of everything in your current directory. Very different to just unstaging changes.

2018年10月22日02分09秒

Yes I understand that. I only wanted to implicitly suggest that your indicate that on your answer like "You can use git-gui...." :)

2018年10月22日02分09秒

It says, "git-gui: command not found". I'm not sure if this works.

2018年10月22日02分09秒

I can't under stand the difference of 'git reset head <file>' and 'git rm --cached <file>. Could you explain it?

2018年10月22日02分09秒

jeswang files are either 'known' to git (changes in them are being tracked.), or they are not 'versioned'. reset head undoes your current changes, but the file is still being monitored by git. rm --cached takes the file out of versioning, so git no longer checks it for changes (and also removes eventually indexed present changes, told to git by the prior add), but the changed file will be kept in your working copy, that is in you file folder on the HDD.

2018年10月22日02分09秒

The difference is git reset HEAD <file> is temporary - the command will be applied to the next commit only, but git rm --cached <file> will unstage untill it gets added again with git add <file>. Also, git rm --cached <file> means if you push that branch to the remote, anyone pulling the branch will get the file ACTUALLY deleted from their folder.

2018年10月22日02分09秒

+1. An extraordinary number of highly-upvoted answers and comments on this page are just flat-out wrong about the behaviour of git rm --cached somefile. I hope this answer makes its way up the page to a prominent position where it can protect newbies from being misled by all the false claims.

2018年10月22日02分09秒

One tip is to copy your .git/config file if you have added remote origin, before deleting the folder.

2018年10月22日02分09秒

ChrisJohnsen comment is spot on. Sometimes, you want to commit all files except one: git add -A && git rm --cached EXCLUDEFILE && git commit -m 'awesome commit' (This also works when there's no previous commits, re Failed to resolve 'HEAD' problem)

2018年10月22日02分09秒

Isn't this going to remove the file altogether?

2018年10月22日02分09秒

Confirmed! Tried a git reset after a git add . and git was complaining about corrupt HEAD. Following your advice, I could git add & reset back and forth with no problems :)

2018年10月22日02分09秒

The second part works, but it is a bit clumsy. How line endings are handled, depends on autocrlf value... This won't work in every project, depending the settings.

2018年10月22日02分09秒

This answer was reasonable at the time it was posted, but is now obsolete; git reset somefile and git reset both work prior to making the first commit, now. This has been the case since several Git releases back.

2018年10月22日02分09秒

MarkAmery, you may be right (it'd be cool if you posted a source for your assertion), but there's still value in starting your repo with a clean commit or two.

2018年10月22日02分09秒

Sure, but then you have the followup question of how one should unadd one of two (or more) files added. The "git reset" manual does mention that "git reset <paths>" is the opposite of "git add <paths>", however.

2018年10月22日02分09秒

I tried git reset <path> and it works just fine without a separator. I'm also using git 1.9.0. Maybe it doesn't work in older versions?

2018年10月22日02分09秒

Mind that the --cached is a really important part here.

2018年10月22日02分09秒

-1; no, this doesn't un-stage the file, it stages a deletion of the file (without actually deleting it from your work tree).

2018年10月22日02分09秒

Actually, this does not reset every file because * uses shell expansion and it ignores dotfiles (and dot-directories).

2018年10月22日02分09秒

You can run git status to see anything remaining and reset it manually i.e. git reset file.

2018年10月22日02分09秒

Mind that * will usually not include dotfiles or 'dot-directories' unless you explicitly specify .* or .*.prj

2018年10月22日02分09秒

Won't work if there's no last commit.

2018年10月22日02分09秒

As it happens, there was a last commit... but I was specifically asking about removing a single file from the commit, not every file from the commit.

2018年10月22日02分09秒

Suppose I am at 1st pic meaning meaning I have not even did "git.add". Also, I not at all want all this change. I mean when I do git status, it should not show any red files. I mean it should be in sync as if there was not a single file altered since the last git push. how to achieve that.

2018年10月22日02分09秒

SO suppose you are just at step first. And you want to get rid of all the changes you have done which is making "newFile.txt" to come up as red.

2018年10月22日02分09秒

When I do git status. I should not see any change at all. All the red files should get reverted.

2018年10月22日02分09秒

Hi, I think your question is how to remove untracked files from the current tree. For that, you can use "git clean -f -d". This will remove untracked directories as well.

2018年10月22日02分09秒

If you don't want to delete the untracked files, just ignore "-f" flag.

2018年10月23日02分09秒

Please explain the difference between git reset <file> and git checkout <file>.

2018年10月22日02分09秒

reset doesn't change the file, just put it away from the stage (=index, where it was put by git add)

2018年10月22日02分09秒

checkout change the codes in file and move to the last updated state. reset doesn't change the codes it just reset the header. As example, reset use for added or committed files resetting before push and checkout use for back to the last updated/committed stage before git add.

2018年10月22日02分09秒

reset = remove the file from stage however changes will still be there. checkout = gets the updated file from the repository and will overrides the current file

2018年10月22日02分09秒

"I'm surprised that no one mention interactive mode" - they did: stackoverflow.com/a/10209776/1709587

2018年10月22日02分09秒