如何将提交移动到 git 中的暂存区?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7214039/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:42:31  来源:igfitidea点击:

How do you move a commit to the staging area in git?

gitcommitstaging

提问by Jonathan M Davis

If you want to move a commit to the staging area - that is uncommit it and move all of the changes which were in it into the staging area (effectively putting the branch in the state that it would have been in prior to the commit) - how do you do it? Or is it something that you can't do?

如果您想将提交移动到暂存区 - 即取消提交并将其中的所有更改移动到暂存区(有效地将分支置于提交之前的状态) -你怎么做呢?或者是你不能做的事情?

The closest that I know how to do is to copy all of the files that were changed in the commit to somewhere else, reset the branch to the commit before the commit that you're trying to move into the staging area, move all of the copied files back into the repository, and then add them to the staging area. It works, but it's not exactly a nice solution. What I'd like to be able to do is just undo the commit and move its changing into the staging area. Can it be done? And if so, how?

我知道如何做的最接近的是将提交中更改的所有文件复制到其他地方,在您尝试移动到暂存区的提交之前将分支重置为提交,移动所有将复制的文件放回存储库,然后将它们添加到暂存区。它有效,但它不是一个很好的解决方案。我希望能够做的只是撤消提交并将其更改移动到暂存区。可以做到吗?如果是这样,如何?

回答by Abizern

git reset --soft HEAD^

This will reset your index to HEAD^(the previous commit) but leave your changes in the staging area.

这会将您的索引重置为HEAD^(上一次提交),但将您的更改留在暂存区。

There are some handy diagramsin the git-resetdocs

还有一些便利的图表git-reset文档

If you are on Windows you might need to use this format:

如果您使用的是 Windows,则可能需要使用以下格式:

git reset --soft HEAD~1

回答by Shiva

A Simple Way

一个简单的方法

  1. Committed files to Staging Area

    git reset --soft HEAD^1

  2. Staging to UnStage:(use "git reset HEAD ..." to unstage)

    git reset HEAD git commands.txt or git reset HEAD *ds.txt

  1. 提交文件到暂存区

    git reset --soft HEAD^1

  2. 暂存到 UnStage:(使用“git reset HEAD ...”取消暂存

    git reset HEAD git commands.txt or git reset HEAD *ds.txt

here, *--> all files end with ds.txt to unstage.

在这里, *--> 所有以 ds.txt 结尾的文件都可以取消暂存。

Refer the below pic for clarity:

为清楚起见,请参阅下图:

enter image description here

在此处输入图片说明