2
4

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の変更を元に戻したい→restoreとcleanコマンド

Posted at

はじめに

gitにおいて,現在の行っていた変更はもうよくて,1つ前のコミットに戻りたいときありませんか?
そんなときによく使うgit restoregit cleanについて説明します

git restore

git restoreは、作業ディレクトリやステージング領域のファイルを復元するためのコマンドです。

主な用途

  • 作業ディレクトリの変更を元に戻す
  • ステージングされた変更を取り消す

よく使うオプション

  • git restore <ファイル名>: 作業ディレクトリの変更を元に戻す
    • すべてのファイルを戻したい時はgit restore .
  • git restore --staged <ファイル名>: ステージングを取り消す(変更はそのまま残る)
  • git restore --source=HEAD~1 <ファイル名>: 特定のコミットの状態に復元

git clean

git cleanは、追跡されていないファイル(Gitで管理されていないファイル = untracking file)を削除するためのコマンドです。

主な用途

未追跡のファイルやディレクトリを削除してリポジトリをクリーンな状態にする

よく使うオプション

  • git clean -n: 削除されるファイルを表示(ドライラン)
  • git clean -f: 未追跡のファイルを強制的に削除
  • git clean -d: 未追跡のディレクトリも削除
  • git clean -x: .gitignoreで指定されたファイルも削除

使い分け

git restore: すでに追跡されているファイルの変更を元に戻す
git clean: 追跡されていない(新規作成された)ファイルを削除する

2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?