如何从分支中删除提交?

发布于 2024-08-03 06:00:28 字数 52 浏览 4 评论 0 原文

如何从分支历史记录中删除提交?我应该使用 git reset --hard HEAD 吗?

How do I delete a commit from my branch history? Should I use git reset --hard HEAD?

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

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

发布评论

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

评论(30

你对谁都笑 2024-08-10 06:00:28

小心: git reset --hard 将删除您的工作目录更改
在运行此命令之前,请务必存储您想要保留的任何本地更改

假设您正在执行该提交,那么此命令将对其进行破坏...

git reset --hard HEAD~1

HEAD~1 表示 head 之前的提交。

或者,您可以查看 git log 的输出,找到要备份到的提交的提交 ID,然后执行以下操作:

git reset --hard <sha1-commit-id>

如果您已经推送了它,则需要执行强制推送以摆脱它......

git push origin HEAD --force

但是,如果其他人可能已经拉动它,那么你最好开始一个新分支。因为当他们拉动时,它只会将其合并到他们的工作中,然后你会再次将其推回原处。

如果您已经推送,最好使用 git revert 来创建一个“镜像”提交来撤消更改。但是,这两次提交都会记录在日志中。


仅供参考:如果您想摆脱正在进行的工作,git reset --hard HEAD 非常有用。它会将您重置回最近的提交,并删除工作树和索引中的所有更改。

git stash 的作用相同,只是您可以在需要时恢复它,而不是通过重置硬模式永久删除。使用 git stash list 和 git stash show 'stash@123' 检查你的存储


最后,如果你需要找到你“删除”的提交,它通常是存在的在 git reflog 中,除非您对存储库进行了垃圾收集。

Careful: git reset --hard WILL DELETE YOUR WORKING DIRECTORY CHANGES.
Be sure to stash any local changes you want to keep before running this command.

Assuming you are sitting on that commit, then this command will wack it...

git reset --hard HEAD~1

The HEAD~1 means the commit before head.

Or, you could look at the output of git log, find the commit id of the commit you want to back up to, and then do this:

git reset --hard <sha1-commit-id>

If you already pushed it, you will need to do a force push to get rid of it...

git push origin HEAD --force

However, if others may have pulled it, then you would be better off starting a new branch. Because when they pull, it will just merge it into their work, and you will get it pushed back up again.

If you already pushed, it may be better to use git revert, to create a "mirror image" commit that will undo the changes. However, both commits will be in the log.


FYI: git reset --hard HEAD is great if you want to get rid of WORK IN PROGRESS.It will reset you back to the most recent commit, and erase all the changes in your working tree and index.

git stash does the same except you can restore it later if you need, versus permanently delete with reset hard mode. Check your stashes by using git stash list and git stash show 'stash@123'


Lastly, if you need to find a commit that you "deleted", it is typically present in git reflog unless you have garbage collected your repository.

酒废 2024-08-10 06:00:28

如果您尚未将提交推送到任何地方,可以使用 git rebase -i 删除该提交。首先,找出该提交距离(大约)有多远。然后执行以下操作:

git rebase -i HEAD~N

~N 表示重新设置最后 N 提交的基准(N 必须是数字,例如 HEAD~10代码>)。然后,您可以编辑 Git 向您提供的文件以删除有问题的提交。保存该文件后,Git 将重写所有以下提交,就好像您删除的文件不存在一样。

Git 书籍中有一个很好的关于变基的部分,其中包含图片和示例。

但要小心这一点,因为如果您更改了已推送到其他地方的内容,则需要另一种方法,除非您打算强制推送。

If you have not yet pushed the commit anywhere, you can use git rebase -i to remove that commit. First, find out how far back that commit is (approximately). Then do:

git rebase -i HEAD~N

The ~N means rebase the last N commits (N must be a number, for example HEAD~10). Then, you can edit the file that Git presents to you to delete the offending commit. On saving that file, Git will then rewrite all the following commits as if the one you deleted didn't exist.

The Git Book has a good section on rebasing with pictures and examples.

Be careful with this though, because if you change something that you have pushed elsewhere, another approach will be needed unless you are planning to do a force push.

淡水深流 2024-08-10 06:00:28

另一种可能性是我个人最喜欢的命令之一:

git rebase -i <commit>~1

这将在您想要破坏的提交之前以交互模式 -i 启动变基。编辑器将开始列出此后的所有提交。删除包含要删除的提交的行并保存文件。 Rebase 将完成其余的工作,仅删除该提交,并将所有其他提交重放回日志中。

Another possibility is one of my personal favorite commands:

git rebase -i <commit>~1

This will start the rebase in interactive mode -i at the point just before the commit you want to whack. The editor will start up listing all of the commits since then. Delete the line containing the commit you want to obliterate and save the file. Rebase will do the rest of the work, deleting only that commit, and replaying all of the others back into the log.

倚栏听风 2024-08-10 06:00:28

我附加这个答案是因为我不明白为什么任何刚刚尝试提交工作的人都会因为使用 Git 时出现一些错误而想要删除所有这些工作!

如果你想保留你的工作并且只需“撤消”该提交命令(您在推送到存储库之前捕获):

git reset --soft HEAD~1

不要使用 --hard 标志,除非您想销毁自上次提交以来正在进行的工作。

I'm appending this answer because I don't see why anyone who has just tried to commit work would want to delete all that work because of some mistake using Git!

If you want to keep your work and just 'undo' that commit command (you caught before pushing to repo):

git reset --soft HEAD~1

Do not use the --hard flag unless you want to destroy your work in progress since the last commit.

〃温暖了心ぐ 2024-08-10 06:00:28

删除整个提交

git rebase -p --onto SHA^ SHA

显然将“SHA”替换为您要删除的引用。该命令中的“^”是字面意思。

http://sethrobertson.github.io/GitFixUm/fixup.html#change_deep


请注意 -p 已被弃用,请改写 --rebase-merges,请参阅答案下方的注释和 git-rebase - 在另一个基本提示之上重新应用提交 ==> --保留合并

-p
--preserve-merges [已弃用:使用 --rebase-merges 代替] 重新创建合并提交,而不是通过重放提交来展平历史记录
合并提交介绍。合并冲突解决方案或手动
对合并提交的修改不会被保留。

这在内部使用了 --interactive 机制,但将其组合起来
明确使用 --interactive 选项通常不是一个好主意
除非你知道自己在做什么(参见下面的错误)。

另请参阅下面的不兼容选项。

以及新参数 git-rebase - 在另一个基本提示之上重新应用提交==> --rebase-merges[=(rebase-cousins|no-rebase-cousins)]:

-r
--rebase-merges[=(rebase-cousins|no-rebase-cousins)] 默认情况下,rebase 只会从待办事项列表中删除合并提交,并将
rebased 提交到单个线性分支。使用--rebase-merges,
相反,变基将尝试保留内部的分支结构
通过重新创建合并提交来重新确定基础的提交。
这些合并中任何已解决的合并冲突或手动修改
提交必须手动解决/重新应用。

默认情况下,或者当指定 no-rebase-cousins 时,提交会执行以下操作
没有直系祖先将保留其原始分支
点,即将被 git-log[1] 排除的提交
--ancestry-path 选项将默认保留其原始祖先。如果开启了 rebase-cousins 模式,则此类提交会被替换
重新基于(或 ,如果指定)。

--rebase-merges 模式在本质上与已弃用的模式类似
--preserve-merges 但适用于交互式变基,其中提交可以随意重新排序、插入和删除。

目前只能使用以下命令重新创建合并提交
递归合并策略;只能使用不同的合并策略
通过显式 exec git merge -s [...] 命令。

另请参阅下面的“重定合并基础”和“不兼容选项”。

Removing an entire commit

git rebase -p --onto SHA^ SHA

Obviously replace "SHA" with the reference you want to get rid of. The "^" in that command is literal.

http://sethrobertson.github.io/GitFixUm/fixup.html#change_deep


Mind that -p has been deprecated, write --rebase-merges instead, see the remark below the answer and the link at git-rebase - Reapply commits on top of another base tip ==> --preserve-merges:

-p
--preserve-merges [DEPRECATED: use --rebase-merges instead] Recreate merge commits instead of flattening the history by replaying commits a
merge commit introduces. Merge conflict resolutions or manual
amendments to merge commits are not preserved.

This uses the --interactive machinery internally, but combining it
with the --interactive option explicitly is generally not a good idea
unless you know what you are doing (see BUGS below).

See also INCOMPATIBLE OPTIONS below.

And the new parameter git-rebase - Reapply commits on top of another base tip ==> --rebase-merges[=(rebase-cousins|no-rebase-cousins)]:

-r
--rebase-merges[=(rebase-cousins|no-rebase-cousins)] By default, a rebase will simply drop merge commits from the todo list, and put the
rebased commits into a single, linear branch. With --rebase-merges,
the rebase will instead try to preserve the branching structure within
the commits that are to be rebased, by recreating the merge commits.
Any resolved merge conflicts or manual amendments in these merge
commits will have to be resolved/re-applied manually.

By default, or when no-rebase-cousins was specified, commits which do
not have as direct ancestor will keep their original branch
point, i.e. commits that would be excluded by git-log[1]'s
--ancestry-path option will keep their original ancestry by default. If the rebase-cousins mode is turned on, such commits are instead
rebased onto (or , if specified).

The --rebase-merges mode is similar in spirit to the deprecated
--preserve-merges but works with interactive rebases, where commits can be reordered, inserted and dropped at will.

It is currently only possible to recreate the merge commits using the
recursive merge strategy; Different merge strategies can be used only
via explicit exec git merge -s [...] commands.

See also REBASING MERGES and INCOMPATIBLE OPTIONS below.

画中仙 2024-08-10 06:00:28

假设我们要删除提交 2 和 2 4 来自仓库。 (提交的数字越大;0 是最旧的提交,4 是最新的提交)

commit 0 : b3d92c5
commit 1 : 2c6a45b
commit 2 : <any_hash>
commit 3 : 77b9b82
commit 4 : <any_hash>

注意:您需要对存储库拥有管理员权限,因为您正在使用 --hard< /code> 和 -f

  • git checkout b3d92c5 签出最后一个可用的提交。
  • git checkout -b Repair 创建一个新的分支来工作。
  • gitcherry-pick 77b9b82 运行提交 3。
  • gitcherry-pick 2c6a45b 运行提交 1。
  • git checkout master 签出 master。
  • git reset --hard b3d92c5 将 master 重置为上次可用的提交。
  • git merge Repair 将我们的新分支合并到 master 上。
  • git push -f origin master 将 master 推送到远程仓库。

Say we want to remove commits 2 & 4 from the repo. (Higher the the number newer the commit; 0 is the oldest commit and 4 is the latest commit)

commit 0 : b3d92c5
commit 1 : 2c6a45b
commit 2 : <any_hash>
commit 3 : 77b9b82
commit 4 : <any_hash>

Note: You need to have admin rights over the repo since you are using --hard and -f.

  • git checkout b3d92c5 Checkout the last usable commit.
  • git checkout -b repair Create a new branch to work on.
  • git cherry-pick 77b9b82 Run through commit 3.
  • git cherry-pick 2c6a45b Run through commit 1.
  • git checkout master Checkout master.
  • git reset --hard b3d92c5 Reset master to last usable commit.
  • git merge repair Merge our new branch onto master.
  • git push -f origin master Push master to the remote repo.
属性 2024-08-10 06:00:28
git rebase -i HEAD~2

这里的“2”是您想要变基的提交数量。

'git rebase -i HEAD`

如果你想重新设定所有提交的基础。

然后您将能够选择这些选项之一。

p, pick = use commit

r, reword = use commit, but edit the commit message
e, edit = use commit, but stop for amending
s, squash = use commit, but meld into previous commit
f, fixup = like "squash", but discard this commit's log message
x, exec = run command (the rest of the line) using shell
d, drop = remove commit

这些行可以重新排序;它们是从上到下执行的。
如果您在此处删除一行,则提交将丢失。
但是,如果删除所有内容,变基将会中止。
请注意,空提交已被注释掉,

您可以使用选项“d”或删除包含您的提交的行来简单地删除该提交。

git rebase -i HEAD~2

Here '2' is the number of commits you want to rebase.

'git rebase -i HEAD`

if you want to rebase all the commits.

Then you will be able to choose one of these options.

p, pick = use commit

r, reword = use commit, but edit the commit message
e, edit = use commit, but stop for amending
s, squash = use commit, but meld into previous commit
f, fixup = like "squash", but discard this commit's log message
x, exec = run command (the rest of the line) using shell
d, drop = remove commit

These lines can be re-ordered; they are executed from top to bottom.
If you remove a line here THAT COMMIT WILL BE LOST.
However, if you remove everything, the rebase will be aborted.
Note that empty commits are commented out

You can simply remove that commit using option "d" or Removing a line that has your commit.

白日梦 2024-08-10 06:00:28

#[快速解答]

您有很多替代方案,例如:

  • 替代方案1:

     git rebase -i ~1
    

    <块引用>

    将 YourCommitId 更改为您要恢复到的提交编号。

  • 替代方案 2:

     git reset --hard YourCommitId
     git push <来源> <分支> - 力量
    

    <块引用>

    将 YourCommitId 更改为您要恢复到的提交编号。


    <块引用>

    我不推荐此选项,因为您可能会丢失正在进行的工作。

  • 替代方案 3:

    <前><代码> git reset --soft HEAD~1

    <块引用>

    您可以保留您的工作,仅撤消提交。

#[Quick Answer]

You have many alternatives, for example:

  • Alternative 1:

     git rebase -i <YourCommitId>~1
    

    Change YourCommitId for the number of the commit which you want to revert back to.

  • Alternative 2:

     git reset --hard YourCommitId
     git push <origin> <branch> --force
    

    Change YourCommitId for the number of the commit which you want to revert back to.

    I don't recommend this option because you may lose your in progress work.

  • Alternative 3:

     git reset --soft HEAD~1
    

    You can keep your work and only undo the commit.

吻风 2024-08-10 06:00:28

如果您没有发布更改,则可以删除最新提交

$ git reset --hard HEAD^

(请注意,这也会删除所有未提交的更改;请小心使用)。

如果您已经发布了要删除的提交,请使用 git revert

$ git revert HEAD

If you didn't publish changes, to remove latest commit, you can do

$ git reset --hard HEAD^

(note that this would also remove all uncommitted changes; use with care).

If you already published to-be-deleted commit, use git revert

$ git revert HEAD
离笑几人歌 2024-08-10 06:00:28

强制更改历史记录

假设您不仅想删除最后一次提交,还想删除最后 n 次提交中的特定提交,请使用:

git rebase -i HEAD~git rebase -i HEAD~git rebase -i HEAD~git rebase -i HEAD~<要返回的提交数>,因此如果您想查看最后五个提交,请使用git rebase -i HEAD~5

然后在文本编辑器中将要删除的每个提交旁边的单词 pick 更改为 drop。保存并退出编辑器。瞧!

附加更改历史记录

尝试git revert 还原将创建一个提交撤消指定的提交。

Forcefully Change History

Assuming you don't just want to delete the last commit, but you want to delete specific commits of the last n commits, go with:

git rebase -i HEAD~<number of commits to go back>, so git rebase -i HEAD~5 if you want to see the last five commits.

Then in the text editor change the word pick to drop next to every commit you would like to remove. Save and quit the editor. Voila!

Additively Change History

Try git revert <commit hash>. Revert will create a new commit that undoes the specified commit.

爱人如己 2024-08-10 06:00:28
git reset --hard commitId

git push <origin> <branch> --force

PS:CommitId指的是你想要恢复到的那个

git reset --hard commitId

git push <origin> <branch> --force

PS: CommitId refers the one which you want to revert back to

So尛奶瓶 2024-08-10 06:00:28

如果您想修复最新的提交,您可以撤消该提交,并取消暂存其中的文件,方法是:

git reset HEAD~1

这会将您的存储库返回到暂存文件的 git add 命令之前的状态。您的更改将位于您的工作目录中。 HEAD~1 指的是分支当前尖端下方的提交。

如果您想取消提交 N 次提交,但将代码更改保留在工作目录中:

git reset HEAD~N

如果您想删除最新的提交,并且不想保留代码更改,则可以执行“硬”重置。

git reset --hard HEAD~1

同样,如果您想放弃最后 N 次提交,并且不想保留代码更改:

git reset --hard HEAD~N

If you want to fix up your latest commit, you can undo the commit, and unstage the files in it, by doing:

git reset HEAD~1

This will return your repository to its state before the git add commands that staged the files. Your changes will be in your working directory. HEAD~1 refers to the commit below the current tip of the branch.

If you want to uncommit N commits, but keep the code changes in your working directory:

git reset HEAD~N

If you want to get rid of your latest commit, and do not want to keep the code changes, you can do a "hard" reset.

git reset --hard HEAD~1

Likewise, if you want to discard the last N commits, and do not want to keep the code changes:

git reset --hard HEAD~N
一紙繁鸢 2024-08-10 06:00:28

如果你想删除当前所在的提交但不想删除数据 -

git reset --soft HEAD~1

如果你想删除当前所在的提交并希望删除相关数据 -

git reset --hard HEAD~1

PS:这些命令只会删除本地提交。

If you want to delete the commit you are currently on but don't want to delete the data-

git reset --soft HEAD~1

If you want to delete the commit you are currently on and want to delete the related data-

git reset --hard HEAD~1

PS: these commands will only delete the local commits.

临走之时 2024-08-10 06:00:28

要在本地分支中删除,请使用

git reset --hard HEAD~1

要在远程分支中删除,请使用

git push origin HEAD --force

To delete in local branch, use

git reset --hard HEAD~1

To delete in a remote branch, use

git push origin HEAD --force
沧笙踏歌 2024-08-10 06:00:28

来源:https://gist.github.com/sagarjethi/c07723b2f4fa74ad8bdf229166cf79d8

删除最后一次提交

例如您的最后一次提交

git push origin +aa61ab32^:master

现在您想要删除此提交,然后按照以下

步骤

  1. 执行此操作的简单方法

    首先将分支重置为当前提交的父级

  2. 将其强制推送到远程。

git Reset HEAD^ --hard

git推送原点-f

对于特定提交,您想要重置如下

git reset bb676878^ --hard

git push origin -f

Source: https://gist.github.com/sagarjethi/c07723b2f4fa74ad8bdf229166cf79d8

Delete the last commit

For example your last commit

git push origin +aa61ab32^:master

Now you want to delete this commit then an Easy way to do this following

Steps

  1. First reset the branch to the parent of the current commit

  2. Force-push it to the remote.

git reset HEAD^ --hard

git push origin -f

For particular commit, you want to reset is following

git reset bb676878^ --hard

git push origin -f
轻许诺言 2024-08-10 06:00:28

如果您是 IntelliJ 用户,那么该界面简直太棒了。只需单击,您就可以立即看到效果。此位置的快捷方式:MacOS 上的 Cmd + 9

输入图像描述这里

If you're an IntelliJ user, the interface is simply awesome. With a single click, you can see the effect immediately at the same time. Shortcut to this place: Cmd + 9 on MacOS.

enter image description here

素染倾城色 2024-08-10 06:00:28

这里我只是发布一个清晰的管道来执行此操作

步骤 1:使用 git log 获取提交 ID。

git log

输入图像描述这里

Step2:使用 git reset 返回到之前的版本:

git reset --hard <your commit id>

Here I just post one clear pipeline to do so

Step1: Use git log to get the commit ID.

git log

enter image description here

Step2: Use git reset to go back to the former version:

git reset --hard <your commit id>
内心激荡 2024-08-10 06:00:28

上面的所有命令都会将工作树和索引的状态恢复为提交之前的状态,但不会恢复存储库的状态。如果你看一下,“删除”的提交实际上并没有被删除,它根本就不是当前分支尖端的提交。

我认为没有办法使用瓷器命令删除提交。唯一的方法是将其从日志和引用日志中删除,然后执行 git prune --expire -now 。

All the commands above restore the state of your work tree and index as they were before making the commit, but do not restore the state of the repository. If you look at it, the "removed" commit is not actually removed, it is simply not the one on the tip of the current branch.

I think that there are no means to remove a commit with porcelain commands. The only way is to remove it from the log and reflog and then to execute a git prune --expire -now.

别理我 2024-08-10 06:00:28

如果你想保留历史记录,显示提交和恢复,你应该使用:

git revert GIT_COMMIT_HASH

输入消息解释你为什么要恢复,然后:

git push  

当你发出git log时,你会看到“错误” " 提交并恢复日志消息。

If you want to keep the history, showing the commit and the revert, you should use:

git revert GIT_COMMIT_HASH

enter the message explaining why are you reverting and then:

git push  

When you issue git log you'll see both the "wrong" commit and revert log messages.

江南烟雨〆相思醉 2024-08-10 06:00:28

这是另一种方法:

签出您想要恢复的分支,然后将本地工作副本重置回您想要成为远程服务器上最新版本的提交(之后的所有内容都将成为再见)。为此,我在 SourceTree 中右键单击并选择“将 BRANCHNAME 重置为此提交”。我认为命令行是:

git reset --hard COMMIT_ID

由于您刚刚从远程签出分支,因此您不会担心丢失任何本地更改。但如果你这样做的话就会失去他们。

然后导航到存储库的本地目录并运行以下命令:

git -c diff.mnemonicprefix=false -c core.quotepath=false \
push -v -f --tags REPOSITORY_NAME BRANCHNAME:BRANCHNAME

这将删除本地存储库中当前提交之后的所有提交,但仅限于该分支。

Here's another way to do this:

Checkout the branch you want to revert, then reset your local working copy back to the commit that you want to be the latest one on the remote server (everything after it will go bye-bye). To do this, in SourceTree I right-clicked on the and selected "Reset BRANCHNAME to this commit". I think the command line is:

git reset --hard COMMIT_ID

Since you just checked out your branch from remote, you're not going to have any local changes to worry about losing. But this would lose them if you did.

Then navigate to your repository's local directory and run this command:

git -c diff.mnemonicprefix=false -c core.quotepath=false \
push -v -f --tags REPOSITORY_NAME BRANCHNAME:BRANCHNAME

This will erase all commits after the current one in your local repository but only for that one branch.

笨笨の傻瓜 2024-08-10 06:00:28

错误:

我 git rebase -i --root 编辑了我的分支,无知地认为我可以重写与主版本不同的第一个提交(Windows 版 GitHub 默认视图是与大师的比较,隐藏其全部)。

当 900 多个提交加载到 Sublime 中时,我留起了硅谷胡须。退出时没有进行任何更改,我给电池充电,然后继续剃须,因为所有 900 多个个人都漫不经心地重新提交了基础 - 将他们的提交时间重置为现在。

决心击败 Git 并保留原始时代,我删除了这个本地存储库并从远程重新克隆。

现在它已重新添加了我希望删除的最新不需要的提交,因此继续进行。

用尽选项:

我不希望 git revert - 它会创建一个额外的提交,让 Git 占据上风。

git reset --hard HEAD 什么也没做,在检查了 reflog 后,最后一个也是唯一的 HEAD 是克隆 - Git 获胜。

为了获取最新的 SHA,我检查了 github.com 上的远程存储库 -minor win。

在考虑 git reset --hard 有效后,我将另一个分支更新为 master 并 1...2...噗!提交回来了——Git 获胜。

回来查看 master,是时候尝试 git rebase -i ,然后删除该行...但无济于事,遗憾的是。 “如果您在此处删除一行,则提交将会丢失”。啊...掩盖了 troll the n00b >2.8.3 发行说明

解决方案:

git rebase -i 然后d, drop = remove commit

为了验证,我检查了另一个分支,瞧 - 没有隐藏从主服务器获取/拉取的提交。

https://twitter.com/holman/status/706006896273063936

祝你有美好的一天。

The mistake:

I git rebase -i --root'ed my branch, ignorantly thinking I could reword the first commit differing from the master (the GitHub for Windows default view is the comparison to master, hiding it's entirety).

I grew a Silicon Valley beard while 900+ commits loaded themselves into Sublime. Exiting with no changes, I charged my battery then proceeded to shave, as all 900+ individual commits nonchalantly rebased - resetting their commit times to now.

Determined to beat Git and preserve the original times, I deleted this local repository and re-cloned from the remote.

Now it had re-added a most recent unneeded commit to master I wished to remove, so proceeded like so.

Exhausting the options:

I didn't wish to git revert - it would create an additional commit, giving Git the upper hand.

git reset --hard HEAD did nothing, after checking the reflog, the last and only HEAD was the clone - Git wins.

To get the most recent SHA, I checked the remote repository on github.com - minor win.

After thinking git reset --hard <SHA> had worked, I updated another branch to master and 1... 2... poof! the commit was back - Git wins.

Checking back out to master, time to try git rebase -i <SHA>, then remove the line... to no avail, sad to say. "If you remove a line here THAT COMMIT WILL BE LOST". Ah...glossed over new feature troll the n00b in the 2.8.3 release notes.

The solution:

git rebase -i <SHA> then d, drop = remove commit.

To verify, I checked out to another branch, and voila - no hiding commit to fetch/pull from the master.

https://twitter.com/holman/status/706006896273063936

Good day to you.

┾廆蒐ゝ 2024-08-10 06:00:28

如果您只是弄乱了上次提交(错误消息,忘记添加一些更改)并且想在将其推送到公共存储库之前修复它,为什么不使用:

git commit --amend -m "New message here"

如果您有新的暂存更改,它们将与上次提交合并(您试图摆脱的)并将替换该提交。

当然,如果您在推送后修改提交,您将重写历史,因此如果您这样做,请务必了解其含义。

如果您希望使用先前提交的消息,您还可以传递“--no-edit”选项而不是“-m”。

文件:
http://git-scm.com/docs/git-commit.html

If you just messed up your last commit (wrong message, forgot to add some changes) and want to fix it before pushing it to a public repo why not use:

git commit --amend -m "New message here"

If you have newly staged changes they'll be combined with the last commit (that you're trying to get rid of) and will replace that commit.

Of course if you amend a commit after you've pushed it, you're rewriting history so if you do that be sure to understand the implications.

You can also pass the '--no-edit' option instead of '-m' if you would prefer to use the previous commit's message.

Docs:
http://git-scm.com/docs/git-commit.html

潇烟暮雨 2024-08-10 06:00:28

如果您已经推送,请首先在 HEAD ($GIT_COMMIT_HASH_HERE) 中找到您想要的提交,然后运行以下命令:

git reset --hard $GIT_COMMIT_HASH_HERE
git push origin HEAD --force

然后在存储库已克隆的每个位置运行:

git reset --hard origin/master

If you've already pushed, first find the commit you want to be at HEAD ($GIT_COMMIT_HASH_HERE), then run the following:

git reset --hard $GIT_COMMIT_HASH_HERE
git push origin HEAD --force

Then each place the repo has been cloned, run:

git reset --hard origin/master
爱本泡沫多脆弱 2024-08-10 06:00:28

当我提交和推送时我通常会做什么(如果有人推送他的提交就可以解决问题):

git reset --hard HEAD~1

git push -f origin

希望这有帮助

What I do usually when I commit and push (if anyone pushed his commit this solve the problem):

git reset --hard HEAD~1

git push -f origin

hope this help

猫烠⑼条掵仅有一顆心 2024-08-10 06:00:28

git reset --hard HEAD~1

您现在将处于先前的位置。拉树枝。推送新代码。提交将从 git 中删除

git reset --hard HEAD~1

You will be now at previous head. Pull the branch. Push new code. Commit will be removed from git

撧情箌佬 2024-08-10 06:00:28

在本地分支上重置

git reset --hard HEAD~<Number of commit> So git reset --hard HEAD~3

强制推送到原点

git push -f origin

Reset on local branch

git reset --hard HEAD~<Number of commit> So git reset --hard HEAD~3

Force push to origin

git push -f origin
梦在深巷 2024-08-10 06:00:28

我已经推过了。需要远程返回一些提交。
尝试过很多变体,
但只有这个来自Justin< /a> 通过 git Bush 对我来说工作得很好:

git reset --hard $GIT_COMMIT_HASH_HERE
git push origin HEAD --force

I have already pushed. Need to return some commits back remotly.
Have tried many variations,
but only this from Justin via git bush is working fine for me:

git reset --hard $GIT_COMMIT_HASH_HERE
git push origin HEAD --force
指尖上得阳光 2024-08-10 06:00:28
// display git commit log    
$ git log --pretty=oneline --abbrev-commit

// show last two commit and open in your default editor
// then delete second commit line and save it
$ git rebase -i HEAD~2

参考:如何删除在 git 中提交,本地和远程

// display git commit log    
$ git log --pretty=oneline --abbrev-commit

// show last two commit and open in your default editor
// then delete second commit line and save it
$ git rebase -i HEAD~2

Reference: How to delete a commit in git, local and remote

放飞的风筝 2024-08-10 06:00:28

使用 IntelliJ drop commit 作为最简单、最直接的解决方案之一。
您可以从左下角访问它:
访问 git 窗口在 IntelliJ

然后你可以选择提交并右键单击它你将得到这个菜单:
形成菜单
然后你可以选择:删除提交选项。

Use IntelliJ drop commit as one of the easiest and most straightforward solution.
You can access it from the bottom left corner:
access git window in IntelliJ

Then you can select the commit and right click on it you will get this menu:
form the menu
Then you can choose: the drop commit option.

墨离汐 2024-08-10 06:00:28

将代码备份到临时文件夹中。以下命令将重置与服务器相同。

git reset --hard HEAD
git clean -f
git pull

如果您想保留更改并删除最近的提交

git reset --soft HEAD^
git pull

Take backup of your code in to temp folder. Following command will reset same as server.

git reset --hard HEAD
git clean -f
git pull

If you want to keep your changes , and remove recent commits

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