通过 1 次提交回滚本地和远程 git 存储库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4647301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:49:04  来源:igfitidea点击:

Rolling back local and remote git repository by 1 commit

git

提问by Jamis Charles

I've read the similar posts on this topic, and can't for the life of me figure out how to do this properly.

我已经阅读了有关此主题的类似帖子,但终生无法弄清楚如何正确执行此操作。

I checked in about 1000 files that I don't want, and I'd rather not have to go through 1by1 and remove them all from the repo.

我检入了大约 1000 个我不想要的文件,我宁愿不必通过 1by1 并将它们从 repo 中全部删除。

  • I have a remote masterBranch.
  • I have the local masterBranch.
  • 我有一个远程master分支。
  • 我有当地的master分公司。

They are both at the same revision.

它们都在同一修订版中。

I want to rollback my remote by 1 commit.

我想通过 1 次提交回滚我的遥控器。

Say my history on masteris A--B--C--D--E.
I want to rollback my local to D.
Then push it to remote so my current hash will be D both remote and local.

说我的历史masterA--B--C--D--E
我想将我的本地回滚到D.
然后将它推送到远程,这样我当前的哈希将是远程和本地的 D。

I'm having issues doing this.
I'm using Git Tower but am comfortable with the command line. Any help?

我在做这件事时遇到了问题。
我正在使用 Git Tower,但对命令行很满意。有什么帮助吗?

UPDATE:Great comments below. Using a reset seems to be partially discouraged especially if the repository is shared with other users. What's the best way to undo the previous commit's changes without using a hard reset? Is there a way?

更新:下面的评论很棒。似乎部分不鼓励使用重置,特别是如果存储库与其他用户共享。 在不使用硬重置的情况下撤消先前提交的更改的最佳方法是什么?有办法吗?

回答by VonC

If nobody has pulled your remote repo yet, you can change your branch HEAD and force push it to said remote repo:

如果还没有人拉取您的远程仓库,您可以更改您的分支 HEAD 并强制将其推送到所述远程仓库:

git reset --hard HEAD^ 
git push -f 

(or, if you have direct access to the remote repo, you can change its HEAD reference even though it is a bare repo)

(或者,如果您可以直接访问远程仓库,即使它是一个裸仓库,您也可以更改其HEAD 引用

Note, as commented by alien-technologyin the comments below, on Windows (CMD session), you would need ^^:

请注意,正如外星人技术下面的评论所评论的那样,在 Windows(CMD 会话)上,您需要^^

git reset --hard HEAD^^
git push -f 

Update since 2011:
Using git push --force-with-lease(that I present here, introduced in 2013 with Git 1.8.5) is safer.

自 2011 年以来的更新:
使用git push --force-with-lease我在此介绍,于 2013 年随 Git 1.8.5 引入)更安全。

See Schwern's answerfor illustration.

有关说明,请参阅Schwern回答



What if somebody has already pulled the repo? What would I do then?

如果有人已经提取了回购怎么办?那我该怎么办?

Then I would suggest something that doesn't rewrite the history:

然后我会建议一些不会改写历史的东西:

  • git revertlocally your last commit (creating a new commit that reverses what the previous commit did)
  • push the 'revert' generated by git revert.
  • git revert在本地你的最后一次提交(创建一个新的提交来逆转之前的提交所做的事情)
  • 推送由git revert.

回答by eckes

Set the local branch one revision back (HEAD^means one revision back):

将本地分支设置回一版(HEAD^表示回一版):

git reset --hard HEAD^

Push the changes to origin:

将更改推送到原点:

git push --force

You will have to force pushing because otherwise git would recognize that you're behind originby one commit and nothing will change.

您将不得不强制推送,否则 git 会认识到您落后origin于一次提交,并且什么都不会改变。

Doing it with --forcetells git to overwrite HEADin the remote repo without respecting any advances there.

这样做--force告诉 gitHEAD在远程仓库中覆盖而不尊重那里的任何进展。

回答by Kannan S

If you want revert last commit listen:

如果您想恢复上次提交,请听:

Step 1:

第1步:

Check your local commits with messages

使用消息检查您的本地提交

$ git log

Step 2:

第2步:

Remove last commit without resetting the changes from local branch (or master)

删除上次提交而不重置本地分支(或主)的更改

$ git reset HEAD^

OR if you don't want last commit files and updates listens

或者,如果您不希望上次提交文件和更新侦听

$ git reset HEAD^ --hard

Step 3:

第 3 步:

We can update the files and codes and again need to push with force it will delete previous commit. It will keep new commit.

我们可以更新文件和代码,再次需要强制推送它会删除以前的提交。它将保持新的提交。

$ git push origin branch -f

That's it!

就是这样!

回答by sahilabrar

By entering bellow command you can see your git commit history -

通过输入波纹管命令,您可以看到您的 git 提交历史记录 -

$ git log

$ git 日志

Let's say your history on that particular branch is like - commit_A, commit_B, commit_C, commit_D. Where, commit_D is the last commit and this is where HEAD remains. Now, to remove your last commit from local and remote, you need to do the following :

假设您在该特定分支上的历史记录类似于 - commit_A、commit_B、commit_C、commit_D。其中,commit_D 是最后一次提交,这是 HEAD 保留的位置。现在,要从本地和远程删除最后一次提交,您需要执行以下操作:

Step 1: Remove last commit locally by -

第 1 步:通过 - 在本地删除最后一次提交

$ git reset --hard HEAD~

$ git reset --hard HEAD~

This will change your commit HEAD to commit_C

这会将您的提交 HEAD 更改为 commit_C

Step 2: Push your change for new HEAD commit to remote

第 2 步:将您对新 HEAD 提交的更改推送到远程

$ git push origin +HEAD

$ git push origin +HEAD

This command will delete the last commit from remote.

此命令将从远程删除最后一次提交。

P.S. this command is tested on Mac OSX and should work on other operating systems as well (not claiming about other OS though)

PS 这个命令在 Mac OSX 上测试过,也应该在其他操作系统上工作(虽然不声称其他操作系统)

回答by Anvesh Yalamarthy

For Windows Machines, use:

对于 Windows 机器,请使用:

git reset HEAD~1  #Remove Commit Locally

回答by Schwern

Here's an updated version of the procedure which is safer.

这是程序的更新版本,它更安全。

git reset --hard HEAD^ 
git push --force-with-lease

git push -fwill indiscriminately replace the remote repository with your own changes. If someone else has pushed changes they will be lost. git push --force-with-leasewill only push your rebase if the repository is as you expect. If someone else has already pushed your push will fail.

git push -f将不加选择地用您自己的更改替换远程存储库。如果其他人推送了更改,他们将丢失。git push --force-with-lease如果存储库符合您的预期,则只会推送您的变基。如果其他人已经推送了您的推送将失败。

See –force considered harmful; understanding git's –force-with-lease.

见——被认为有害的力;理解 git 的 –force-with-lease

I recommend aliasing this as repush = push --force-with-lease.

我建议将其别名为repush = push --force-with-lease.

What if somebody has already pulled the repo? What would I do then?

如果有人已经提取了回购怎么办?那我该怎么办?

Tell them to git pull --rebase=merges. Instead of a git fetch originand git merge origin/masterit will git fetch originand git rebase -r origin/master. This will rewrite any of their local changes to masteron top of the new rebased origin/master. -rwill preserve any merges they may have made.

告诉他们git pull --rebase=merges。而不是 a git fetch originandgit merge origin/master它将git fetch originand git rebase -r origin/master。这会将他们的任何本地更改重写到master新的 rebased 之上origin/master-r将保留他们可能进行的任何合并。

I recommend making this the default behavior for pulling. It is safe, will handle other's rebasing, and results in less unnecessary merges.

我建议将此作为拉动的默认行为。它是安全的,将处理其他人的变基,并减少不必要的合并。

[pull]
        rebase = merges

回答by Ibrohim Ermatov

I solved problem like yours by this commands:

我通过以下命令解决了像你这样的问题:

git reset --hard HEAD^
git push -f <remote> <local branch>:<remote branch> 

回答by minhas23

I just wanted to remove last commit from remote and clear commit history also. The following worked like a charm

我只想从远程和清除提交历史中删除最后一次提交。以下工作就像一个魅力

git reset --hard HEAD^ 
git push -f 

回答by ravi.zombie

The way to reset the head and do the revert to the previous commit is through

重置头部并恢复到先前提交的方法是通过

$ git reset HEAD^ --hard
$ git push <branchname> -f

But sometimes it might not be accepted in the remote branch:

但有时它可能不被远程分支接受:

To ssh:<git repo>
 ! [rejected]        develop -> develop (non-fast-forward)
error: failed to push some refs to 'ssh:<git repo>'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

then the other way to do is

那么另一种方法是

git revert HEAD
git push <remote branch>

This works fine.

这工作正常。

NOTE: remember if the git push -f <force>failed and then you try to revert. Do a git pullbefore, so that remote and local are in sync and then try git revert.
Check with git logto make sure the remote and local are at same point of commit with same SHA1..

注意:请记住,如果git push -f <force>失败,然后您尝试恢复。做一个git pull之前,让远程和本地同步,然后尝试git revert
检查git log以确保远程和本地使用相同的 SHA1 处于同一提交点。

git revert 
A --> B --> C -->D
A--> B --> C --> D --> ^D(taking out the changes and committing reverted diffs)

回答by Bhushan

on local master

在本地主

git reflog
-- this will list all last commit
  e.g Head@{0} -- wrong push
      Head@{1} -- correct push  
git checkout Head@{1} .
  -- this will reset your last modified files

git status 
git commit -m "reverted to last best"
git push origin/master

No need to worry if other has pulled or not.

无需担心其他人是否已拉动。

Done!

完毕!