如何获取特定提交时的工作目录内容

发布于 2024-11-27 15:55:21 字数 1944 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怀中猫帐中妖 2024-12-04 15:55:22

删除 git checkout 中的 .。手册页中的选择:

   git checkout [<branch>], git checkout -b|-B <new_branch> [<start
     point>], git checkout [--detach] [<commit>]
       This form switches branches by updating the index, working tree,
       and HEAD to reflect the specified branch or commit.

       (...)

   git checkout [-p|--patch] [<tree-ish>] [--] <pathspec>...
       When <paths> or --patch are given, git checkout does not switch
       branches. It updates the named paths in the working tree from the
       index file or from a named <tree-ish> (most often a commit).

此外,如果您想删除索引中检查的文件,请使用git clean(阅读手册页,有一个“反-哎呀”选项,你必须在命令行上传递它才能工作)。

Remove the . in your git checkout. Selections from the man page:

   git checkout [<branch>], git checkout -b|-B <new_branch> [<start
     point>], git checkout [--detach] [<commit>]
       This form switches branches by updating the index, working tree,
       and HEAD to reflect the specified branch or commit.

       (...)

   git checkout [-p|--patch] [<tree-ish>] [--] <pathspec>...
       When <paths> or --patch are given, git checkout does not switch
       branches. It updates the named paths in the working tree from the
       index file or from a named <tree-ish> (most often a commit).

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).

最冷一天 2024-12-04 15:55:22

只需执行以下操作:

git checkout fb05f7e

...即指定当前目录(.)作为结帐路径。您会发现 second.filethird.file 已按您的预期删除。

原因是 git checkout 有两种截然不同的操作模式,具体取决于您是否提供路径。如果你确实提供了一个路径,它根本不会改变 HEAD,它只是

...从索引文件或命名的更新工作树中的命名路径。 (最常见的是提交)

(来自 git checkout 文档)。这只会更新其他提交中实际存在的路径。

Just do:

git checkout fb05f7e

... i.e. without specifying the current directory (.) as the path to checkout. You'll find that second.file and third.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 change HEAD at all, it just

... updates the named paths in the working tree from the index file, or from a named <tree-ish> (most often a commit)

(That's from the git checkout documentation). This only updates the paths that were actually present in that other commit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文