6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ちょっと困ったときの Git コマンド

Last updated at Posted at 2018-08-15

いつもではないけれど、ちょっと困ったときに使いたくなる Git コマンドをまとめました。
「使い方は分かっているのだけど、よくコマンドを忘れてしまうんだよな」という方向けです。

git revert

コミットを相殺するコミットを打ちたい

$ git revert [commit_id]

マージコミットを相殺するコミットを打ちたい

$ git revert -m [1|2] [commit_id]

git reset

コミットIDまで戻したい

$ git reset --hard [commit_id]

ローカルブランチの変更履歴をリモートブランチの変更履歴で上書きしたい

$ git checkout [branch_name]
$ git fetch
$ git reset --hard origin/[branch_name]

git log

短く表示したい、コミットメッセージとコミットIDだけ知りたい

$ git log --oneline

変更したファイルと差分のある行数を表示したい

$ git log --stat

コードの差分を表示したい

$ git log -p

コミットメッセージで絞り込みたい

$ git log --grep [commit_message]

コードの差分に含むキーワードで絞り込みたい

$ git log -S [keyword]

git grep

リポジトリ内のファイルをキーワードで検索したい

$ git grep [keyword]

リポジトリ内のファイルをパターンで検索したい

$ git grep -E "[regexp]"

git branch

ブランチをリネームしたい

$ git branch -M [old_branch_name] [new_branch_name]

マージ済のブランチを削除したい

$ git branch -d [branch_name]

マージ済に関わらずブランチを削除したい

$ git branch -D [branch_name]

master と develop 以外のマージ済のブランチをすべて削除したい

$ git branch --merged | grep -vE '^\*|master$|develop$' | xargs -I % git branch -d %

マージされていないブランチを表示したい

$ git branch --no-merged

man git

マニュアルを読みたい

$ man git
$ man git-[subcommand]
6
2
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
6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?