我正在处理一个项目(主分支),其中有约100个提交,没有分支等等。我想看看在1、10、20、30次提交时该项目的样子。如何查看早期提交而不丢失任何数据?我想在查看之后返回到现在的位置。怎么做?我认为还原更加永久性,我应该寻找其他方法吗?
如果只是想查看(如你所问),请执行以下操作
git checkout [the name of the sha]
git log
这里你可以看到我正在查看一个有数百个提交的存储库的初始提交:
$ git checkout 772df05 # Enough of the sha to be unique, often 6-8 chrs.
Note: checking out '772df05'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b new_branch_name
HEAD is now at 772df05... Initial commit
查看文件,然后...
$ git checkout master
最后提醒一下,当你要切换分支时,你可能需要处理两件事:
首先,任何已经存在于git中的本地修改过的文件(与新增文件相对)应该被添加或者被隐藏。在学习git时,添加文件可能是最简单的方式。
其次,任何已经执行了git add
命令的新文件或者修改过的文件都应该被提交,即使只是作为一个"工作正在进行中"的标记。
error: cannot stat 'core/admin': Permission denied error: cannot stat 'core/admin': Permission denied error: cannot stat 'core/admin': Permission denied
- 尝试中止变基,但没有正在进行的变基。 是的,我当然在Windows(8.1)上。 为什么会发生这种情况以及如何解决? :( [更新] 关闭Sublime Text,看起来当您更改的文件在任何程序中打开时,它们会在Windows上被锁定。 如果其他人也遇到此问题,则已解决并留给他们。 - Wordpressorgit revert
并不会丢失任何数据。但是,它确实会改变当前分支的工作副本和头部。如果您想查看以前的提交记录,可以使用 git log
和它的许多参数。如果您想要检出一个实际的提交版本以在编辑器中查看文件,则只需使用 git checkout
移动到任何您想要的提交版本。完成后,只需执行 git checkout master
返回到当前状态。
您还可以使用git stash save
将当前工作保存在存储中作为补丁。然后进行任何想做的事情,包括检出修订版本或应用补丁。然后使用git stash apply
或git stash pop
重新应用本地更改。
编辑:哎呀,抱歉意识到这与原始帖子有些不同。
使用git log
命令查看项目的所有提交记录,使用箭头键向下滚动,直到找到您想要查看的提交记录。
然后,只需记下其哈希值(如55ea49),并使用以下命令:
git checkout <hash>
它会提取与提交相匹配的文件,但保留其他文件,因此当您完成后,可以使用
git checkout master
返回到最新的提交。
git checkout HEAD
git checkout HEAD~10
显然,您可以将10更改为任何数字
您应该执行
git help checkout