首先,我以前在这里看到过这个问题的答案,但它被太多的“答案”所掩盖,这些“答案”不能正确回答我的问题,以至于我无法再次找到它。
所以事情就是这样:如何在 Git 中恢复到我的历史记录中的先前版本,以便它成为我当前历史记录之上的新提交?
基本上,这是我想用版本控制系统做的最基本的事情。简单地进行重置是行不通的,因为它会丢弃我的历史记录,简单的恢复也不起作用,因为有时它会给我错误消息(我想做的事情应该可以没有任何错误消息)。
如果我只是做一个git revert
发生此错误:
git revert HEAD~1
error: could not revert f1b44e3... Towards translating API to kernel.
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
Simply "checkout
提交”。这将使用历史记录中存储库的指定快照(提交)覆盖您当前的工作目录,并使该工作集成为您可以根据需要暂存和提交的新工作集。
cd ~/git/your-repo-root
git log
# find the commit id you want
git checkout <commitId> .
重要的提示: the 尾随.
上一行很重要!如果省略它,您最终将处于“分离的 HEAD”状态,这是您不想要的。
If you commit
之后您的存储库将立即具有与您执行的提交相同的文件系统内容checkout
到(假设您没有其他未暂存或暂存的更改):
git commit -m "Restoring old source code"
这会not重写历史记录,也不编辑或删除任何以前的提交 - 因此它的工作方式类似于 Mediawiki 上的“回滚”:
也可以看看:回滚到公共存储库中旧的 Git 提交 https://stackoverflow.com/questions/2007662/rollback-to-an-old-git-commit-in-a-public-repo
关于.
(dot)
The .
(点)字符的意思是“当前目录”——它对 git 来说并不是什么特殊或独特的东西,它是一个标准的命令行文件系统约定,在 Windows、Linux、macOS 甚至 MS-DOS 上都是相同的。它的工作原理类似于..
意思是“父目录”。我建议阅读这些:
- https://askubuntu.com/questions/54900/what-do-and-mean-when-in-a-folder https://askubuntu.com/questions/54900/what-do-and-mean-when-in-a-folder
- https://superuser.com/questions/37449/what-are-and-in-a-directory https://superuser.com/questions/37449/what-are-and-in-a-directory
- Linux 中的双点(..) 和单点(.) 是什么? https://stackoverflow.com/questions/23242004/what-is-double-dot-and-single-dot-in-linux
- https://unix.stackexchange.com/questions/118175/what-are-some-uses-of-single-period-double-period-in-the-shell-command https://unix.stackexchange.com/questions/118175/what-are-some-uses-of-single-period-double-period-in-the-shell-command
关于checkout
意识到:checkout
是 git 中的一个重载命令 - 它可能意味着切换分支(a lasvn switch
)或从历史记录中获取特定文件或提交并将其放入您的工作区(a lasvn update -r <id>
)。它也可能意味着其他事情:https://git-scm.com/docs/git-checkout https://git-scm.com/docs/git-checkout- 我很欣赏它会让人们感到困惑,尤其是当我在使用 TFS 多年后开始使用 git 时(其中“Checkout”完全意味着其他东西)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)