如何获取特定提交时的工作目录内容
我有一个 git 存储库,其中包含一些提交。我想重现工作目录的准确状态,因为它是在特定提交之后的(这里我假设我已经提交了所做的所有更改)。
我尝试使用 git checkout ,但此命令不会删除工作目录中的现有文件(在所需提交后添加)。
简单的例子来说明我的问题。我使用以下命令准备了存储库
u@u-desktop:/tmp/git$ git init
Initialized empty Git repository in /tmp/git/.git/
u@u-desktop:/tmp/git$ echo 'ffff' > first.file
u@u-desktop:/tmp/git$ git add first.file
u@u-desktop:/tmp/git$ git commit -m "Important file was added"
[master (root-commit) fb05f7e] Important file was added
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 first.file
u@u-desktop:/tmp/git$ echo 'Second line' >> first.file
u@u-desktop:/tmp/git$ git commit -m "Important data was added" -a
[master df93d04] Important data was added
1 files changed, 1 insertions(+), 0 deletions(-)
u@u-desktop:/tmp/git$ echo 'ssss' > second.file
u@u-desktop:/tmp/git$ git add second.file
u@u-desktop:/tmp/git$ git commit -m "Second important file was added"
[master b6c106a] Second important file was added
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 second.file
u@u-desktop:/tmp/git$ echo 'tttt' > third.file
u@u-desktop:/tmp/git$ git add third.file
u@u-desktop:/tmp/git$ git commit -m "Third important file was added"
[master 33fce06] Third important file was added
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 third.file
现在目录看起来像这个
u@u-desktop:/tmp/git$ ls
first.file second.file third.file
first.file
具有以下内容
u@u-desktop:/tmp/git$ cat first.file
ffff
Second line
现在我想恢复工作目录,因为它在第一次提交后看起来正好(fb05f7e
)
u@u-desktop:/tmp/git$ git checkout fb05f7e .
u@u-desktop:/tmp/git$ cat first.file
ffff
但是 < code>second.file 和 third.file
仍在目录中
u@u-desktop:/tmp/git$ ls
first.file second.file third.file
I have a git repository with some commits in it. I want to reproduce exact state of working directory as it was right after specific commit (Here i suppose that i've commited all changes that were made).
I tried to use git checkout
, but this command doesn't delete existing files (which were added after desired commit) in working directory.
Simple example to illustrate my problem. I prepared repository using following commands
u@u-desktop:/tmp/git$ git init
Initialized empty Git repository in /tmp/git/.git/
u@u-desktop:/tmp/git$ echo 'ffff' > first.file
u@u-desktop:/tmp/git$ git add first.file
u@u-desktop:/tmp/git$ git commit -m "Important file was added"
[master (root-commit) fb05f7e] Important file was added
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 first.file
u@u-desktop:/tmp/git$ echo 'Second line' >> first.file
u@u-desktop:/tmp/git$ git commit -m "Important data was added" -a
[master df93d04] Important data was added
1 files changed, 1 insertions(+), 0 deletions(-)
u@u-desktop:/tmp/git$ echo 'ssss' > second.file
u@u-desktop:/tmp/git$ git add second.file
u@u-desktop:/tmp/git$ git commit -m "Second important file was added"
[master b6c106a] Second important file was added
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 second.file
u@u-desktop:/tmp/git$ echo 'tttt' > third.file
u@u-desktop:/tmp/git$ git add third.file
u@u-desktop:/tmp/git$ git commit -m "Third important file was added"
[master 33fce06] Third important file was added
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 third.file
Right now directory looks like this
u@u-desktop:/tmp/git$ ls
first.file second.file third.file
first.file
has following content
u@u-desktop:/tmp/git$ cat first.file
ffff
Second line
Now i want restore working directory as it looked right after first commit (fb05f7e
)
u@u-desktop:/tmp/git$ git checkout fb05f7e .
u@u-desktop:/tmp/git$ cat first.file
ffff
But second.file
and third.file
are still in the directory
u@u-desktop:/tmp/git$ ls
first.file second.file third.file
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
删除
git checkout
中的.
。手册页中的选择:此外,如果您想删除索引中未检查的文件,请使用
git clean
(阅读手册页,有一个“反-哎呀”选项,你必须在命令行上传递它才能工作)。Remove the
.
in yourgit checkout
. Selections from the man page:If, moreover you want to remove files that are not checked in the index, use
git clean
(read the man page, there's an "anti-oops" option you'll have to pass on the command line for it to work).只需执行以下操作:
...即不指定当前目录(
.
)作为结帐路径。您会发现second.file
和third.file
已按您的预期删除。原因是 git checkout 有两种截然不同的操作模式,具体取决于您是否提供路径。如果你确实提供了一个路径,它根本不会改变
HEAD
,它只是(来自
git checkout
文档)。这只会更新其他提交中实际存在的路径。Just do:
... i.e. without specifying the current directory (
.
) as the path to checkout. You'll find thatsecond.file
andthird.file
are removed as you expect.The reason for this is that
git checkout
has two very different modes of operation, depending on whether you supply a path or not. If you do supply a path it doesn't changeHEAD
at all, it just(That's from the
git checkout
documentation). This only updates the paths that were actually present in that other commit.