1

いくつかのコミットを含む 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.filethird.fileまだディレクトリにあります

u@u-desktop:/tmp/git$ ls
first.file  second.file  third.file
4

2 に答える 2

3

の を削除.しますgit checkout。man ページからの選択:

   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(man ページを読んでください。コマンドラインで渡す必要がある「anti-oops」オプションがあります)。

于 2011-08-02T15:17:34.797 に答える
3

ただ行う:

git checkout fb05f7e

... つまり、現在のディレクトリ ( ) をチェックアウトへのパスとして指定しません。.それが見つかり、second.file期待third.fileどおりに削除されます。

この理由はgit checkout、パスを指定するかどうかに応じて、2 つの非常に異なる操作モードがあるためです。HEADパスを指定しても、まったく変更されません。

... 作業ツリー内の名前付きパスをインデックス ファイルから、または名前付き <tree-ish> (ほとんどの場合コミット) から更新します。

(それはgit checkoutドキュメントからのものです)。これは、他のコミットに実際に存在していたパスのみを更新します。

于 2011-08-02T15:15:20.367 に答える