Introduction
Git has different mechanisms for rewriting history, which include git commit --amend, git rebase and git reflog commands. Reading our tutorial will help you to better understand the functions of these commands and their particularities.
Changing the last commit with git commit --amend
You can often forget to format your commit, stage files or make a mistake in your commit log message. Here git commit --amend comes to help. Executing this command will help you to correct such minor mistakes. In general, this command is used for changing the last commit. Execute the following command to combine staged changes with the previous commit:
git commit --amendIt’s important to remember that to avoid collaboration problems, you shouldn’t amend commits which are being developed by other users.
Git reflog command
The “reflogs”, which stand for reference logs, are used for recording updates that are made to the tip of branches. With the git reflog command, you can go back to all the commits, including the ones that haven’t been referenced by any branch. Run the command below to view the log:
git reflogThe git reflog command has its own subcommands, which include git reflog show, git reflog expire and git reflog delete.
Git rebase
The git rebase command is used for moving or combining a series of commits to an entirely new base commit. The most important benefit of this command is a clear history, which is important for effective work in Git. There are two modes of the git rebase command: standard and interactive. In standard mode you can use git rebase to apply commits in the current working branch to the passed branch head. With the help of interactive mode you can maintain the project history clean. To run the interactive mode add the -i option to git rebase:
git rebase -i <base-branch>Multiple messages
Each Git commit has a log message that explains what happened in the commit. During an interactive rebase, you can use the following commands to edit commit messages:
- Use the
rcommand, which stands for reword, to pause the rebase playback and rewrite the individual commit message. - The
scommand, standing for squash, will pause all the commits markeds, and you will receive a prompt for modifying the separate commit messages into a combined one. - The
fcommand, which stands for fixup, is similar to squash. The difference is that it will not stop rebase playback for opening an editor to combine commit messages.
Squash commits for a clean history
The squash command specifies the commits that you want to merge into the previous ones. Git opens the text editor and prompts for combining the specific commit messages. The following infographic perfectly demonstrates the process.

The commits that have been modified with a git rebase command have different IDs than the original ones. If the previous commits are rewritten, the commits which are marked with pick will have an entirely new ID.
Practice
Which of the following statements are true regarding the mechanisms for rewriting history in Git?