Skip to main content
  1. Learn center
  2. Software Development
  3. Guides
  4. Git tutorial
  5. Git commands & settings
  6. Git commit history commands
GuidesSoftware DevelopmentBacklog
Git

Project and code management together.

Git commit history commands


Modify previous commit and messages

$ git commit --amend

Add the --amend option to overwrite the latest commit of the branch you’re working on.

When there are no files in an index, you can recommit the previous commit by adding the --amend option, and you’ll be prompted to edit the existing commit message.

See:

Modify & move past commit and messages

$ git rebase -i <commit>

Add a commit hash and a list of all commits up to the latest commit will be listed. Find the commit you wish to modify, and change that line from pick to edit then save and quit.

Next, add the --amend option to commit. A screen for adding a message will be displayed. Modify the message.

$ git commit --amend

Finally, add the --continue option to run rebase.

$ git rebase --continue

See:

Exit rebase

$ git rebase --abort

By adding the --abort option, you can exit the rebase operation.

Show reference log

$ git reflog

The reflog command allows you to see a list of commits that HEAD used to indicate in the past.

08084a5 HEAD@{0}: commit: append description of the pull command
  99daed2 HEAD@{1}: commit: append description of the commit command
  48eec1d HEAD@{2}: checkout: moving from main to issue1
  326fc9f HEAD@{3}: commit: append description of the add command
  48eec1d HEAD@{4}: commit (initial): first commit

Both deleted and successful commits gathered by rebase will be displayed.

Show reference log of branch tip

$ git reflog <ref>

This will show a list of commits for each time the tip of <ref> has changed, similar to below.

445e0ae issue1@{0}: commit (merge): Merge branch 'main' into issue1
1c904bd issue1@{1}: commit (amend): modify description of the pull command
08084a5 issue1@{2}: commit: append description of the pull command
99daed2 issue1@{3}: commit: append description of the commit command
48eec1d issue1@{4}: branch: Created from 48eec1ddf73a7fb508ef664efd6b3d873631742f

We can see the rebase history of both deleted and existing commits.

Remove previous commit

$ git reset --hard HEAD~

See:

Reset rebase

$ git reset --hard <commit>

Use the reflog command to look up commits before they were rebased. Identify the commit hash or the value of [HEAD@{number}] of the commit that occurred before the rebase. In this example, 71bdfbd and HEAD@{4} are the references to the commit.

a51f8d2 HEAD@{0}: rebase -i (finish): returning to refs/heads/dev
  a51f8d2 HEAD@{1}: rebase -i (squash): update 1
  3a273e1 HEAD@{2}: rebase -i (squash): updating HEAD
  f55ef69 HEAD@{3}: checkout: moving from dev to f55ef69
  71bdfbd HEAD@{4}: commit: update 2
  f55ef69 HEAD@{5}: commit (amend): update 1

Add the hash value (71bdfbd and HEAD@{4}) to <commit> when running the reset command.

The head position of the branch will now move to the commit before the rebase was executed. The state of the branch now will be identical to that before rebase was executed.

Cancel previous reset

$ git reset --hard ORIG_HEAD

ORIG_HEAD refers to the commit before reset took place. You can revert the previous reset by using reset to ORIG_HEAD.

Copy commit from another branch

$ git cherry-pick "<commit>"

The commit identified by its hash will be copied to the current branch.

See:

Search for commit message

$ git log --grep "<pattern>"

Displays commits with text specified in <pattern>.

Subscribe to our newsletter

Learn with Nulab to bring your best ideas to life