我有一个 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
但是second.file
并且third.file
仍在目录中
u@u-desktop:/tmp/git$ ls
first.file second.file third.file