git 进行本地提交后,如何再次取消暂存文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6682740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I unstage my files again after making a local commit?
提问by Kit Ho
I have executed the following command
我已经执行了以下命令
git add <foo.java>
git commit -m "add the foo.java file"
How can I delete my local commit now and unstage foo.java?
我现在如何删除我的本地提交并取消 foo.java?
If I type git reset --hard
, I found that it reverts my modified foo.java
to the original one.
如果我输入git reset --hard
,我发现它会将我的修改恢复foo.java
为原始的。
回答by Antti
git reset --soft HEAD~1
should do what you want. After this, you'll have the first changes in the index (visible with git diff --cached
), and your newest changes not staged. git status
will then look like this:
git reset --soft HEAD~1
应该做你想做的。在此之后,您将获得索引中的第一个更改(用 可见git diff --cached
),并且您的最新更改不会暂存。git status
然后看起来像这样:
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: foo.java
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: foo.java
#
You can then do git add foo.java
and commit both changes at once.
然后git add foo.java
,您可以同时执行和提交这两项更改。
回答by Ryan Stewart
Use:
用:
git reset HEAD^
That does a "mixed" reset by default, which will do what you asked; put foo.java in unstaged, removing the most recent commit.
默认情况下会进行“混合”重置,这将按照您的要求进行;将 foo.java 放在 unstaged 中,删除最近的提交。
回答by Andrey Deineko
To me, the following is more readable (thus preferable) way to do it:
对我来说,以下是更易读(因此更可取)的方法:
git reset HEAD~1
Instead of 1
, there could be any number of commits you want to unstage.
取而代之的是1
,您可能想要取消暂存任何数量的提交。
回答by wRAR
git reset --soft
is just for that: it is like git reset --hard
, but doesn't touch the files.
git reset --soft
只是为了:它就像git reset --hard
,但不触及文件。
回答by Lavika
For unstaging all the files in your last commit -
为了取消上次提交中的所有文件 -
git reset HEAD~
git reset HEAD~
回答by HieroB
"Reset" is the way to undo changes locally. When committing, you first select changes to include with "git add"--that's called "staging." And once the changes are staged, then you "git commit" them.
“重置”是在本地撤消更改的方法。提交时,您首先选择要包含在“ git add”中的更改——这就是所谓的“暂存”。一旦更改被暂存,那么你“ git commit”它们。
To back out from either the staging or the commit, you "reset" the HEAD. On a branch, HEAD is a git variable that points to the most recent commit. So if you've staged but haven't committed, you "git reset HEAD." That backs up to the current HEAD by taking changes off the stage. It's shorthand for "git reset --mixed HEAD~0."
要退出暂存或提交,您可以“重置”HEAD。在一个分支上, HEAD 是一个指向最近提交的 git 变量。因此,如果您已上演但尚未提交,则可以“ git reset HEAD ”。通过从舞台上移除更改来备份到当前的 HEAD。它是“ git reset --mixed HEAD~0”的简写。
If you've already committed, then the HEAD has already advanced, so you need to back up to the previous commit. Here you "reset HEAD~1" or "reset HEAD^1" or "reset HEAD~" or "reset HEAD^"-- all reference HEAD minus one.
如果你已经提交了,那么 HEAD 已经提前了,所以你需要备份到之前的提交。在这里你“ reset HEAD~1”或“ reset HEAD^1”或“ reset HEAD~”或“ reset HEAD^”——所有参考HEAD减一。
Which is the better symbol, ~ or ^? Think of the ~ tilde as a single stream-- when each commit has a single parent and it's just a series of changes in sequence, then you can reference back up the stream using the tilde, as HEAD~1, HEAD~2, HEAD~3, for parent, grandparent, great-grandparent, etc. (technically it's finding the firstparent in earlier generations).
〜或^哪个更好?将 ~ 波浪号视为单个流——当每个提交都有一个父项并且它只是一系列按顺序进行的更改时,您可以使用波浪号引用备份流,如 HEAD~1, HEAD~2, HEAD ~3,对于父母、祖父母、曾祖父母等(从技术上讲,它是寻找前几代的第一个父母)。
When there's a merge, then commits have more than one parent. That's when the ^ caret comes into play--you can remember because it shows the branches coming together. Using the caret, HEAD^1 would be the first parent and HEAD^2 would be the second parent of a single commit--mother and father, for example.
当进行合并时,提交将有多个父级。这就是 ^ 插入符号发挥作用的时候——你可以记住,因为它显示了分支聚集在一起。使用插入符号, HEAD^1 将是单个提交的第一个父级, HEAD^2 将是单个提交的第二个父级 - 例如,母亲和父亲。
So if you're just going back one hop on a single-parent commit, then HEAD~ and HEAD^ are equivalent--you can use either one.
所以如果你只是在单亲提交上返回一跳,那么 HEAD~ 和 HEAD^ 是等效的——你可以使用任何一个。
Also, the reset can be --soft, --mixed, or --hard. A soft reset just backs out the commit--it resets the HEAD, but it doesn't check out the files from the earlier commit, so all changes in the working directory are preserved. And --softreset doesn't even clear the stage (also known as the index), so all the files that were staged will still be on stage.
此外,重置可以是--soft、--mixed或--hard。软重置只会取消提交——它会重置 HEAD,但不会检出先前提交中的文件,因此工作目录中的所有更改都会保留。并且--softreset 甚至不会清除舞台(也称为index),因此所有已上演的文件仍将在舞台上。
A --mixedreset (the default) also does not check out the files from the earlier commit, so all changes are preserved, but the stage is cleared. That's why a simple "git reset HEAD" will clear off the stage.
一个--mixed复位(默认),也不会从签出文件的早期承诺,因此,所有的变化被保留,但阶段已被清除。这就是为什么一个简单的“ git reset HEAD”将清除舞台的原因。
A --hardreset resets the HEAD, and it clears the stage, but it also checks out all the files from the earlier commit and so it overwrites any changes.
一个-硬复位复位头,它清除阶段,但它也检查出所有从文件中较早的承诺,所以它覆盖任何变化。
If you've pushed the commit to a remote repository, then resetdoesn't work so well. You can reset locally, but when you try to push to the remote, git will see that your local HEAD is behind the HEAD in the remote branch and will refuse to push. You may be able to force the push, but git really does not like doing that.
如果您已将提交推送到远程存储库,则重置效果不佳。您可以在本地重置,但是当您尝试推送到远程时,git 会看到您本地的 HEAD 在远程分支中的 HEAD 后面,并且会拒绝推送。您也许可以强制推送,但 git 真的不喜欢这样做。
Alternatively, you can stashyour changes if you want to keep them, check outthe earlier commit, un-stash the changes, stage them, create a new commit, and then push that.
或者,如果您想保留更改,您可以存储更改,检查较早的提交,取消存储更改,暂存更改,创建新提交,然后推送。
回答by Vasantha Ganesh K
Lets say you want to unstage changes upto n commits,
假设您想取消最多 n 次提交的更改,
Where commit hashes are as follows:
其中提交哈希如下:
- h1
- h2 ...
- hn
- hn+1
- 小时1
- h2 ...
- 恩
- hn+1
Then run the following command:git reset hn
然后运行以下命令:git reset hn
Now the HEAD will be at hn+1. Changes from h1 to hn will be unstaged.
现在 HEAD 将位于 hn+1。从 h1 到 hn 的更改将取消暂存。