Altering commits with Git Rebase: The rebase

Part 2 of using rebase unconventionally to alter previous commits in the project history

Altering commits with Git Rebase: The rebase

Photo by Yancy Min on Unsplash

Continuing from where we left off in part 1: The setup of this two-part rebase series, we started with the interactive rebase for the project, wherein we set up commands for each of the commits we wish to alter during the rebasing. Let's now deep dive into the actual rebasing and get the know-how of the art of altering commits!

Blast off again!

The rebase

Now the terminal will take you through each of the commits selected for rebasing, starting from the base commit. In this section, we will go through an example for each of the commands specified before the rebase.

The pick command does not do anything and just includes the commit in the final history and it will be done automatically, and the next non-pick commit will be shown. So let's move on to the rewording of Commit 0.

Rewording a commit

Once the rebase starts, the editor would display the current commit message for Commit 0, you can edit it to alter the commit message or leave it as it is.

Commit Message 0

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

Hit I to enter the INSERT mode. Modify the commit message to say "Altered Commit Message 0".

Altered Commit Message 0

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

Hit Esc and type :wq to save the changes. The commit is saved successfully and the updated commit message is shown, note that the commit date would be updated to the current date-time. (It would obviously!)

$ [detached HEAD edbs23s] Altered Commit Message 0
 Author: <Author>
 Date: <Date>

You can amend the commit now, with
  git commit --amend '-S'

Once you are satisfied with your changes, run
  git rebase --continue

Enter the git rebase command with the --continue flag to go to the next commit and continue with the rebase.

$ git rebase --continue

Editing a commit

Next comes Commit 1, for which we have passed the edit command. This allows us to modify all aspects of the commit i.e. change the files, dates, authors and even the commit message. It is as good as making the same commit once again but with its properties modified.

Here is an entire git commit command that modifies every aspect of the commit:

$ GIT_COMMITTER_DATE="yyyy-mm-ddThh:mm:ss" git commit --amend --date="yyyy-mm-ddThh:mm:ss" --reset-author -m "Altered Commit Message 1"

The breakdown of the above command is as follows:

  • --date: Modify the date when the commit has been authored.

  • --reset-author: Reset the author of the commit to the current author (as set in the git config of the system)

  • -m or --message: Modify the commit message

  • GIT_COMMITTER_DATE: Modify the date when the commit has been committed. (Refer to the box above on the difference between the commit author and committer)

One can also pass the --no-edit flag instead of the commit message to keep it unaltered.

$ GIT_COMMITTER_DATE="yyyy-mm-ddThh:mm:ss" git commit --amend --date="yyyy-mm-ddThh:mm:ss" --reset-author --no-edit

If you wish to modify the files in a commit, just normally edit the files before executing the above commands (this action may cause unwanted effects in the showing of the diffs between the commits prior to and next to the current commit in the history).

Note that, all the above flags are optional and alter a particular property of the commit. You may use only the ones you require depending on the property /properties you wish to modify.

Continue to the next commit using the --continue flag.

Deleting a commit

The final commit in this rebase is Commit 2, which has to be deleted as we have passed the drop command. No action has to be taken at this step, the commit will be automatically removed from the commit history. Use the --continue flag one last time to complete rebasing.

Tip: You can abort the rebasing and discard all changes anytime during the rebase by using the --abort flag.

$ git rebase --abort

Finalizing the changes

While this is not recommended on public repositories, sometimes rebasing is useful to make some critical modifications to the commit history. However, extreme care must be taken while pushing the rebased branch onto the base branch, as this process overwrites any of the old commits which are not a part of the rebased branch.

To integrate the changes, we would have to do a force push onto the base branch:

$ git push -f <remote> <branch>

Conclusion

In this read, we learnt how we can actually use rebasing to alter previous commits and modify the entire commit history of a project (for our own gains :)). But one should keep extreme caution during rebasing else one might end up messing up the commit history. Rebasing just doesn't end here and there's a lot more to explore. This is just the start and I hope that I was able to answer the questions that were put forth at the beginning of this series!