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.

Пікірлер: 28

  • @jeetendranavgikar6384
    @jeetendranavgikar6384Ай бұрын

    After watching so many videos for the solution. this video got it right. thank you

  • @cameronmcnz

    @cameronmcnz

    Ай бұрын

    Why would you watch someone else's video? Happy to help!

  • @user-ly9qv8oe9o
    @user-ly9qv8oe9oАй бұрын

    Don't think there will be any other video which will explain it much better than you sir. Thank you

  • @shawnbunch4872
    @shawnbunch48722 ай бұрын

    Clear, concise, and straight to the point. 🐐

  • @cameronmcnz

    @cameronmcnz

    2 ай бұрын

    I do try my best to cover what's important. Nobody seems to want to hear my opinions on middle-east politics in these Git videos, it seems.

  • @Spyros.Charalambous
    @Spyros.Charalambous2 ай бұрын

    It's the most simplified video ever! thank you so much for your time!!!

  • @cameronmcnz

    @cameronmcnz

    2 ай бұрын

    Thanks for the kind words! And given the two different options and the implications of each, I'd say it's anything but simple. If you think it's simple, that's a reflection of the fact that you're smarter than the average bear. 👍

  • @samhkwest
    @samhkwest2 ай бұрын

    Thanks for your demonstration. I've just used this tricks to fix a code merges issue for my work.

  • @cameronmcnz

    @cameronmcnz

    2 ай бұрын

    Happy to have helped. Nothing worse than thinking you Git pushed something you shouldn't have and you're worried you've broken the build!

  • @ABDUL-REHMAN345
    @ABDUL-REHMAN3452 ай бұрын

    Thankyou so much sir ! i'm watching a lot of video's but i don't understand....finally seeing your video and i will understand♥♥

  • @cameronmcnz

    @cameronmcnz

    2 ай бұрын

    Thanks for watching! Honestly, once you get pulls, pushes and commits all under your belt, there's really not much more to being a true Git expert!

  • @pravinprince3221
    @pravinprince32213 ай бұрын

    Thank you sir it is one of the best video in the Git series, it is so helpful to many of us sir, thanks again sir

  • @cameronmcnz

    @cameronmcnz

    3 ай бұрын

    First comment! Thanks for the support. Every comment and like really helps out. It boosts the algo and helps get the videos in front of more people.

  • @vodka7924
    @vodka79242 ай бұрын

    You are a life saver sir, Thank you!!!

  • @cameronmcnz

    @cameronmcnz

    2 ай бұрын

    Happy to help!

  • @keifer7813
    @keifer78138 күн бұрын

    You're born to teach, man. Quick question though: What if the bad commit was the first and only commit? Then there's no commit hash before it to "git reset" to. Or is it still possible somehow? 🤔

  • @user-il7oz8jr7x
    @user-il7oz8jr7x2 ай бұрын

    thanks. your video really helped me

  • @mickey008ful
    @mickey008fulАй бұрын

    Thank you a lot!

  • @cesarmta87
    @cesarmta872 ай бұрын

    Thank you!!!

  • @abujayed8999
    @abujayed89992 ай бұрын

    tnx, life saving.

  • @cameronmcnz

    @cameronmcnz

    2 ай бұрын

    Meh, being a great dev means knowing how to find answers to your problems. You found it. You saved yourself. All I did was record a video.

  • @justin9494
    @justin94942 ай бұрын

    thank you so much!

  • @cameronmcnz

    @cameronmcnz

    2 ай бұрын

    Happy to help! Which approach did you take? Revert the pushed commit or reset it?

  • @justin9494

    @justin9494

    2 ай бұрын

    @@cameronmcnz kind of a bummer because I skipped the intro and didn't know there was a git revert after I've done git reset. I wanted to grab the code that didnt break my app but well, my fault for skipping. Thanks!

  • @cameronmcnz

    @cameronmcnz

    2 ай бұрын

    @@justin9494 locally you can still find the orphaned commit created by the reset with the reflog command. Ten reset back to that and then do a revert. A lot of with though if what you’ve got works. Maybe I should make the intros shorter?

  • @sagar.sreepuram1960
    @sagar.sreepuram19603 ай бұрын

    How to undo commit if we committ before push and get an error

  • @cameronmcnz

    @cameronmcnz

    3 ай бұрын

    You can run into so many different scenarios. What's the error? I've probably run into it.

  • @pclovers219
    @pclovers2192 ай бұрын

    Sir when i pass the command git revert (commit id ) of any previous 3 or 4 commit it give me a merge conflict and in it there is a (current code ) but after ======== in the file there is nothing, no incoming change what can i do to go back to my previous state? Hope so you understand my problem 😂