Git
version control
git reset
git revert
local commits

Throw away local commits in Git

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Understanding and Removing Local Commits in Git

In Git, a commit is a fundamental part of the version control workflow. It represents a snapshot of your project's changes at a particular point in time. However, there are scenarios where you might need to discard local commits. This could happen because of a mistake, a change in requirements, or simply to tidy up your commit history before pushing changes to a shared repository. This article explores the technical methods to throw away local commits in Git, with examples illustrating the procedures.

Why Discard Local Commits?

Before diving into the technicalities, it's essential to understand why you might need to discard commits:

  1. Error Correction: You may have committed erroneous code that you intended to fix in an ongoing branch.
  2. Undo Unnecessary Changes: Sometimes, you might decide that certain changes aren't needed and want to remove their trace.
  3. Commit History Clean-Up: For clean collaboration, you might reframe the commit history for clarity before a PR.
  4. Rebasing: During a rebase, you might choose to discard commits that don't fit anymore with the new base branch.

Common Methods to Discard Local Commits

1. Using git reset

The git reset command is a powerful tool that can help you discard local commits effectively. It comes in three main modes:

  • Soft Reset: Retains changes in the working directory.
  • Mixed Reset: Retains changes in the working directory but unstages them.
  • Hard Reset: Discards all changes permanently.
Example: Reverting a Couple of Commits

Suppose you have created three commits on your feature branch, and upon reviewing, you decide to discard the last two:

bash
1$ git log --oneline
2d41d8cd (HEAD -> feature) Third commit
3c157a79 Second commit
4e7d9ab2 First commit

Soft Reset:

bash
$ git reset --soft HEAD~2

This command moves the HEAD to the first commit and keeps the changes made by the last two commits staged.

Mixed Reset:

bash
$ git reset --mixed HEAD~2

Here, the changes are unstaged, but still available in your working directory.

Hard Reset:

bash
$ git reset --hard HEAD~2

This completely removes the last two commits and any associated changes from your working directory.

2. Using git rebase

Rebasing allows you to rewrite commit history, and during an interactive rebase, you can choose to remove commits.

Example: Interactive Rebase

To remove commits using interactive rebase, execute:

bash
$ git rebase -i HEAD~3

An editor will open, displaying the commits:

 
pick e7d9ab2 First commit
pick c157a79 Second commit
pick d41d8cd Third commit

Change pick to drop for the commits you wish to discard. Save and exit the editor:

 
pick e7d9ab2 First commit
drop c157a79 Second commit
drop d41d8cd Third commit

Comparing Git Reset and Rebase

Below is a table comparing git reset and git rebase for discarding local commits:

CommandModes AvailableKeeps ChangesAlters HistoryUse Case
git resetsoft, mixed, hardyes/noyesDiscard latest local commits
rebaseinteractiveoptionalyesRemove specific commits flexibly

Caveats and Best Practices

  • Back Up Important Data: Always ensure that you have backups of critical data. Once a commit is discarded with a hard reset, it can be difficult to recover.
  • Checkout Only in Local: Only discard commits in your local branches. Avoid using these methods on shared branches to prevent conflicts.
  • Understand Your History: Use tools like git log and git reflog to review and understand your commit history before discarding any changes.

Additional Details

Recovering Discarded Commits

If you accidentally discard a commit, you might be able to recover it using the reflog:

bash
$ git reflog
$ git reset --hard <commit hash>

Alternative: Git Stash

If you want to temporarily remove changes without committing or discarding completely, consider using:

bash
$ git stash

This command stores your changes on a separate stack, allowing you to reapply them later with git stash apply.

Conclusion

Discarding local commits in Git is a common task that can be accomplished through reset or rebase. Understanding the nuances of these commands will help you manage your project's history effectively and keep your workflows clean and organized. Remember to choose the appropriate method based on your specific needs and always proceed with caution to prevent accidental data loss.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.