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

发布于 2024-12-01 16:53:18 字数 228 浏览 2 评论 0 原文

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

弃爱 2024-12-08 16:53:18
git reset --soft HEAD^

这会将您的索引重置为 HEAD^ (之前的提交),但将更改保留在暂存区域中。

方便的图表 -scm.com/docs/git-reset" rel="noreferrer">git-reset docs

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

git reset --soft HEAD~1
git reset --soft HEAD^

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

There are some handy diagrams in the git-reset docs

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

git reset --soft HEAD~1
裂开嘴轻声笑有多痛 2024-12-08 16:53:18

一种简单的方法

  1. 将文件提交到暂存区

    git reset --soft HEAD^1

  2. < p>暂存到取消暂存:(使用“git reset HEAD ...”取消暂存)

    git重置HEAD git命令.txt
    或 git reset HEAD *ds.txt


*-->所有以 ds.txt 结尾的文件都需要取消暂存。

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

在此处输入图像描述

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

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

Refer the below pic for clarity:

enter image description here

紧拥背影 2024-12-08 16:53:18

将提交移回暂存区域取决于您的最后一次提交。
如果您的最后一次提交是存储库的第一次(或初始)提交,那么您需要执行

git update-ref -d HEAD

如果您的最后一次提交不是第一次(或初始)提交,则执行

git reset HEAD~

To move a commit back to the staging area depends on your last commit.
If your last commit was the first(or initial) commit of the repo, then you need to execute

git update-ref -d HEAD

If your last commit is not the first(or initial) commit, then execute

git reset HEAD~
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文