How to Undo a Pushed Git Commit - Reset & Revert a Git Commit After Push

Описание к видео How to Undo a Pushed Git Commit - Reset & Revert a Git Commit After Push

Need to undo a pushed Git commit from GitHub, GitLab, Bitbucket or CodeCommit? Well, there are two ways to revert a pushed commit in git.

You can revert a commit pushed to GitHub or GitLab, but that doesn't delete the pushed commit. It just resets the state of your Git repo.

Alternatively you could do a Git reset to undo a commit pushed to GitHub or GitLab or CodeCommit. That deletes the pushed Git commit for good, but it also messes up the Git commit history, so it's dangerous.

Should you use git reset or revert to undo a pushed Git commit?

The Git revert command is the safest way to undo a Git push. The Git reset command completely deletes the pushed commit, so that one is best if you need everything gone, but it does mess up the commit history.

Take a look at these two approaches on how to undo or revert a pushed commit in git and find out which is best for you!


To undo a pushed Git commit, you can follow these general steps. Keep in mind that this process will alter the commit history, so use caution and make sure it's appropriate for your situation.

1. Locate the commit you want to undo:
- Use `git log` to find the commit hash of the commit you want to undo.

2. Create a new commit that undoes the changes:
- Use the following command, replacing `-commit-hash-` with the hash of the commit you want to undo:
git revert commit-hash

- This will create a new commit that undoes the changes introduced by the specified commit.

3. Push the new commit to GitHub:
- After creating the revert commit, push it to the remote repository on GitHub.

git push origin master

- Replace `master` with the branch name if you are working on a different branch.

4. Verify the changes on GitHub:
- Go to your GitHub repository and check if the new revert commit is pushed.

By creating a revert commit, you keep a record of the fact that a change was undone. This is often a safer approach than using commands like `git reset` or `git push --force`, which can rewrite history and cause problems if others have already pulled the changes.

Using `git reset` and force-pushing to undo a pushed commit is generally discouraged because it rewrites the commit history. When you force-push, you replace the existing history with a new one, and this can lead to several issues:

1. Collaboration Issues:
- If others have already pulled the changes you force-pushed, they will have the old commit history. When they try to push their changes, it can result in conflicts, and they may need to force-push as well. This creates a messy and confusing collaboration environment.

2. Loss of History:
- Rewriting history removes the existing commits from the branch, making it difficult to track changes over time. This can be especially problematic if you're working on a shared branch with multiple contributors.

3. Potential Data Loss:
- If the commit you want to undo contains important changes or information, force-pushing without caution can lead to unintentional data loss.

4. Build and Deployment Issues:
- Continuous integration (CI) systems and deployment pipelines often rely on commit history to trigger builds or deployments. Rewriting history can disrupt these processes.

5. Security Concerns:
- If you force-push changes to a shared branch, it can make it harder to audit and track changes, potentially introducing security concerns.

Instead of force-pushing, the recommended approach is to use `git revert` to create a new commit that undoes the changes introduced by the problematic commit. This way, the history remains intact, and collaborators can easily understand what happened.

In summary, force-pushing should be used cautiously and primarily in cases where you're confident that it won't create issues for collaborators. For public or shared branches, using `git revert` is generally a safer and more collaborative way to undo changes.

If you need to completely remove the commit from the history (which is generally not recommended if the commit has been shared with others), you might consider using `git reset` and force-pushing, but this should be done with caution and only in situations where you're sure it won't cause issues for collaborators.

If you need to reset or undo pushed Git commits on GitHub, GitLab or Bitbucket, heed this advice.

Комментарии

Информация по комментариям в разработке