如何从分支中删除提交?
如何从分支历史记录中删除提交?我应该使用 git reset --hard HEAD 吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何从分支历史记录中删除提交?我应该使用 git reset --hard HEAD 吗?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(30)
小心:
git reset --hard
将删除您的工作目录更改。在运行此命令之前,请务必存储您想要保留的任何本地更改。
假设您正在执行该提交,那么此命令将对其进行破坏...
HEAD~1
表示 head 之前的提交。或者,您可以查看 git log 的输出,找到要备份到的提交的提交 ID,然后执行以下操作:
如果您已经推送了它,则需要执行强制推送以摆脱它......
但是,如果其他人可能已经拉动它,那么你最好开始一个新分支。因为当他们拉动时,它只会将其合并到他们的工作中,然后你会再次将其推回原处。
如果您已经推送,最好使用 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...
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:If you already pushed it, you will need to do a force push to get rid of it...
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 usinggit stash list
andgit 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.如果您尚未将提交推送到任何地方,可以使用
git rebase -i
删除该提交。首先,找出该提交距离(大约)有多远。然后执行以下操作:~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:The
~N
means rebase the lastN
commits (N
must be a number, for exampleHEAD~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.
另一种可能性是我个人最喜欢的命令之一:
这将在您想要破坏的提交之前以交互模式
-i
启动变基。编辑器将开始列出此后的所有提交。删除包含要删除的提交的行并保存文件。 Rebase 将完成其余的工作,仅删除该提交,并将所有其他提交重放回日志中。Another possibility is one of my personal favorite commands:
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.我附加这个答案是因为我不明白为什么任何刚刚尝试提交工作的人都会因为使用 Git 时出现一些错误而想要删除所有这些工作!
如果你想保留你的工作并且只需“撤消”该提交命令(您在推送到存储库之前捕获):
不要使用 --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):
Do not use the --hard flag unless you want to destroy your work in progress since the last commit.
删除整个提交
显然将“SHA”替换为您要删除的引用。该命令中的“^”是字面意思。
http://sethrobertson.github.io/GitFixUm/fixup.html#change_deep
请注意
-p
已被弃用,请改写--rebase-merges
,请参阅答案下方的注释和 git-rebase - 在另一个基本提示之上重新应用提交 ==> --保留合并:以及新参数 git-rebase - 在另一个基本提示之上重新应用提交==> --rebase-merges[=(rebase-cousins|no-rebase-cousins)]:
Removing an entire commit
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:And the new parameter git-rebase - Reapply commits on top of another base tip ==> --rebase-merges[=(rebase-cousins|no-rebase-cousins)]:
假设我们要删除提交 2 和 2 4 来自仓库。 (提交的数字越大;0 是最旧的提交,4 是最新的提交)
注意:您需要对存储库拥有管理员权限,因为您正在使用
--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)
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.这里的“2”是您想要变基的提交数量。
如果你想重新设定所有提交的基础。
然后您将能够选择这些选项之一。
p, pick = use commit
这些行可以重新排序;它们是从上到下执行的。
如果您在此处删除一行,则提交将丢失。
但是,如果删除所有内容,变基将会中止。
请注意,空提交已被注释掉,
您可以使用选项“d”或删除包含您的提交的行来简单地删除该提交。
Here '2' is the number of commits you want to rebase.
if you want to rebase all the commits.
Then you will be able to choose one of these options.
p, pick = use 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.
#[快速解答]
您有很多替代方案,例如:
替代方案1:
<块引用>
将 YourCommitId 更改为您要恢复到的提交编号。
替代方案 2:
<块引用>
将 YourCommitId 更改为您要恢复到的提交编号。
<块引用>
我不推荐此选项,因为您可能会丢失正在进行的工作。
替代方案 3:
<前><代码> git reset --soft HEAD~1
<块引用>
您可以保留您的工作,仅撤消提交。
#[Quick Answer]
You have many alternatives, for example:
Alternative 1:
Alternative 2:
Alternative 3:
如果您没有发布更改,则可以删除最新提交
(请注意,这也会删除所有未提交的更改;请小心使用)。
如果您已经发布了要删除的提交,请使用 git revert
If you didn't publish changes, to remove latest commit, you can do
(note that this would also remove all uncommitted changes; use with care).
If you already published to-be-deleted commit, use git revert
强制更改历史记录
假设您不仅想删除最后一次提交,还想删除最后 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>
, sogit rebase -i HEAD~5
if you want to see the last five commits.Then in the text editor change the word
pick
todrop
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.PS:CommitId指的是你想要恢复到的那个
PS: CommitId refers the one which you want to revert back to
如果您想修复最新的提交,您可以撤消该提交,并取消暂存其中的文件,方法是:
这会将您的存储库返回到暂存文件的 git add 命令之前的状态。您的更改将位于您的工作目录中。 HEAD~1 指的是分支当前尖端下方的提交。
如果您想取消提交 N 次提交,但将代码更改保留在工作目录中:
如果您想删除最新的提交,并且不想保留代码更改,则可以执行“硬”重置。
同样,如果您想放弃最后 N 次提交,并且不想保留代码更改:
If you want to fix up your latest commit, you can undo the commit, and unstage the files in it, by doing:
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:
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.
Likewise, if you want to discard the last N commits, and do not want to keep the code changes:
如果你想删除当前所在的提交但不想删除数据 -
如果你想删除当前所在的提交并希望删除相关数据 -
PS:这些命令只会删除本地提交。
If you want to delete the commit you are currently on but don't want to delete the data-
If you want to delete the commit you are currently on and want to delete the related data-
PS: these commands will only delete the local commits.
要在本地分支中删除,请使用
要在远程分支中删除,请使用
To delete in local branch, use
To delete in a remote branch, use
来源:https://gist.github.com/sagarjethi/c07723b2f4fa74ad8bdf229166cf79d8
删除最后一次提交
例如您的最后一次提交
git push origin +aa61ab32^:master
现在您想要删除此提交,然后按照以下
步骤
首先将分支重置为当前提交的父级
将其强制推送到远程。
对于特定提交,您想要重置如下
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
First reset the branch to the parent of the current commit
Force-push it to the remote.
For particular commit, you want to reset is following
如果您是 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.这里我只是发布一个清晰的管道来执行此操作
步骤 1:使用 git log 获取提交 ID。
Step2:使用 git reset 返回到之前的版本:
Here I just post one clear pipeline to do so
Step1: Use git log to get the commit ID.
Step2: Use git reset to go back to the former version:
上面的所有命令都会将工作树和索引的状态恢复为提交之前的状态,但不会恢复存储库的状态。如果你看一下,“删除”的提交实际上并没有被删除,它根本就不是当前分支尖端的提交。
我认为没有办法使用瓷器命令删除提交。唯一的方法是将其从日志和引用日志中删除,然后执行 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
.如果你想保留历史记录,显示提交和恢复,你应该使用:
输入消息解释你为什么要恢复,然后:
当你发出
git log
时,你会看到“错误” " 提交并恢复日志消息。If you want to keep the history, showing the commit and the revert, you should use:
enter the message explaining why are you reverting and then:
When you issue
git log
you'll see both the "wrong" commit and revert log messages.这是另一种方法:
签出您想要恢复的分支,然后将本地工作副本重置回您想要成为远程服务器上最新版本的提交(之后的所有内容都将成为再见)。为此,我在 SourceTree 中右键单击并选择“将 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:
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:
This will erase all commits after the current one in your local repository but only for that one branch.
错误:
我 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 thereflog
, the last and onlyHEAD
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>
thend, 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.
如果您只是弄乱了上次提交(错误消息,忘记添加一些更改)并且想在将其推送到公共存储库之前修复它,为什么不使用:
如果您有新的暂存更改,它们将与上次提交合并(您试图摆脱的)并将替换该提交。
当然,如果您在推送后修改提交,您将重写历史,因此如果您这样做,请务必了解其含义。
如果您希望使用先前提交的消息,您还可以传递“--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:
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
如果您已经推送,请首先在 HEAD ($GIT_COMMIT_HASH_HERE) 中找到您想要的提交,然后运行以下命令:
然后在存储库已克隆的每个位置运行:
If you've already pushed, first find the commit you want to be at HEAD ($GIT_COMMIT_HASH_HERE), then run the following:
Then each place the repo has been cloned, run:
当我提交和推送时我通常会做什么(如果有人推送他的提交就可以解决问题):
希望这有帮助
What I do usually when I commit and push (if anyone pushed his commit this solve the problem):
hope this help
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
在本地分支上重置
强制推送到原点
Reset on local branch
Force push to origin
我已经推过了。需要远程返回一些提交。
尝试过很多变体,
但只有这个来自Justin< /a> 通过 git Bush 对我来说工作得很好:
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 中提交,本地和远程
Reference: How to delete a commit in git, local and remote
使用 IntelliJ drop commit 作为最简单、最直接的解决方案之一。
您可以从左下角访问它:
然后你可以选择提交并右键单击它你将得到这个菜单:
然后你可以选择:删除提交选项。
Use IntelliJ drop commit as one of the easiest and most straightforward solution.
You can access it from the bottom left corner:
Then you can select the commit and right click on it you will get this menu:
Then you can choose: the drop commit option.
将代码备份到临时文件夹中。以下命令将重置与服务器相同。
如果您想保留更改并删除最近的提交
Take backup of your code in to temp folder. Following command will reset same as server.
If you want to keep your changes , and remove recent commits