Githubでチーム開発する際によく使うGitコマンド
issueに着手する際、DraftのPRを出す
issueに着手する際、空のプルリクエストを送る必要がある。
git commit --allow-empty -m "PR Title"
プルリクを出した後、ローカルで作業を継続する
main → test1のブランチで作業し、プルリクを出してマージされるのを待っている状態。
そのまま作業を継続する場合。
-
そのまま test1のブランチで実装
-
マージされたら
git stash
して実装の差分を退避される -
master を pull して test1 の実装がマージされた最新状態にする
-
新しく test2 ブランチを切って
git stash pop
を実行して差分を復活させる -
そのまま test2 ブランチで実装を続行して PR 出す
git stash -u
で未追跡のファイルもstashできる
#以下どちらでも可能
$ git stash -u
$ git stash --include-untracked
#忘れそうならメッセージも添えられます
$ git stash save -u 'hoge'
# 変更を戻す
$ git stash pop stash@{0}
コミットの取り消し、打ち消し、上書き
コミットの取り消し
直前のコミットをなかったことにしたい
git reset --hard HEAD^
-
hard
オプション:コミット取り消した上でワークディレクトリの内容も書き換えたい場合に使用。 -
soft
オプション:ワークディレクトリの内容はそのままでコミットだけを取り消したい場合に使用。 -
HEAD^
:直前のコミットを意味する。 -
HEAD~{n}
:n個前のコミットを意味する。
コミットの打ち消し
作業ツリーを指定したコミット時点の状態にまで戻し、コミットを行う(コミットをなかったことにはせず、逆向きのコミットをすることで履歴を残す)には、
git revert コミットのハッシュ値
コミットの上書き
git commit --amend
-
コミットメッセージを変更したい時
-
これもコミットに加えたい が後から気づいたとき
-
git rebase
失敗した時、コンフリクトを避けるためにコミットを上書きしたり
gitのcommitをまとめる
git log --oneline
でログを確認。
426f407 (HEAD -> develop) commit C
778ec4e commit B
cfe4665 commit A
432cb61 rebaseTest
rebase
するコミットを指定する
git rebase -i コミットid
git rebase -i HEAD~4
指定するコミットidは、リベースしたいところの一つ前を指定すること。 上記の例だと、 以下の3つのコミットをまとめたい場合は、1つ前のコミット 432cb61 rebaseTest
を指定する。 (HEAD指定だと HEAD~4
)
-
commit A
-
commit B
-
commit C
pick cfe4665 commit A
pick 778ec4e commit B
pick 426f407 commit C
# Rebase ccaab03..426f407 onto ccaab03 (4 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
指示コマンド
-
(p)pick コミットをそのまま残す。
-
(r)reword コミットメッセージを変更。
-
(e)edit コミット自体の内容を編集。
-
(s)squash 直前のpickを指定したコミットに統合。メッセージも統合。
-
(f)fixup 直前のpickを指定したコミットに統合。メッセージは破棄。
pick cfe4665 commit A
s 778ec4e commit B
s 426f407 commit C
# Rebase ccaab03..426f407 onto ccaab03 (4 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# This is a combination of 3 commits.
ここに新しいコミットメッセージを書く
# This is the 1st commit message:
commit A
# This is the commit message #2:
commit B
commit C
commitA commitB commitCがrebaseされた。
8c44d3b (HEAD -> develop) new message
432cb61 rebaseTest