Avas LogoAvas

Mastering GitHub Commits Tips for Efficient Editing and Version Control

Imagine you want to go back in time and change your commits, delete them, merge multiple commits etc. All this can be achieved using interactive rebase which makes it a powerful tool.

What is interactive rebase?

It gives an opportunity to alter older individual commits. It's like git commit --amend on steroids.

Scott Chacon, in his book Pro Git, describes interactive rebasing as follows:

Sometimes the thing fixed … cannot be amended to the not-quite perfect commit it fixes, because that commit is buried deeply in a patch series. That is exactly what interactive rebase is for: use it after plenty of [work has been committed], by rearranging and editing commits, and squashing multiple commits into one.

How do we start

To start an interactive rebase, we need to tell Git which commits to modify and we do this by referencing the commits as shown below.

git rebase -i HEAD~n
  • Where n is the recent number of commit messages that you want to edit

Lets look at an example.

1. Open commit messages
git rebase -i HEAD~1

This opens a screen with commit message preceded with letter pick

choose rebase option

It also shows a list of commands that you would want to use as per your usecase.

choose rebase option

2. Replace pick with reword

Replace pick with reword for all the commit messages that you want to edit. In this case, we are editing 1 message only.

choose rebase option

3. Editing git commit message

Edit git commit

After editing commit message, Press Ctrl + O & Enter then Ctrl + X if you are in nano editor or Press Esc & type :wq if you are in vim editor respectively.

4. Verify changes using git log

git log

After editing commit message, push the changes.

More info

https://www.youtube.com/watch?v=ElRzTuYln0M&t=219s


Medium Article Link