我在远程存储库中有一个项目,与本地存储库(开发)和服务器存储库(prod)同步。我一直在做一些已经推送到远程和从服务器上拉出来的承诺更改。现在,我想撤销这些更改。因此,我可以在更改之前签出到提交,并提交新更改,但我猜再次将它们推到远程会有问题。我该怎么做,有什么建议吗?


当前回答

在这些情况下,我所做的是:

在服务器中,将光标移回上一次已知的正确提交: Git push -f origin <last_known_good_commit>:<branch_name> 在本地,执行同样的操作: Git重置——hard <last_known_good_commit> # ^^^^^^ # 可选




查看我为此目的创建的分支my_new_branch的完整示例:

$ git branch
my_new_branch

这是在myfile.py中添加了一些东西后的最近历史:

$ git log
commit 80143bcaaca77963a47c211a9cbe664d5448d546
Author: me
Date:   Wed Mar 23 12:48:03 2016 +0100

    Adding new stuff in myfile.py

commit b4zad078237fa48746a4feb6517fa409f6bf238e
Author: me
Date:   Tue Mar 18 12:46:59 2016 +0100

    Initial commit

我想摆脱上一个提交,它已经被推送了,所以我运行:

$ git push -f origin b4zad078237fa48746a4feb6517fa409f6bf238e:my_new_branch
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:me/myrepo.git
 + 80143bc...b4zad07 b4zad078237fa48746a4feb6517fa409f6bf238e -> my_new_branch (forced update)

好了!现在我看到提交时更改的文件(myfile.py)显示为“not staging for commit”:

$ git status
On branch my_new_branch
Your branch is up-to-date with 'origin/my_new_branch'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   myfile.py

no changes added to commit (use "git add" and/or "git commit -a")

因为我不想要这些改变,我只是把光标移回本地:

$ git reset --hard b4zad078237fa48746a4feb6517fa409f6bf238e
HEAD is now at b4zad07 Initial commit

所以现在HEAD在之前的提交中,在本地和远程:

$ git log
commit b4zad078237fa48746a4feb6517fa409f6bf238e
Author: me
Date:   Tue Mar 18 12:46:59 2016 +0100

    Initial commit

其他回答

重置对我来说很困难: 谢谢@Mo D Genensis和@vibs2006


git reset --hard 'your last working commit hash'

git clean -f -d

git push -f

这是我的方法:

假设分支名为develop。

# Checkout a new temp branch based on one history commit
# (command to get history commit hashes: git log --oneline)
git checkout <last_known_good_commit_hash>

# Delete the original develop branch 
git branch -D develop
# Create a new develop branch based on the temp branch
git checkout -b develop

# Force update the remote with the new branch
git push -f origin develop

干净利落地做:

git rebase -i <hash of last good commit, 9 chars is enough>

现在,您将得到一个从上一次提交到HEAD的提交列表,以及对每个提交做什么的选项。DROP将丢弃该提交。保存文件。

现在修复上游做:

git push --force-with-lease

(使用租赁,这样你就不会意外地给其他人带来问题)

通过删除错误的提交,而不是引入新的提交来修复先前的错误提交,从而保持日志干净。

你可以这样做

git push origin +<short_commit_sha>^:<branch_name>

如果你按照下面给出的步骤通过Git命令行,你可以将Git本地和远程提交还原(或者你也可以称之为DELETE)。

运行以下命令查看要还原的提交id

git log --oneline --decorate --graph

你会得到如下截图

如果你也检查远程(通过Web接口),那么你可以看到这将是相同的如下所示

根据截图,目前你的提交id是e110322,但你想在本地和远程都恢复到030bbf6。

执行以下步骤删除/恢复本地+远程提交


首先本地恢复提交id 030bbf6

git reset --hard 030bbf6

紧随其后的是

git clean -f -d

这两个命令强制重置以提交阶段030bbf6,如下图所示

现在如果你跑了 git状态,然后你会看到你在远程分支中是TWO Commits BEHIND,如下所示

运行以下命令更新索引(如果有更新的话)。建议您要求所有开发人员不要接受主远程分支上的任何拉请求。

git fetch --all

一旦你完成了它,然后你需要在分支前面使用+符号强制推送这个提交,如下所示。我这里用的是主分支,你可以替换成任何分支

代码

git push -u origin +master

现在如果你看到远程的web界面,那么提交也应该被还原。