11
5

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】小技集

Last updated at Posted at 2025-05-13

はじめに

普段あまり使っていなかったけど、最近便利だなと感じたGitコマンドを備忘録としてまとめます。

利用可能なすべてのコマンドを標準出力に出力

git help -a

Git公式の基本的なコマンドのサンプルを表示

git help everyday

コミット間の差分ファイル一覧を取得する

git diff --name-only <コミットA> <コミットB> >> <出力先ファイル>
  • --name-only オプションで変更ファイル名のみを表示
  • <コミットA><コミットB> の間の差分を抽出
  • >> で結果を指定ファイルに追記

実行例

  1. リポジトリのルートで実行
  2. ~/Desktop/changed_files.txt に変更ファイルが追記される
git diff --name-only c60be7b e67c042 >> ~/Desktop/changed_files.txt

スクリーンショット 2025-05-11 11.54.30.png

日付付きバックアップブランチを作成する

定期的なメンテナンスやマージ前に、現在のブランチを日付付きでバックアップしたい場合に便利なコマンド

git branch develop-backup-$(date +%Y%m%d) develop
  • $(date +%Y%m%d) で YYYYMMDD 形式の日付を自動挿入
  • develop ブランチを元に develop-backup-20250511 のようなブランチを作成

実行例

# 今日の日付が 2025-05-11 の場合
git branch develop-backup-20250511 develop

これで、万が一のロールバックが必要になった際にも簡単に復元できます。

git show でコミットの詳細を表示する

git diffで詳細を見ていたのですが、pushしたコミットについては見れないので、メンバーに教えていただいた便利コミット。

git show <コミットID>
  • <コミットID> の変更内容(diff)を標準出力に表示
  • オプションでフォーマットをカスタマイズ可能

オプション

オプション 説明
--stat ファイルごとの変更行数サマリーを表示
--name-only 変更されたファイル名のみを表示
--pretty=oneline コミットを1行形式で表示(ID とメッセージ)

実例

git show --stat e67c042
 commit e67c042
 Author: 名前
 Date:   2025-05-10 14:23:45 +0900

    修正

 screenshots/top.png | Bin 89369 -> 556222 bytes
 1 file changed, 0 insertions(+), 0 deletions(-)

git show --name-only e67c042
 commit e67c042
 Author: 名前
 Date:   2025-05-10 14:23:45 +0900
 
    修正
screenshots/top.png

git show --pretty=oneline e67c042
e67c042 修正
diff --git a/screenshots/top.png b/screenshots/top.png
index f3286d4..8fcdc11 100644
Binary files a/screenshots/top.png and b/screenshots/top.png differ

まとめ

普段あまり使っていなかったけど便利なGitコマンドを紹介しました。

参考記事

11
5
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
11
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?