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

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 ก.ค. 2024
  • 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.

ความคิดเห็น • 33

  • @jeetendranavgikar6384
    @jeetendranavgikar6384 2 หลายเดือนก่อน +4

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

    • @cameronmcnz
      @cameronmcnz  2 หลายเดือนก่อน +1

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

  • @user-ly9qv8oe9o
    @user-ly9qv8oe9o 2 หลายเดือนก่อน +1

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

  • @shawnbunch4872
    @shawnbunch4872 3 หลายเดือนก่อน +2

    Clear, concise, and straight to the point. 🐐

    • @cameronmcnz
      @cameronmcnz  3 หลายเดือนก่อน +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.Charalambous 3 หลายเดือนก่อน +1

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

    • @cameronmcnz
      @cameronmcnz  3 หลายเดือนก่อน +1

      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
    @samhkwest 3 หลายเดือนก่อน +1

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

    • @cameronmcnz
      @cameronmcnz  3 หลายเดือนก่อน

      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!

  • @amalkrishnas1696
    @amalkrishnas1696 10 วันที่ผ่านมา +1

    thank you, great video

  • @ABDUL-REHMAN345
    @ABDUL-REHMAN345 3 หลายเดือนก่อน +1

    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  3 หลายเดือนก่อน +1

      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!

  • @vodka7924
    @vodka7924 3 หลายเดือนก่อน +2

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

    • @cameronmcnz
      @cameronmcnz  3 หลายเดือนก่อน +1

      Happy to help!

  • @pravinprince3221
    @pravinprince3221 4 หลายเดือนก่อน +2

    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  4 หลายเดือนก่อน

      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.

  • @user-il7oz8jr7x
    @user-il7oz8jr7x 3 หลายเดือนก่อน

    thanks. your video really helped me

  • @mickey008ful
    @mickey008ful 2 หลายเดือนก่อน

    Thank you a lot!

  • @cesarmta87
    @cesarmta87 3 หลายเดือนก่อน

    Thank you!!!

  • @utuberyder
    @utuberyder 17 วันที่ผ่านมา +1

    Perfect one.

    • @cameronmcnz
      @cameronmcnz  16 วันที่ผ่านมา

      Always happy the help. Thanks for watching!

  • @abujayed8999
    @abujayed8999 3 หลายเดือนก่อน +1

    tnx, life saving.

    • @cameronmcnz
      @cameronmcnz  3 หลายเดือนก่อน +1

      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.

  • @keifer7813
    @keifer7813 หลายเดือนก่อน

    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? 🤔

  • @justin9494
    @justin9494 3 หลายเดือนก่อน +1

    thank you so much!

    • @cameronmcnz
      @cameronmcnz  3 หลายเดือนก่อน

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

    • @justin9494
      @justin9494 3 หลายเดือนก่อน +1

      @@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  3 หลายเดือนก่อน

      @@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?

  • @rosaal9030
    @rosaal9030 24 วันที่ผ่านมา

    Sir can you please help me i use azuredev and I did revert from there and after that i go to visual studio code and I did git revert then again i did push . I mean i push the unwanted commit again what should I do

  • @pclovers219
    @pclovers219 3 หลายเดือนก่อน

    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 😂

  • @sagar.sreepuram1960
    @sagar.sreepuram1960 4 หลายเดือนก่อน +1

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

    • @cameronmcnz
      @cameronmcnz  4 หลายเดือนก่อน

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

  • @spacetabs
    @spacetabs วันที่ผ่านมา

    Using git reset with push -f (force) without understanding what you are doing, extremely bad things will happen.