コミットを整理する git rebase -i
or git commit --amend
不要なコミット、例えばADDし忘れたファイルを追加するだけのコミットとかいらないので消したり修正したいとき
# 1つ前のコミットを書き換える方法
$ git commit --amend -m "書き換えたいメッセージ"
# 書き換えないで追加する方法
# git add 追加したいファイル
# git commit --amend --no-edit
# それ以外のコミットを書き換える方法。(もちろん直前も可)
$ git rebase -i コミットID
# するとエディタが起動
pickと書いてあるところをeditやdropなどコマンドの説明に書いてあるように編集し、保存。`:wq`
-----コマンド一覧に従う↓
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
-----
→ editとした場合、また編集のエディタが起動し、コミットの内容を書き換えることができる。
誰かがリモートブランチを削除した場合
#リモート上の削除されたブランチがあるかどうか確認
$ git remote show origin
#リモート上の削除されたブランチをローカルから消す
$ git fetch --prune
又は
$ git pull --prune
- 上記がめんどくさい場合git configに設定
#グローバルに設定。--prune付きでfetchとpullが実行されるようになる。
$ git config --global fetch.prune true
$ git fetch
犯人捜し
git blameコマンドを使うことで、各行が変更された最終コミットを知ることができる。
$ git blame ファイル名
# インデントコミットを無視
$ git blame -w
# 他
# `-L` オプションで行数を指定できる
コミットはせずに変更を退避
# saveはなくてもOK
$ git stash save
# 新規ファイルも含めて保存
$ git stash -u
# 保存したものを確認
$ git stash list
stash@{X}がstashの名前。WIP onのあとはブランチ名
# 戻す
$ git stash apply stash@{0}
stash名を指定しない場合は、直近に退避された変更を戻す
# ADDされた状態で戻す場合はindexをつける
$ git stash apply stash@{0} --index
# 削除する
$ git stash drop stash@{0}
# すべて削除する
$git stash clear
# 戻す&削除はpop。らくちん
$ git stash pop stash@{0}