🙆

【Git】untracked fileを元に戻す方法

に公開

はじめに

Gitで作業をしていると、以下のような文言に出会うことがあります。
この文言に出会った時に元に戻す方法についてご紹介します。

$ git status
On branch add-configuration-to-run-terraform-ci
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	.github/workflows/terraform-ci.yml

nothing added to commit but untracked files present (use "git add" to track)
$

untracked fileとは?

  • 未追跡ファイルになります。
  • 前回のcommitの時点では存在しなかったファイル(新たに作成したファイル)になります。

対応方法

git clean コマンドを使用します。

untracked fileを削除する前に確認したい場合

コマンド
git clean -n
実行例
$ git clean -n
Would remove .github/workflows/terraform-ci.yml
$ 

untracked fileを削除したい場合

コマンド
git clean -f
実行例
$ git clean -f
Removing .github/workflows/terraform-ci.yml
$ 

参考

https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository

https://git-scm.com/docs/git-clean

https://qiita.com/tmyn470/items/c8359e4ec92d1f462bdf

Discussion