Skip to content

Reset a branch to a specific tag in Git

Introduction

git reset is a powerful command in Git that allows you to undo changes in your working directory and reset the repository to a specific commit. This is useful when you want to discard commits and get back to a particular state in your project's history.

Scenario

Let's consider a scenario where you have two branches: main and develop. The main branch was initially in a good state, but due to a mistake, some commits were accidentally merged into it instead of the develop branch. Now, you need to reset the main branch to its original state.

Steps to Reset main Branch

Follow these steps to reset the main branch:

  1. Check the Status:

    git status
    

  2. Switch to main Branch:

    git checkout main
    

  3. View Commit History:

    git log --oneline
    

  4. Perform the Reset:

    git reset --hard 592ac92
    
    Replace 592ac92 with the commit hash you want to reset to.

  5. Force Push to Remote:

    git push -f origin main
    
    This forcefully overwrites the existing history in the upstream repository.

Conclusion

Using git reset can be a lifesaver when you need to undo changes and get back to a specific commit. However, use it with caution, especially when force-pushing, as it can rewrite history.