git 将远程重置为某个提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5816688/
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 10:49:09  来源:igfitidea点击:

Resetting remote to a certain commit

git

提问by nacho4d

I want to discard all changes done after commit <commit-hash>. So I did:

我想放弃 commit 后所做的所有更改<commit-hash>。所以我做了:

git reset --hard <commit-hash>

Now I want to do the same with my remote. How can I do this? I have done some commits (and pushes) after <commit-hash>and I just want to discard them all. Is just something went terriblywrong in the way and I don't want to make it worse than it is already. ;(

现在我想对我的遥控器做同样的事情。我怎样才能做到这一点?之后我做了一些提交(和推送)<commit-hash>,我只想丢弃它们。只是出了点可怕的方式错了,我不想让它比它差了。;(

I basically want to rewind my origin/masterto <commit-hash>

我基本上想倒带我origin/master<commit-hash>

回答by Mark Longair

Assuming that your branch is called masterboth here and remotely, and that your remote is called originyou could do:

假设您的分支master在此处和远程都被调用,并且您的远程被调用,origin您可以执行以下操作:

 git reset --hard <commit-hash>
 git push -f origin master

However, you should avoid doing this if anyone else is working with your remote repository and has pulled your changes. In that case, it would be better to revertthe commits that you don't want, then push as normal.

但是,如果其他人正在使用您的远程存储库并已提取您的更改,则应避免这样做。在这种情况下,最好还原您不想要的提交,然后正常推送。

Update:you've explained below that other people have pulled the changes that you've pushed, so it's better to create a new commit that reverts all of those changes. There's a nice explanation of your options for doing this in this answer from Jakub Nar?bski. Which one is most convenient depends on how many commits you want to revert, and which method makes most sense to you.

更新:您在下面解释了其他人已经拉取了您推送的更改,因此最好创建一个新提交来还原所有这些更改。在Jakub Nar?bski 的这个答案中,对您执行此操作的选项有一个很好的解释。哪种方法最方便取决于您要还原多少次提交,以及哪种方法对您最有意义。

Since from your question it's clear that you have already used git reset --hardto reset your masterbranch, you may need to start by using git reset --hard ORIG_HEADto move your branch back to where it was before. (As always with git reset --hard, make sure that git statusis clean, that you're on the right branch and that you're aware of git reflogas a tool to recover apparently lost commits.) You should also check that ORIG_HEADpoints to the right commit, with git show ORIG_HEAD.

由于从您的问题中很明显您已经使用git reset --hard过重置master分支,因此您可能需要首先使用git reset --hard ORIG_HEAD将分支移回之前的位置。(与往常一样git reset --hard,请确保它git status是干净的,您位于正确的分支上,并且您知道这git reflog是一种恢复明显丢失提交的工具。)您还应该检查ORIG_HEAD指向正确提交的git show ORIG_HEAD.

Troubleshooting:

故障排除:

If you get a message like "! [remote rejected] a60f7d85 -> master (pre-receive hook declined)"

如果您收到类似“ ![远程拒绝] a60f7d85 -> master (pre-receive hook denied)”这样的消息

then you have to allow branch history rewriting for the specific branch. In BitBucket for example it said "Rewriting branch history is not allowed". There is a checkbox named Allow rewriting branch historywhich you have to check.

那么你必须允许特定分支的分支历史重写。例如在 BitBucket 中,它说“不允许重写分支历史记录”。有一个名为的复选框Allow rewriting branch history,您必须检查它。

回答by Walf

Use the other answers if you don't mind losing local changes.This method can still wreck your remote if you choose the wrong commit hash to go back to.

如果您不介意丢失本地更改,请使用其他答案。如果您选择错误的提交哈希返回,此方法仍然会破坏您的遥控器。

If you just want to make the remote match a commit that's anywhere in your local repo:

如果您只想使远程匹配本地存储库中任何位置的提交:

  1. Do notdo any resetting.
  2. Use git logto find the commit you want to the remote to be at. git log -pto see changes, or git log --graph --all --oneline --decorateto see a compact tree.
  3. Copy the commit's hash or its tag, or the name of its branch if it's the tip.
  4. Run a command like:

    git push --force <remote> <commit-ish>:<the remote branch>
    

    e.g.

    git push --force origin 606fdfaa33af1844c86f4267a136d4666e576cdc:master
    

    or

    git push --force staging v2.4.0b2:releases
    
  1. 千万不能做任何复位。
  2. 使用git log找到你想要的遥远,在提交。git log -p查看变化,或git log --graph --all --oneline --decorate查看紧凑的树。
  3. 复制提交的哈希或其标签,或者如果是提示,则复制其分支的名称。
  4. 运行如下命令:

    git push --force <remote> <commit-ish>:<the remote branch>
    

    例如

    git push --force origin 606fdfaa33af1844c86f4267a136d4666e576cdc:master
    

    或者

    git push --force staging v2.4.0b2:releases
    

I use convenient alias (git go) for viewing history as in step 2, which can be added like so:

git go在步骤 2 中使用方便的别名 ( ) 来查看历史记录,可以像这样添加:

    git config --global alias.go 'log --graph --all --decorate --oneline'`

回答by Ibrohim Ermatov

I solved problem like yours by this commands:

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

git reset --hard <commit-hash> 
git push -f <remote> <local branch>:<remote branch> 

回答by dudasaus

On GitLab, you may have to set your branch to unprotectedbefore doing this. You can do this in [repo] > Settings > Repository > Protected Branches. Then the method from Mark's answer works.

在 GitLab 上,您可能必须在执行此操作之前将您的分支设置为不受保护。您可以在 [repo] > Settings > Repository > Protected Branches 中执行此操作。然后马克的答案中的方法有效。

git reset --hard <commit-hash>
git push -f origin master

回答by priya khokher

If you want a previous version of file, I would recommend using git checkout.

如果您想要以前版本的文件,我建议使用 git checkout。

git checkout <commit-hash>

Doing this will send you back in time, it does not affect the current state of your project, you can come to mainline git checkout mainline

这样做会及时把你送回去,不影响你项目的当前状态,你可以来mainline git checkout mainline

but when you add a file in the argument, that file is brought back to you from a previous time to your current project time, i.e. your current project is changed and needs to be committed.

但是当您在参数中添加文件时,该文件会从前一个时间返回到您当前的项目时间,即您当前的项目已更改并需要提交。

git checkout <commit-hash> -- file_name
git add .
git commit -m 'file brought from previous time'
git push

The advantage of this is that it does not delete history, and neither does revert a particular code changes (git revert)

这样做的好处是它不会删除历史记录,也不会恢复特定的代码更改(git revert)

Check more here https://www.atlassian.com/git/tutorials/undoing-changes#git-checkout

在此处查看更多信息https://www.atlassian.com/git/tutorials/undoing-changes#git-checkout

回答by badbishop

My two cents to the previous answers: if

我对以前的回答只差两分钱:如果

git push --force <remote> <the-hash>:<the remote branch>

still doesn't work, you might want to edit <your-remote-repo>.git/configfile's receive section:

仍然不起作用,您可能想要编辑<your-remote-repo>.git/config文件的接收部分:

[receive]
  #denyNonFastforwards = true
  denyNonFastforwards = false

回答by Gerardo Suarez

If your branch is not development or production, the easiest way to achieve this is resetting to a certain commit locally and create a new branch from there. You can use:

如果您的分支不是开发或生产分支,实现这一点的最简单方法是在本地重置为某个提交并从那里创建一个新分支。您可以使用:

git checkout 000000

git 结帐 000000

(where 000000 is the commit id where you want to go) in your problematic branch and then simply create a new branch:

(其中 000000 是您想要去的提交 ID)在有问题的分支中,然后简单地创建一个新分支:

git remote add [name_of_your_remote]

git 远程添加 [name_of_your_remote]

Then you can create a new PR and all will work fine!

然后你可以创建一个新的 PR,一切都会正常的!

回答by Joel Wiklund

Sourcetree: resetting remote to a certain commit

Sourcetree:将远程重置为某个提交

  1. If you have pushed a bad commit to your remote (origin/feature/1337_MyAwesomeFeature) like below picture
  1. 如果你向你的遥控器(origin/feature/1337_MyAwesomeFeature)推送了一个错误的提交,如下图

Go to Remotes

Go to Remotes

  1. Go to Remotes > origin > feature > 1337_MyAwesomeFeature
  2. Right click and choose "Delete origin/feature/1337_MyAwesomeFeature" (Or change name of it if you want a backup and skip step 4.)
  3. Click "Force delete" and "OK".
  1. 转到遥控器 > 原点 > 功能 > 1337_MyAwesomeFeature
  2. 右键单击并选择“Delete origin/feature/1337_MyAwesomeFeature”(如果您想要备份并跳过步骤 4,则更改它的名称。)
  3. 单击“强制删除”和“确定”。

enter image description hereenter image description here

enter image description hereenter image description here

  1. Select your older commit and choose "Reset current branch to this commit"
  2. Choose which mode you want to have (Hard if you don't want your last changes) and "OK".
  1. 选择您的旧提交并选择“将当前分支重置为此提交”
  2. 选择您想要的模式(如果您不想进行上次更改,则为“硬”)并“确定”。

enter image description hereenter image description here

enter image description hereenter image description here

  1. Push this commit to a new origin/feature/1337_MyAwesomeFeature enter image description here

  2. Your local feature branch and your remote feature branch are now on the previous (of your choice) commit enter image description here

  1. 将此提交推送到新的 origin/feature/1337_MyAwesomeFeature enter image description here

  2. 您的本地功能分支和远程功能分支现在位于上一个(您选择的)提交中 enter image description here

回答by Siddharth Choudhary

Do one thing, get the commit's SHA no. such as 87c9808 and then,

做一件事,获取提交的 SHA 编号。比如 87c9808 然后,

  1. move yourself ,that is your head to the specified commit (by doing git reset --hard 89cef43//mention your number here )
  2. Next do some changes in a random file , so that the git will ask you to commit that locally and then remotely Thus, what you need to do now is. after applying change git commit -a -m "trial commit"
  3. Now push the following commit (if this has been committed locally) by git push origin master
  4. Now what git will ask from you is that
  1. 移动你自己,这是你的头到指定的提交(通过做 git reset --hard 89cef43//在这里提到你的号码)
  2. 接下来在一个随机文件中做一些更改,以便 git 会要求您在本地然后远程提交该文件。因此,您现在需要做的是。应用更改后 git commit -a -m "trial commit"
  3. 现在通过 git push origin master 推送以下提交(如果已在本地提交)
  4. 现在 git 会问你的是

error: failed to push some refs to 'https://github.com/YOURREPOSITORY/AndroidExperiments.git' 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.**

错误:无法将一些引用推送到“ https://github.com/YOURREPOSITORY/AndroidExperiments.git”提示:更新被拒绝,因为您当前分支的提示落后于提示:其远程副本。在再次推送之前集成远程更改(例如提示:'git pull ...')。**

  1. Thus now what you can do is
  1. 因此现在你可以做的是

git push --force origin master

git push --force origin master

  1. And thus, i hope it works :)
  1. 因此,我希望它有效:)