Skip to main content
  1. Learn
  2. Software Development
  3. Guides
  4. Git tutorial
  5. Git basics
  6. Undoing changes
  7. Resetting commits
GuidesSoftware DevelopmentBacklog
Git

Project and code management together.

Resetting commits

Use the git reset command to make the HEAD point to a previous commit. You can specify the scope for the reset command by going into reset mode.

Diagram of removing commits.
Use git reset to remove unnecessary commits.

When to reset commits

Removing commits with the git reset command is a strategic process in Git used primarily to undo local commits that have not yet been pushed to a remote repository. This command is especially useful during local development when you realize that recent commits need to be modified or removed entirely before they are shared with others. By using git reset, you can effectively reset the HEAD pointer to a previous commit, thereby undoing the commit and adjusting the state of your repository.

However, it's crucial to exercise caution with this action, particularly when commits have already been pushed to a shared repository. Resetting commits alters the commit history, potentially causing complications if others have already based their work on the commits you intend to remove. This action can lead to synchronization issues and disrupt collaborative workflows, as Git will not automatically synchronize the reset history across all repositories.

Therefore, before using git reset to remove commits, it's advisable to communicate with your team and consider alternative approaches, such as git revert for undoing changes in a safer manner without altering the commit history. This approach preserves the integrity of the commit history and avoids conflicts that may arise from rewriting shared history.

Subscribe to our newsletter

Learn with Nulab to bring your best ideas to life