okuzawatsの日記

Android / Kotlin Enthusiast 🤖

Clean up Recent Git Commit

Sometime we’d like to clean up recent git commit. For example, when we wrote experimental code and found it worked, we’d want to clean up the commits before pushing to the remote repository. In such a case, we should prepare the commits locally before pushing them.

git commit –amend

The first and the simplest option is git commit --amend. The command enables us to edit the recent commit message. If you hit the command, the default editor starts (in my case, it’s Vim), and you can edit the recent commit message. After saving the edited commit message (in my case, :x), the recent one commit message would be updated.

% git commit --amend

git rebase -i

The second option is git rebase -i. The command put some commits together. For example, the command below put the recent two commits together.

% git rebase -i HEAD^^

When you hit the command above, the default editor starts, and you can edit the recent two commit messages.

pick 8b2902d recent commit message
pick 1225f25 awesome commit message

Edit the pick before the commit you’d like to put together to the base commit to squash. After saving, the two commit are put together. There is other options so please refer to the reference.

pick 8b2902d recent commit message
squash 1225f25 awesome commit message

If you want to rebase more commits, you’d like to change the command like this:

% git rebase -i HEAD~2

CAUTION

Be care NOT to edit the commits after pushed to the remote repository. When you edit such commits, the commit hash is changed and you have to push with -f option. -f option is not safe for team development, so you have to think deeply before pushing with -f.


References.

#Git

About me 😎

profile

茨城県つくば市在住のソフトウェアエンジニアです。専門領域はAndroidアプリ開発で、特にアーキテクチャに興味があります。某社でAndroidアプリ開発のテックリードをしています。

Jetpack ComposeによるAndroid MVVMアーキテクチャ入門の著者です。

👉 もっと詳しく

Writing 📝

Android MVVMアーキテクチャ入門

Androidアプリ開発の初学者に向けた、MVVM(Model-View-ViewModel)アーキテクチャの入門書を書きました。初学者の方を確実にネクストレベルに引き上げる技術書です。

👉 もっと詳しく

See Also 👀