gitファイルをステージングエリアバージョンに戻すにはどうすればよいですか?


80

という名前のファイルがあるとしましょうa.txt。それをステージング領域に追加してから、変更します。追加したときの状態に戻すにはどうすればよいですか?

回答:


77
  • Git 2.23より前: git checkout a.txt
  • Git 2.23以降: git restore a.txt

を入力すると、Gitがこれを通知しますgit status

Git 2.23より前:

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
# modified:   a
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified:   a
#

Git 2.23以降:

On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   a

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   a

3
@Daenyth投稿する前に確認しましたが、出力には、さまざまな状態(ステージングと非ステージング)でファイルをリセットするさまざまな方法が示されています
abyx 2010年

1
@ Daenyth-「gitcheckoutbranch-namepath」または「gitcheckoutHEADpath」を考えています
William Pursell 2010年

@ウィリアム:ありがとう!今でははるかに理にかなっています。
Daenyth 2010年

新しいファイルでは機能しないため、オブジェクトが必要なため、ステージングから実際にチェックアウトすることはありません。ステージングからチェックアウトするにはどうすればよいですか?編集それは--ステータスが言うように動作しました。
ルディ2014年

30

git checkout -- a.txt

このページの他の回答にはがなく--、混乱を招きました。

これは、Gitが入力したときに通知する内容ですgit status

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
# modified:   a
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified:   a
#

4
以前に引用されたものを投稿するよりも、違いを教えてください。
バッハサウ

2

ステージングされたファイルのステージング解除

次の2つのセクションでは、ステージング領域と作業ディレクトリの変更を操作する方法を示します。良い点は、これら2つの領域の状態を判別するために使用するコマンドが、それらへの変更を元に戻す方法も通知することです。たとえば、2つのファイルを変更し、それらを2つの別々の変更としてコミットしたいが、誤ってgit add *と入力し、両方をステージングしたとします。2つのうちの1つをどのようにアンステージできますか?gitstatusコマンドは次のことを思い出させます。

$ git add *
$ git status

On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

renamed:    README.md -> README
modified:   CONTRIBUTING.md

「変更をコミットする」というテキストのすぐ下に、git reset HEAD ...を使用してステージを解除すると書かれています。それでは、そのアドバイスを使用して、CONTRIBUTING.mdファイルのステージングを解除しましょう。

$ git reset HEAD CONTRIBUTING.md
Unstaged changes after reset:
M   CONTRIBUTING.md

$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

renamed:    README.md -> README

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

modified:   CONTRIBUTING.md

コマンドは少し奇妙ですが、機能します。CONTRIBUTING.mdファイルは変更されていますが、再びステージングされていません。

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.