简体   繁体   English

GIT 删除错误提交并推送到远程存储库

[英]GIT remove the bad commit and Push to the remote repository

A repository is created, and a calculator project is cloned in Python.创建了一个存储库,并在 Python 中克隆了一个计算器项目。

The file newfile.py has the code.文件 newfile.py 有代码。 This code is written in various commits.此代码是在各种提交中编写的。 In the previous commits, the calculator code worked well.在之前的提交中,计算器代码运行良好。 However, after certain commits, the calculator code displays a divide by zero exception.但是,在某些提交之后,计算器代码会显示除以零异常。 This is due to the change in line 2 of 'newfile.py'.这是由于“newfile.py”的第 2 行发生了变化。

Use cat newfile.py to read the contents of the file使用 cat newfile.py 读取文件内容

Use git log to check the previous commits.使用 git 日志检查之前的提交。

Use Git Bisect to find the commit in which line 2 is changed from 'b = 20' to 'b = 0.00000'.使用 Git Bisect 查找第 2 行从“b = 20”更改为“b = 0.00000”的提交。

Remove the bad commit by using Git Revert.使用 Git Revert 删除错误提交。 Leave the commit message as is.保持提交消息不变。

Push to the remote repository.推送到远程存储库。

My approach:我的做法:

I have identified the bad commit using:我已经使用以下方法确定了错误提交:

git bisect start git bisect good git bisect bad git 平分开始 git 平分好 git 平分坏

after few steps i have identified the bad commit Id which has caused the problem.经过几个步骤后,我确定了导致问题的错误提交 ID。

I have followed the below steps to remove the bad commit:我已按照以下步骤删除错误提交:

  1. git revert <> git 恢复 <>
  2. git bisect reset git 平分复位
  3. Need help in last step of [Push to the remote repository]?????在[推送到远程存储库]的最后一步需要帮助?????? How to do this last step Note: i have attached the repository shown in the attachement.如何执行此最后一步注意:我已附加附件中显示的存储库。

[Git console showing branch] [Git 控制台显示分支]

When you use git revert , you do not remove a commit.当您使用git revert时,您不会删除提交。 1 Instead, you add a new commit , specifically one whose effect is to undo the effect of a previous commit. 1相反,您添加一个新的提交,特别是一个其效果是撤消先前提交的效果的提交。 2 The consequence of this is that there is no need to force your push at all. 2这样做的结果是根本不需要强迫你推动。 Your new commit can be sent with an ordinary, non-forcing git push .您的新提交可以使用普通的、非强制的git push发送。

Remember that git push works by having your Git call up some other Git.请记住, git push通过让您的 Git 调用其他一些 Git 来工作。 Your Git and their Git then have a sort of conversation, where your Git announces to their Git that your Git has a new commit for them. Your Git and their Git then have a sort of conversation, where your Git announces to their Git that your Git has a new commit for them. If they have never seen this commit before, 3 they will ask your Git to send it.如果他们以前从未见过此提交, 3他们会要求您的 Git 发送它。

Then, having sent them your new revert commit, your Git asks their Git to set one of their branch names —the one you use in your git push command—to record this new commit as the latest commit in their branch.然后,在向他们发送了您的新回复提交后,您的 Git 会要求他们的 Git 设置他们的分支名称之一——您在git push中使用的分支名称中一个分支命令中的最新提交。 In other words, their master , or develop , or whatever name you use here, is their name to control however they like.换句话说,他们的masterdevelop或您在此处使用的任何名称,都是他们可以随意控制的名称。 You're just having your Git ask them to set their name to remember your new commit, instead of whatever commit it remembers right now.你只是让你的 Git 要求他们设置他们的名字来记住你的新提交,而不是它现在记住的任何提交。

If they agree to set that name as you request, your git push succeeds.如果他们同意按照您的要求设置该名称,则您的git push成功。 If they reject that request for some reason, then you have to examine why they rejected it, and figure out what they want instead.如果他们出于某种原因拒绝了该请求,那么您必须检查他们拒绝该请求的原因,并弄清楚他们想要什么。


1 In general, all commits are both read-only and permanent. 1通常,所有提交都是只读的和永久的。 It is possible to discard existing commits, but it is difficult and generally discouraged, with one particular set of exceptions: any commits that you have made, but not sent to anyone else—that is, you have not used git push to publish them for others to use—are yours and yours alone, so you can remove them without consequence.可以丢弃现有的提交,但是这很困难并且通常不鼓励,除了一组特定的例外:您已经做出但没有发送给其他任何人的任何提交 - 也就是说,您没有使用git push发布它们其他人使用——是你的,你自己的,所以你可以删除它们而不会产生任何后果。 The problem with removing published commits is that commits are a bit like viruses: bring your Git in contact with another Git that has that commit, and you get it again.删除已发布提交的问题在于,提交有点像病毒:让您的 Git 与另一个具有该提交的 Git 接触,然后您再次获得它。 So if you've published some commit, then you remove it, then you connect to any Git that has seen the published commit anywhere, well, now you have it again.因此,如果您已经发布了一些提交,那么您将其删除,然后您连接到任何已在任何地方看到已发布提交的 Git,好吧,现在您再次拥有它。

(Note that all commits are completely read-only. The unique hash ID for some commit is actually a cryptographic checksum of the contents of that commit, so if you extract a commit from a Git repository and change its contents and put that back, what you get is a new commit with a new and different hash ID. The existing commit remains in the repository, with its existing hash ID.) (请注意,所有提交都是完全只读的。某些提交的唯一 hash ID 实际上是该提交内容的加密校验和,因此,如果您从 Git 存储库中提取提交并更改其内容并将其放回去,那么你得到的是一个新的提交,它有一个新的和不同的 hash ID。现有的提交保留在存储库中,其现有的 hash ID。)

2 Note that reverting a merge commit doesn't undo the fact that the merge actually happened. 2请注意,还原合并提交并不能撤销合并实际发生的事实。 It only backs out code changes.它只支持代码更改。

3 Every commit has a unique number—a hash ID —that is reserved to that one particular commit, and every Git everywhere agrees that that hash ID goes with that commit. 3每个提交都有一个唯一的编号——一个hash ID——保留给那个特定的提交,并且每个Git 都同意hash ID 与提交一起使用。 So your Git need only present its commit number to the other Git.因此,您的 Git 只需将其提交号提供给另一个 Git。 The other Git checks its database-of-all-commits, and if it does not have that commit, says yes, send me that commit .另一个 Git 检查它的 database-of-all-commits,如果它没有那个 commit,说是的,把那个 commit 发给我 If it already has that commit, it says no need, I already have that commit .如果它已经有那个提交,它说不需要,我已经有那个提交 That's how your Git and their Git can figure out which commits you have, that they don't ( git push ), or which commits they have, that you don't ( git fetch ). That's how your Git and their Git can figure out which commits you have, that they don't ( git push ), or which commits they have, that you don't ( git fetch ).

$ git bisect start $ git 平分开始

$ git log --oneline $ git 日志 --oneline

display the gitlog history with prefix git commit id显示带有前缀 git 提交 id 的 gitlog 历史记录

$ git bisect good INITIAL COMMIT $ git bisect good INITIAL COMMIT

$ git bisect bad $ git 平分坏

this will display bad commit这将显示错误提交

$git bisect reset $git bisect 重置

$git revert BAD COMMIT $git 恢复错误提交

It will display a simple text file with commit message along with details.它将显示一个带有提交消息的简单文本文件以及详细信息。 leave as is.保持原样。 :wq :wq

$git push $git 推送

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM