使用以下命令卸下头后恢复工作:git checkout xxx <期间>

时间:2018-06-25 12:38:08

标签: git

我每个https://stackoverflow.com/a/2007704/1032531都检出了以前的git commit。请注意,它声明包含.。我是否正在抽水,并且在进行此操作之前是否应该学习更多有关此主题的内容?在签出另一个分支之前,如何找回正在进行的更改?

[michael@devserver autoapp]$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   public/index.php
        ..and about 40 other files

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        src/Lib/Helper.php

no changes added to commit (use "git add" and/or "git commit -a")

[michael@devserver autoapp]$ git log
commit 4037ec1702bd2fa6a5a15f7413f4ccccb59d356e (HEAD -> master)
Author: Michael Reed <michael@gmail.com>
... and lists the other commits but the one above was the latest one

[michael@devserver autoapp]$ git checkout 4037ec1702bd2fa6a5a15f7413f4ccccb59d356e .
[michael@devserver autoapp]$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        src/Lib/Helper.php

nothing added to commit but untracked files present (use "git add" to track)
[michael@devserver autoapp]$ git checkout master
Already on 'master'
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)
[michael@devserver autoapp]$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        src/Lib/Helper.php

nothing added to commit but untracked files present (use "git add" to track)
[michael@devserver autoapp]$ git diff
[michael@devserver autoapp]$ git checkout -
Already on 'master'
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

[michael@devserver autoapp]$

1 个答案:

答案 0 :(得分:0)

在命令中

git checkout 4037ec1702bd2fa6a5a15f7413f4ccccb59d356e .

.是路径说明符。提供路径说明符会从根本上改变checkout的作用。

在没有路径说明符的情况下,checkout移动HEAD。这意味着它要么“签出”分支,要么“签出”处于“分离式HEAD”状态的提交(可能是也可能不是分支的尖端)。除非您另行告知git,否则它将在破坏未提交的更改之前发出警告(即它将拒绝执行checkout)。

但是带有路径说明符,checkout从另一个版本更新文件的工作树版本。因为该命令被视为明确要求覆盖工作文件,并且实际上是您还原未执行的更改的方式,所以它并不像checkout的其他形式那样谨慎。

不幸的是,它们具有相同的基本命令名称,因为它们是非常不同的操作,而后者比前者要危险得多。

虽然git非常擅长保存已提交(或藏匿)的更改,但是如果丢失工作文件,它并不会提供太多帮助。缺少外部备份解决方案,恐怕您可能只需要重新开始。