0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Gitのコミットを綺麗にしたいけど、リベースが怖かったので練習してみた

Posted at

はじめに

CVS、SVN、の流れでGitが主流になってなんとなくGitを使っていました。
とある現場で、プッシュする前に可能であればコミットログを綺麗にして欲しい。
でも無理そうなら、そのままプッシュしていいから!

とお話を受けたけど当時、余裕もなくGitに不慣れだったので、そのままプッシュしていました。

そこから流れて、コミットログを綺麗にしてプッシュする。という事を求められる事も無かったのですが、改めてやってみたいな。と思ったので、ちょっとやってみました。

シチュエーション

test2機能を作っていて、Aサービスを追加してコミットしたあと、あ、Aサービス関連でコミットが漏れた!ので追加コミット!

とすると、Aサービス追加というコミットが2つに分離してしまった。

ので、リベースでコミットを集約

やりたきことの流れ

2つ目の漏れによる追加コミットを1個目のコミットに統合して、プッシュ!

やってみる

事前準備

git switch develop
git branch feature/test2-A
git switch feature/test2-A
echo 'A-service-method' >> A-Service.txt
echo 'A-serviceA-DTO' >> A-Service-DTO.txt

1個目のコミット

Aサービスをコミットしたけど、A-service-DTOをコミットし忘れた。

git add A-Service.txt
git commit -m 'Aサービスの追加'

2個目のコミット

追加でA-service-DTOをコミット。

git add A-Service-DTO.txt
git commit -m 'AサービスのDTO追加'

ログは?

git log --oneline
$ git log --oneline
8d6d29b (HEAD -> feature/test2-A) AサービスのDTO追加
a4ab18c Aサービスの追加

2個目のコミットを1個目のコミットに合体!

git rebase -i HEAD~2
pick a4ab18c Aサービスの追加
pick 8d6d29b AサービスのDTO追加

# Rebase 3ef6ba0..8d6d29b onto 3ef6ba0 (2 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
#                    opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label

pick a4ab18c Aサービスの追加
squash 8d6d29b AサービスのDTO追加

に変更して、wq
続いてメッセージも、

# This is a combination of 2 commits.
# This is the 1st commit message:

Aサービスの追加

# This is the commit message #2:

AサービスのDTO追加

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
~ 中略 ~

をいかに変更(2個目はコメントアウト)

# This is a combination of 2 commits.
# This is the 1st commit message:

Aサービスの追加

# This is the commit message #2:

#AサービスのDTO追加

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.

まとめたログは?

おー!1個に変わった!

git log --oneline
$ git log --oneline
f4d5b6a (HEAD -> feature/test2-A) Aサービスの追加

ブランチをプッシュ

リモートに初プッシュなので、強制オプションが無くても正常にプッシュ完了!

git push origin feature/test2-A
$ git push origin feature/test2-A
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 428 bytes | 428.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
remote:
remote: Create a pull request for 'feature/test2-A' on GitHub by visiting:
remote:      https://github.com/tarosuke777/sandbox/pull/new/feature/test2-A
remote:
To https://github.com/tarosuke777/sandbox
 * [new branch]      feature/test2-A -> feature/test2-A
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?