0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Gitで新規ファイルをステージから外す方法

0
Posted at

Gitで新規ファイルをステージから外す方法

Gitで間違えて新規ファイルをステージしてしまったことはありませんか?

例えば、git statusで以下のような状態になったとします。

Changes to be committed:
  new file: sample.txt

新規ファイルをステージから外す

以下のコマンドを実行します。

git restore --staged sample.txt

これでステージから外れます。

まだファイルは残っている

git restore --stagedステージから外すだけなので、ファイル自体は削除されません。

git status
Untracked files:
  sample.txt

このように未追跡ファイルになります。

ファイル自体も削除したい場合

未追跡ファイルを削除するには

git clean -f

ディレクトリもまとめて削除したい場合は

git clean -fd

を実行します。

まとめ

やりたいこと コマンド
ステージから外す git restore --staged <file>
未追跡ファイルを削除 git clean -f
未追跡ディレクトリも削除 git clean -fd

Gitを使い始めたばかりの頃は、ステージと作業ツリーの違いが分かりにくいですが、この3つを覚えておくと困る場面が減ります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?