![]() |
Undoing pushed commits in Git is a common task that developers encounter when they need to rectify mistakes or change their commit history. Depending on the specific scenario and requirements, there are different approaches to undoing pushed commits. Here, we will discuss various methods to achieve this, ensuring that you can handle any situation effectively. Scenarios:
Approach 1. Revert the CommitThe git revert command is used to create a new commit that undoes the changes made by previous commits. This is a safe method because it doesn’t change the commit history. Step 1: Identify the commit hash you want to revert using git log.Run the revert command: git revert <commit-hash>
Steps 2: Push the new commit to the remote repository:git push origin <branch-name>
This method is ideal for public repositories where rewriting history is not recommended. Approach 2. Reset the CommitThe git reset command can be used to move the HEAD pointer to a previous state, effectively removing commits from the history. This method changes the commit history and should be used with caution, especially on shared branches. Step 1: Identify the commit hash to which you want to reset using git log.Run a hard reset command: git reset --hard <commit-hash>
Step 2: Force push the changes to the remote repositorygit push --force origin <branch-name>
Note: Force pushing can overwrite changes in the remote repository, which might disrupt other developers’ work. Ensure you communicate with your team before performing this action. Best Practices
|
Reffered: https://www.geeksforgeeks.org
Git |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |