删除未推送的 git 提交

发布于 2024-08-08 11:39:20 字数 455 浏览 11 评论 0 原文

我进行了 git commit ,但尚未将其推送到存储库。 因此,当我执行 git status 时,我得到“# Yourbranch is before 'master' by 1 commit”。

因此,如果我想回滚我的最高提交,我可以这样做吗:

git reset --hard eb27bf26dd18c5a34e0e82b929e0d74cfcaab316

鉴于当我执行 git log 时,我得到:

commit eb27bf26dd18c5a34e0e82b929e0d74cfcaab316
Date:   Tue Sep 29 11:21:41 2009 -0700


commit db0c078d5286b837532ff5e276dcf91885df2296
Date:   Tue Sep 22 10:31:37 2009 -0700

I did a git commit but I have not pushed it to the repository yet.
So when I do git status, I get '# Your branch is ahead of 'master' by 1 commit.

So if I want to roll back my top commit, can I just do:

git reset --hard eb27bf26dd18c5a34e0e82b929e0d74cfcaab316

given that when I do git log I get:

commit eb27bf26dd18c5a34e0e82b929e0d74cfcaab316
Date:   Tue Sep 29 11:21:41 2009 -0700


commit db0c078d5286b837532ff5e276dcf91885df2296
Date:   Tue Sep 22 10:31:37 2009 -0700

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

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

发布评论

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

评论(13

鲸落 2024-08-15 11:39:20

如果您尚未将更改推送到远程

git reset HEAD~1

通过git status检查工作副本是否干净。

否则您已将更改推送到远程

git revert HEAD

此命令将恢复/删除本地提交/更改,然后您可以推送

IF you have NOT pushed your changes to remote

git reset HEAD~1

Check if the working copy is clean by git status.

ELSE you have pushed your changes to remote

git revert HEAD

This command will revert/remove the local commits/change and then you can push

烛影斜 2024-08-15 11:39:20

实际上,当你使用git reset时,你应该引用你要重置的提交;;所以您可能需要 db0c078 提交。

一个更简单的版本是 git reset --hard HEAD^ ,重置到当前 head 之前的上一次提交;这样您就不必复制提交 ID。

当您执行任何 git reset --hard 操作时要小心,因为您可能会丢失任何未提交的更改。您可能需要检查 git status 以确保您的工作副本是干净的,或者您确实想要清除其中的任何更改。

此外,您可以使用 origin/master 作为参考,而不是 HEAD,正如 @bdonlan 在评论中建议的那样:git reset --hard origin/master

Actually, when you use git reset, you should refer to the commit that you are resetting to; so you would want the db0c078 commit, probably.

An easier version would be git reset --hard HEAD^, to reset to the previous commit before the current head; that way you don't have to be copying around commit IDs.

Beware when you do any git reset --hard, as you can lose any uncommitted changes you have. You might want to check git status to make sure your working copy is clean, or that you do want to blow away any changes that are there.

In addition, instead of HEAD you can use origin/master as reference, as suggested by @bdonlan in the comments: git reset --hard origin/master

乞讨 2024-08-15 11:39:20

我相信其中之一将满足您的需求

1 - 撤消提交并保留所有文件:
git reset --soft HEAD~

2 - 撤消提交并取消暂存所有文件:
git reset HEAD~

3 - 撤消提交并完全删除所有更改:
git reset --hard HEAD~

这是我找到答案的地方

I believe that one of those will fit your need

1 - Undo commit and keep all files staged:
git reset --soft HEAD~

2 - Undo commit and unstage all files:
git reset HEAD~

3 - Undo the commit and completely remove all changes:
git reset --hard HEAD~

here is were I found the answer

小傻瓜 2024-08-15 11:39:20
git reset --hard origin/main

它适用于其他分支:

git reset --hard origin/master
git reset --hard origin/staging

将其重置为原点所在的位置。

这是 @bdonlan 在评论中发布的。我为那些不看评论的人添加了这个答案。

git reset --hard origin/main

It works for other branch:

git reset --hard origin/master
git reset --hard origin/staging

to reset it to whatever the origin was at.

This was posted by @bdonlan in the comments. I added this answer for people who don't read comments.

回首观望 2024-08-15 11:39:20

这个问题有两个分支(回滚提交并不意味着我想丢失所有本地更改):

1.恢复最新提交并放弃已提交文件中的更改< /strong> do:

git reset --hard HEAD~1

2. 恢复最新提交,但保留本地更改(在磁盘上) do:

git reset --soft HEAD~1

这条命令(后面的命令)将把你带到你执行 git add 时的状态。

如果您想在此之后取消暂存文件,请执行

git reset

现在,您可以在添加然后再次提交之前进行更多更改。

There are two branches to this question (Rolling back a commit does not mean I want to lose all my local changes):

1. To revert the latest commit and discard changes in the committed file do:

git reset --hard HEAD~1

2. To revert the latest commit but retain the local changes (on disk) do:

git reset --soft HEAD~1

This (the later command) will take you to the state you would have been if you did git add.

If you want to unstage the files after that, do

git reset

Now you can make more changes before adding and then committing again.

摇划花蜜的午后 2024-08-15 11:39:20

删除推送前的最后一次提交

git reset --soft HEAD~1

1 表示最后一次提交,如果要删除最后两个使用 <代码>2,依此类推*

Remove the last commit before push

git reset --soft HEAD~1

1 means the last commit, if you want to remove two last use 2, and so forth*

早乙女 2024-08-15 11:39:20

只需在控制台中输入:

$ git reset HEAD~

此命令会丢弃远程 HEAD 之前的所有本地提交

Simply type in the console :

$ git reset HEAD~

This command discards all local commits ahead of the remote HEAD

不一样的天空 2024-08-15 11:39:20

我经历过与下面相同的情况,因为这更容易。
通过传递 commit-Id,您可以到达您想要去的特定提交:

git reset --hard {commit-id}

由于您想删除最后一次提交,因此您需要传递 commit-Id需要移动指针:

git reset --hard db0c078d5286b837532ff5e276dcf91885df2296

I have experienced the same situation I did the below as this much easier.
By passing commit-Id you can reach to the particular commit you want to go:

git reset --hard {commit-id}

As you want to remove your last commit so you need to pass the commit-Id where you need to move your pointer:

git reset --hard db0c078d5286b837532ff5e276dcf91885df2296
彡翼 2024-08-15 11:39:20

这就是我所做的:

首先签出您的分支(对于我的情况 master 分支):

git checkout master

然后重置为远程 HEAD^ (它将删除所有本地更改),强制清洁和拉动:

git reset HEAD^ --hard && git clean -df && git pull

This is what I do:

First checkout your branch (for my case master branch):

git checkout master

Then reset to remote HEAD^ (it'll remove all your local changes), force clean and pull:

git reset HEAD^ --hard && git clean -df && git pull
酷到爆炸 2024-08-15 11:39:20

一种方法是删除本地分支并从服务器检出该分支,如果您的本地分支领先于远程多次提交,并且您需要取消所有提交。

One way would be to delete the local branch and checkout that branch from the server if your local branch is ahead of remote by multiple commits and you need to uncommit all of them.

孤蝉 2024-08-15 11:39:20

我刚刚遇到了同样的问题,最终做了:

git rebase -i HEAD~N

(N 是 git 将向您显示的提交数量)

这会提示您的文本编辑器,然后您可以通过删除与其关联的行来删除您想要的提交。

I just had the same problem and ended up doing:

git rebase -i HEAD~N

(N is the number of commits git will show you)

That prompts your text editor and then you can remove the commit you want by deleting the line associated with it.

旧瑾黎汐 2024-08-15 11:39:20

从提交中删除文件夹

git rm -r --cache <folder name>

从提交中删除文件

git rm --cache <file name>

To delete folder from commit

git rm -r --cache <folder name>

To delete file from commit

git rm --cache <file name>
情话难免假 2024-08-15 11:39:20

如果其他人正在寻找相反的情况,即恢复到您最初认为应该删除的提交,请使用git reflog信用:Murtuzaali Surti 关于 git reflog 的文章,用于查找丢失的提交。

幸运的是,即使在尝试了多次疯狂的 git reset --soft/hard 命令之后,我也没有设法删除我需要的提交,老实说,我越来越有信心 git<无论如何,/strong> 都会支持我,至少在我做出承诺的情况下。
您仍然需要git reset --hard
但请参阅该文章的链接以获取指导。

If anyone else is looking for the opposite i.e., reverting back to the commit you initially thought you should delete, use git reflog credit: Murtuzaali Surti's article on git reflog to find the missing commits.

I luckily didn't manage to delete the commit I needed back, even after attempting multiple frantic git reset --soft/hard commands and honestly I'm becoming more and more confident that git has my back no matter what, at least if a commit has been made.
You will still need to git reset --hard <SHA-found-with-reflog>
But see the link to the article for guidance.

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