【问题标题】:Git go back to a specific commit [duplicate]Git回到特定的提交[重复]
【发布时间】:2018-04-05 10:19:54
【问题描述】:

我现在执行这个:

git reset --hard shaCommit .

我会将我的代码收回到以前的特定提交,但是,我该如何推送呢?假设我的最后 4 次提交是垃圾,我想回到特定的提交,但是当我尝试推送时出现此错误:

$ git push
To ssh://git@uk-gitlab.almuk.corp/warlocks/capexbaar-web.git
 ! [rejected]        development -> development (non-fast-forward)
error: failed to push some refs to 'ssh://git@uk-gitlab.almuk.corp/warlocks/capexbaar-web.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.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

【问题讨论】:

标签: git


【解决方案1】:

你必须强制推送,使用命令git push -f。

如果您在强制推送之前拉取更改,它将下载所有更改并执行合并提交。

【讨论】:

  • 请注意,这是一件非常容易做的事情,但如果做错了也会非常危险。你应该仔细研究推力,并在使用它之前知道你正在进入什么,否则你会冒着让你的同事对你非常生气的风险。
【解决方案2】:

推荐你在这里查看这个答案Similar Question

引用原始已接受的解决方案

只要做:

git push origin <your_branch_name> --force

或者如果您有特定的仓库:

git push https://git.... --force

【讨论】:

    【解决方案3】:

    如果您在 repo 中与其他人合作,并且您已经推送了要删除的提交,通常最好不要修改历史记录,这样您就不会搞砸其他人正在做的事情。

    为此,您可以推送新的提交以将更改恢复到该状态,而不仅仅是删除旧的提交:

    git checkout shaCommit -- .
    git commit -a
    

    查看Revert multiple git commits了解更多详情

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-16
      • 2011-05-17
      • 2019-01-12
      • 2014-05-02
      • 2016-04-20
      • 2019-12-11
      • 2010-12-09
      • 2011-12-06
      相关资源
      最近更新 更多