LoginSignup
0
2

More than 3 years have passed since last update.

rebaseでcommitが消えた…?

Last updated at Posted at 2019-09-15

内容

commitrebaseされず、想定の結果にならなかったので、事象や解決策などをまとめました。
rebaseの挙動についてはこの記事が参考になります。

やりたかったこと

masterfeature/Arebaseしたかった。

git rebase master feature/A

実行結果

あれ…feature/Acommitrebaseされていない…

Before

* commit 414b95af83ab984b20147e0094ed8eade31f348b (origin/master, master)
| Author: ****
| Date:   Sat Sep 14 22:02:50 2019 +0900
|
|     D
|
| * commit 12b5e389f870e565beb195e6b9deda974daacac0 (HEAD -> feature/A, origin/feature/A)
| | Author: ****
| | Date:   Sat Sep 14 21:43:10 2019 +0900
| |
| |     C
| |
| * commit 53af224f9c0f1c326dbdd20efc85edd04fe84e36
| | Author: ****
| | Date:   Sat Sep 14 19:37:50 2019 +0900
| |
| |     B
| |
| * commit 650053d9adbaa1b33a3a6b72504cbcad9ff23eb9
|/  Author: ****
|   Date:   Sat Sep 14 19:37:35 2019 +0900
|
|       A
|
* commit 78369ea68187c501d4b3fa7b453a9ce3eea95ca1
  Author: ****
  Date:   Sat Sep 14 19:35:28 2019 +0900

      first commit

After

* commit 414b95af83ab984b20147e0094ed8eade31f348b (HEAD -> feature/A, origin/master, master)
| Author: ****
| Date:   Sat Sep 14 22:02:50 2019 +0900
|
|     D
|
| * commit 12b5e389f870e565beb195e6b9deda974daacac0 (origin/feature/A)
| | Author: ****
| | Date:   Sat Sep 14 21:43:10 2019 +0900
| |
| |     C
| |
| * commit 53af224f9c0f1c326dbdd20efc85edd04fe84e36
| | Author: ****
| | Date:   Sat Sep 14 19:37:50 2019 +0900
| |
| |     B
| |
| * commit 650053d9adbaa1b33a3a6b72504cbcad9ff23eb9
|/  Author: ****
|   Date:   Sat Sep 14 19:37:35 2019 +0900
|
|       A
|
* commit 78369ea68187c501d4b3fa7b453a9ce3eea95ca1
  Author: ****
  Date:   Sat Sep 14 19:35:28 2019 +0900

      first commit

問題点

実はfeature/Acommitは全て空コミットでした、
デフォルトだと空コミットrebaseの際に削除されてしまう。

解決策

rebase時に--keep-emptyオプションを指定する。

git rebase --keep-empty master feature/A

実行結果

想定通りの結果になりました!

After

* commit 1bb327740bf6cb6e25e0afb6500ed9c1020503c0 (HEAD -> feature/A)
| Author: ****
| Date:   Sat Sep 14 21:43:10 2019 +0900
|
|     C
|
* commit 5bb9c9e4e943deccbb5e7ddeb4ac07caf158aeb9
| Author: ****
| Date:   Sat Sep 14 19:37:50 2019 +0900
|
|     B
|
* commit ad32378d763b315b419a86f4303f61f147886c11
| Author: ****
| Date:   Sat Sep 14 19:37:35 2019 +0900
|
|     A
|
* commit 414b95af83ab984b20147e0094ed8eade31f348b (origin/master, master)
| Author: ****
| Date:   Sat Sep 14 22:02:50 2019 +0900
|
|     D
|
| * commit 12b5e389f870e565beb195e6b9deda974daacac0 (origin/feature/A)
| | Author: ****
| | Date:   Sat Sep 14 21:43:10 2019 +0900
| |
| |     C
| |
| * commit 53af224f9c0f1c326dbdd20efc85edd04fe84e36
| | Author: ****
| | Date:   Sat Sep 14 19:37:50 2019 +0900
| |
| |     B
| |
| * commit 650053d9adbaa1b33a3a6b72504cbcad9ff23eb9
|/  Author: ****
|   Date:   Sat Sep 14 19:37:35 2019 +0900
|
|       A
|
* commit 78369ea68187c501d4b3fa7b453a9ce3eea95ca1
  Author: ****
  Date:   Sat Sep 14 19:35:28 2019 +0900

      first commit

深掘り

rebaseは、

  1. 移行元のcommitを一時退避する。
  2. 移行先のブランチに移動する。
  3. 移行先に1.で一時退避したcommitを適用

という動きをするようです。
rebaseのデフォルトの動きは、3.でファイル差分が存在しない時はcommitしない挙動のようです。

追加で気になったこと

cherry-pickで空コミットを適用しようとした時はどうなるのか。

やってみたこと

cherry-pickで空コミットを取り込んでみました。

git cherry-pick 414b95af83ab984b20147e0094ed8eade31f348b

実行結果

commitできるものがないよ〜。
とにかくcommitしたかったら空コミットしてね。
それがイヤだったらresetしてね」
と言っているようです。

On branch feature/A
You are currently cherry-picking commit 414b95a.

nothing to commit, working tree clean
The previous cherry-pick is now empty, possibly due to conflict resolution.
If you wish to commit it anyway, use:

    git commit --allow-empty

Otherwise, please use 'git reset'

解決策

cherry-pick--allow-emptybgitオプションを指定しましょう。

git cherry-pick --allow-emptybgit 414b95af83ab984b20147e0094ed8eade31f348b

まとめ

  • rebase時に--keep-emptyオプションを指定しないと空コミットがrebaseされない。
  • cherry-pick時に--allow-emptybgitオプションを指定しないと手動で別作業が必要になる。

今回の事象がgithubのプルリクで問題になるケースの記事を見つけたので参考にしてください。

参考記事

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