Introduction

  1. Introduction
  2. Changing the last commit with git commit -- amend
  3. Git reflog command
  4. Git rebase command

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 its particularities.

Changing the last commit with git commit -- amend

You can often forget to format your commit, stage files or make 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 commits. Execute git commit --amend command to combine staged changes with the previous commit. It’s important to remember that for avoiding the 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 thegit reflog command, you can go back to all the commits, including the ones that haven’t been referenced by any branch. 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 the clear history, which is important for an effective work on Git. There exist two modes of 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.

Multiple messages

Each of Git commits has a log message, which explains what happens in the commit. While rebasing run the following commands on the commits for editing commit messages.

  • Use “r” command, which stands for reword, to pauses the rebase playback and rewrite the individual commit message.
  • “s” command, standing for squash, will pause all the commits marked s, and you will receive a prompt for modifying the separate commit messages into a combined one.
  • “f”, 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. Here Git opens the text editor and prompts for combining the specific commit message. 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.




Do you find this helpful?

Related articles