如果我查看之前的提交,如何避免丢失提交?

时间:2014-04-18 19:19:14

标签: git commit

如果我查看之前的提交,如何避免丢失提交?我想回过头来看看我几天前提到的一个早期状态。

我收到了消息:你要离开一个提交,而不是连接到我们的任何分支。我通过检查头,提交,然后检查主人来得到这个。

2 个答案:

答案 0 :(得分:2)

您可以使用git checkout <checksum>访问之前的提交。

Git会警告你,你在detached HEAD state,但不要担心,其他人的提交不会丢失。你可以环顾四周,当你完成后,你可以git checkout你所在的分支开始回到原来的位置。

在执行此操作时,查看自己身份的好方法是使用git branch命令。

示例:

>$ (...) demo setup (...)
>$ git branch
* master
>$ git log
commit c69905e97e0975810a7b35ca1adf872e9e916758
[...]
Commit again

commit a9d5d2f5ad39036093048750ecffeb570875611b
[...]
First commit
>$ git checkout a9d5d2f
Note: checking out 'a9d5d2f'.

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 a9d5d2f... First commit
>$ git branch
* (detached from a9d5d2f)
  master
>$ (...) look at my files, do some stuff (...)
>$ (...) I can make some commits here and make a new branch with them - see above git message (...)
>$ git checkout master # I'm back to the original state
>$ git branch
* master

Voilà!

答案 1 :(得分:0)

git checkout HEAD~10 -- <path to file>会将现有文件覆盖到本地副本中的该版本。这并不意味着你已经丢失了你的更改,除非你提交回来,在这种情况下,你仍然可以使用git中的原始提交,你可以回来。

如果您想比较两次提交,请使用git diff而不是git checkout