0

我想恢复到特定的提交 ID。恢复我“git rm”编辑的文件。清除从那时起所做的所有提交。然后提交该回购。我该怎么做?

4

3 回答 3

2
$ git reset --hard <commit ID>    # Will reset to a specific commit
$ git push -f <remote>            # Push branch into remote, with force

请注意,任何克隆了您的 repo 的人都可能会遇到小问题,如果他们只是拉入您的更改(因为您在提交历史记录中强制分支向后退);他们应该这样做以获得您的更改:

$ git fetch <remote>
$ git reset --hard <remote>/<branch>

请注意,这也会消除他们在当前分支中的任何更改。但是,如果他们有从当前分支创建的任何分支,这些分支仍然会有你“吹走”的提交。

于 2012-11-13T17:55:17.043 回答
0

也许您正在寻找git reset --soft {ID}.

   git reset --<mode> [<commit>]
       This form resets the current branch head to <commit> and possibly updates the index (resetting it to the tree of <commit>) and the working tree depending on <mode>, which must be one of the
       following:

       --soft
           Does not touch the index file nor the working tree at all (but resets the head to <commit>, just like all modes do). This leaves all your changed files "Changes to be committed", as git
           status would put it.

不确定“打击”的真正含义是什么,为什么--hard模式也很有用。

于 2012-11-13T17:54:16.883 回答
0

您可以使用git revert my_commit_id命令。

如果是之前的一两次提交,则分别使用git revert HEAD和进行git revert HEAD^

如果这不能解决您的问题,请使用您的提交 ID 而不是 HEAD。

这还将为您提供再次编辑提交消息并创建新提交的选项。`

于 2012-11-13T17:54:41.820 回答