LoginSignup
8
1

More than 3 years have passed since last update.

gitコマンドで、未追跡ファイルを一括削除

Posted at

tl;dr

git cleanコマンドで未追跡ファイルを一括削除できます。

デモ

現在のカレントブランチを初期状態に戻したいとします。
git statusコマンドでブランチの状態を確認すると、変更ファイルが1つ、未追跡ファイルが2つあります。

$ git status
On branch develop
Your branch is up to date with 'origin/develop'.

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:   src/index.js

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        core.33
        core.43

git checkout .コマンドを実行したあと、再度git statusコマンドでブランチの状態を確認すると、未追跡ファイルが2つ残っています。

$ git status
On branch develop
Your branch is up to date with 'origin/develop'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        core.33
        core.43

そこで、cleanコマンドで未追跡ファイルを一括削除します。

$ git clean -df
Removing core.33
Removing core.43

一括削除前に、どのファイルが削除対象なのか確認することもできます。

$ git clean -n
Would remove core.33
Would remove core.43

未追跡ファイルがクリアされ、完全に初期のブランチの状態に戻りました。

$ git status
On branch develop
Your branch is up to date with 'origin/develop'.

nothing to commit, working tree clean

以上。

8
1
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
8
1