This document discusses two methods for reverting commits in Git: reverting and resetting. It explains that reverting uses a new commit to undo the changes from the previous commit, which is suitable when working on shared branches with a team. Resetting deletes and resets the head pointer to remove the commit from history, using git reset and git push -f, which is suitable for mistakes on private branches that don't impact others. The key is to use reverts for shared code and resets cautiously for only private branches.
1 of 2
More Related Content
Application of Git Revert in different situation
1. Shobhakars Blog #1 for Git Revert 1
Shobhakars Blog #1 for Git
Revert
PROBLEM STATEMENT: Suppose you are working in a tech company where you
may encounter two situations:
1. Working in a Team with Shared Branches:
If you are working in a team where code is regularly pushed to shared
branches, and you need to revert any accidentally pushed code while
maintaining the revert history.
Let's discuss the first situation.
Important Points
1. Push a new commit that reverts the pushed commit.
2. This strategy is suitable for public or shared branches where multiple
developers are pushing their changes.
Commands:
Git revert HEAD
Git push origin main
2. Working on a Private Branch:
If you are working on your private branch, committing daily, and accidentally
pushing code that needs to be reverted without maintaining the history in the
Git repository.
Important Points
1. Delete the pushed commit.
2. This strategy is suitable for private branches that are not dependent on other
team members.
Commands:
2. Shobhakars Blog #1 for Git Revert 2
1. Unstage the pushed commit.
Command: git reset Head~1
2. Discard the changes and move the branch pointer.
Command: git reset --hard HEAD~1
3. Force push to update the remote repository.
Command: git push -f origin main
1. Understanding the Differences and When to Use Each Method:
In both scenarios, the goal is to revert a code commit. However, the context
significantly changes the way you should go about it.
When working on shared branches, the changes you push could affect other
developers. Therefore, pushing a new commit to revert the previous one
ensures that everyone can see what has happened and adjust their work
accordingly. This preserves the history and avoids confusion.
On the other hand, when working on a private branch, you have more
flexibility. You can delete the pushed commit and force push the branch. This
erases the mistake from the history as if it never happened, which is generally
a cleaner approach when the changes aren't impacting others.
Remember, using git push -f (force push) should be done cautiously. It can
potentially overwrite changes pushed by others, which can lead to data loss.
Always ensure you're on a private branch when using it.