LoginSignup
1
0

More than 1 year has passed since last update.

git rebase

commitをまとめたい場合(git rebase -i)

これはめちゃくちゃ使います。レビューをしてもらって直すところが全くない場合は使いませんが、僕の場合は1発でLGTMをもらうことはなかなかないので、ほぼ毎回使ってます。

プルリクエストによって達成された最終的な差分が分かればいいので、コミットを1つにまとめます。
逆に1つにまとめなかった場合、取り込んだ側のコミット履歴に複数のコミット履歴が追加されて可読性が落ちます。

    git rebase -i まとめたいコミットの中の1番古いコミットの1つ前のコミットハッシュ

上記のコマンドを実行するとvimが起動します。(↑ 1つ前というのは古いの意味です。)
※1つ前を指定するのは、rebaseでは新しい親コミット(base)を指定する必要があるからです。

初めて起動した時はびびると思うのでまずは呼吸を整えましょう。

    pick 13f9ef9 add wip1 // 古
    pick 091a96a add wip2
    pick cba0fda add wip3  // 新
    
    # Rebase 436709c..cba0fda onto 436709c (3 commands)
    #
    # Commands:
    # p, pick <commit> = use commit
    # r, reword <commit> = use commit, but edit the commit message
    # e, edit <commit> = use commit, but stop for amending
    # s, squash <commit> = use commit, but meld into previous commit
    # f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
    #                    commit's log message, unless -C is used, in which case
    #                    keep only this commit's message; -c is same as -C but

1番古いものだけをpickにして他のコミットはsquashかfixupにします(fixupでコミットしている場合はfixup)。
fixupの場合はこれだけで終わりですが、squashの場合は以下のようなvimの画面が起動します。

    # This is a combination of 2 commits.
    # This is the 1st commit message:
    
    aaa
    
    # This is the commit message #2:
    
    bbb
    
    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    #
    # Date:      Wed Nov 23 01:09:40 2022 +0900
    #
    # interactive rebase in progress; onto 2c05697
    # Last commands done (2 commands done):

ここでは、1まとめになったコミットのコミットメッセージを入力できます。
#がついていないところの文言が採用されます。

分岐元を変更する場合

こっちはconflictを解消するときぐらいしか使っていません。
rebaseすることによって、分岐元を変更することができます。

↓図を見るとわかりやすいです。
https://git-scm.com/docs/git-rebase#_description

git merge

mergeをすることによって、別ブランチでの変更分を現在のブランチに取り込むことができます。
取り込む側(ours)にcheckoutしてから、取り込まれる側(theirs)の変更分を取り込みます。

    git checkout ours
    git merge theirs

mergeをするとvimが起動し下記のような画面になります。

Merge branch 'dev'
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
~
~                                 

mergeをすることによって、新しいコミットハッシュが作られるのでそのコミットメッセージをここで入力します。
mergeによってできたコミットハッシュは親コミットを2つ持ちます。

git stash

業務をしていく中で、同時に複数のissueに着手することが結構あると思います。
一旦この作業は(コミットせずに)退避しといて別issueやろうかななんて時にとても便利です。

最初にstashを教えてもらった時、感動しました。

こちらの記事を読めば大体わかります。

新しく作成されたファイルなどはuntracked(gitから追跡されていない状態)なので、git stashだけでは退避されません。
untrackedなファイルも一緒に退避したい場合はgit stash -uで退避することができます。

1
0
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
1
0