Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从git上丢失的提交中恢复代码?_Git - Fatal编程技术网

如何从git上丢失的提交中恢复代码?

如何从git上丢失的提交中恢复代码?,git,Git,我使用git checkout“commit number1”回滚到上一次提交。然后我没有意识到我是在提交,而不是在任何分支上,所以我在这里做了更改,并在“commit number1”中提交了代码。 现在我切换到功能分支功能/分支1我没有看到任何代码。 如果我切换回“commit Number1”,我也看不到那里的代码。 我是否与任何事物分离 $ git checkout 49da8b4d431 Note: checking out '49da8b4d431'. You are in 'd

我使用git checkout“commit number1”回滚到上一次提交。然后我没有意识到我是在提交,而不是在任何分支上,所以我在这里做了更改,并在
“commit number1”
中提交了代码。 现在我切换到功能分支<代码>功能/分支1我没有看到任何代码。 如果我切换回
“commit Number1”
,我也看不到那里的代码。 我是否与任何事物分离

$ git checkout 49da8b4d431

Note: checking out '49da8b4d431'.

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

如何恢复代码?我的代码到哪里去了?

git reflog
将向您显示
头部的历史记录。查看它,找到您在
“commit number1”
上所做的提交的SHA。当您知道SHA时,您可以在需要的地方挑选它。

键入
git reflog
,它将显示所有最近提交的列表。查找其消息为“commit number1”的提交,然后记录此提交的SHA-1哈希值(看起来类似于由7个字符组成的随机字母数字字符串,例如
s73nd9a

要将此提交引入您的功能分支,一个选项是使用
git cherry pick
。请尝试以下操作:

git checkout feature/branch1
git cherry-pick s73nd9a

这将应用您在分离头部状态下所做的单个提交。请记住,一个cherry pick本质上是一个commit的合并,因此您可能会遇到冲突。

git从源代码中提取您想要的分支这里是对您需要执行的操作的完整描述。谢谢你,蒂姆。这很有帮助。