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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so you've pushed a git commit that you don't like to a remote repository like GitHub or gitlab or bitbucket and you want to undo that pushed commit well how do you do it well you've really got two different options and which option you choose depends on how many other people might be pushing or pulling to that remote repository where you want to undo that g commit if you just want to completely get rid of that get commit and you have full control over that repository and nobody else has done a push or pull since you did that unfortunate commit well you can just use a get reset and that'll clean everything up completely however if you're on a team and other people may have been pushing and pulling from or to that repository you're going to have to do a git revert now that doesn't actually delete that git commit on the server so if you wrote something in a commit that was unkind about your boss well it's still going to be there if they want to go in and search it that's your lesson for writing anything bad about your boss in a git commit but the git revert will make it look like that commit never happen so essentially it gives you the effect of undoing a previous git commit hi I'm Cameron McKenzie I'm the editor-in chief over at the serers side.com and I have to be one of the world's biggest git Advocates and a lot of people ask me how do you undo a commit that you've already pushed to the server I like reset revert is probably safest I'm going to show you both of them but reset is what I'm going to start with right now so if you have full control over your remote GitHub gitlab bit bucket repository nobody else is pushing or pulling from that repository since you push that commit that you want to undo the best easiest simplest way to undo that pushed commit is to use the git reset method now I've got a git repository up here on GitHub it's called salmon and it's called salmon because I was talking a little bit about Upstream branches and nothing goes Upstream like a salmon and I'm just going to clone it I'm just going to bring it down onto my local file system if you're undoing a get commit you're probably familiar with this uh whole git cloning thing but there I go right there on my file system I have the same G commit history locally as there is on the server now let's add a bad file and then do a bad commit and push it so we can undo it I'm going to add a new file bad file is what we will call it uh I now need to do a get commit uh but of course before I do that get commit I got to do a get add then do the get commit dasm and I'll call this bad commit to undo after a push that's a long get commit message but you get the idea and now I am going to push that bad commit up to the server that goes up to GitHub and if I come over here and do a little refresh we can see boom all of a sudden we've got that bad file and if we actually look at our get commit history compare it to the history over on our file system uh we'll see that that bad commit a97 c34 f is up there on the server and that is the one that we want to get rid of so how do we do it well you just go into your repository you take a look at the git history so git log on line and locally you reset to the kit commit before that bad commit happened so f8 b2015 is before that bad commit so I'll say get reset and I'm going to do the get reset hard cuz uh we don't do the soft one one here the reset hard will take your commit history back it'll also clear out the git staging index and it'll reset the file system so keep your mind my file system after I type in f8 b2015 I will click enter notice that bad file boom goes away we have now done a reset on our local machine and if I bring in that git log on line maybe even compare it to the old git log one line You'll see that locally commit a97 c34 f is gone it's been undone but up here on the server not so lucky we still need to undo that get commit that's been pushed to GitHub so how do we do it well all we have to do now is just do a get push so do a get push and in fact we have to say get push with Force we are changing the get commit history that's up on that remote repository git's not going to like it if you just try and do a push right now like for example if I try and do a push right now it's going to say to me hey uh updates were rejected because the tip of your current branch is behind we've messed up the git commit history with the reset but you can always do a git push with Force it's a little unchristian but sometimes just have to pull out the force we do the force this says look I know this is going to mess up the git commit history I know it's not a safe operation but I really need to undo that pushed git commit GitHub says okay and boom that commit a97 c34 f is gone it's been completely removed from the server so that is how you undo a git commit you use that git reset command but but again it's unsafe and if other people have pushed and pulled to and from this repository since undoing that get commit well the next time they interact with that repository they're going to be very mad at you because their git commit history is going to be all messed up now the other option is to do a git revert which actually doesn't get rid of that git commit the git commit still stays on the server but you undo all of the changes associated with that git commit so it looks as though you've undone that pushed get commit but the commit is still in history that is what I'm going to show you how to do next when you undo a push commit with a git reset command you're performing an unsafe operation because you're changing the git commit history in an irreparable way a better approach is to use the git revert command and I'm going to show you how to do that right here I've got a GitHub repository it's cloned down to my local machine I'm going to add a bad file to this repository then I'm going to do a git add dot I'll do the git Commit This is the uh bad commit to undo after push and now I will push that to my remote repository okay so those are just the commands to push a bad commit to the server so that we can undo it if I take a look at the git log here you'll notice that the bad offending commit is ECC 3C DB that's the one that I want to undo that's the one that was pushed to the server and if I come over here to the server and take a look at my commit history we'll see that well right there is ecc3 cdb that's the one I want to get rid of so how do you do it well this is what you do you do a get revert command and you revert convert that offending commit you provide the ID of that offending commit ECC 3C DB I think is it do I have the D's and B's all configured correctly I think I do uh watch uh the file system when I click enter here notice that the bad file has gone away it asks me for a get commit message I'm going to accept the default and just type in Colon Q to get me add here and you can see that well the files that were edited in that bad committer removed the git revert surgically removes all of the changes that happened in that offending commit now it does it without changing the git commit history in an irreparable way it doesn't go back in time it actually adds a new commit watch this I'm going to open up a file system here a new folder a new get back Dash I'll do get log on line and when you compare that to the original you'll see that well we've actually got a new commit ecc3 cdb is still in my commit history but all of the changes from that commit have been undone and we've kept that in history because just in case other people have been pushing and pulling to the repository they've got that commit if we do a get push force and reset and get rid of that it's going to mess up their uh commit histories as well they'll be very mad at you so this was a very safe operation now I can do a get push to the server I don't need to do the push with Force this is a much friendlier approach to pushing all of the changes go up to the server when I come back to GitHub and click refresh you notice when I look at the code all of the files are in exactly the same state as they were before I've essentially undone the pushed git commit that went up to GitHub by using that git revert command now again that commit is in the history so if you there was something in there or you said something nasty about your boss or something like that that's still in the history so you know get doesn't forget um the reset can get you around that but the revert doesn't it still has that history and if you look here you'll still see that bad commit uh ecc3 cdb still there in the history but the files are in a state that would reflect that that git commit that was pushed is undone so there you go that's how easy it is to undo a git commit that was pushed to GitHub or gitlab or bit bucket if you enjoyed that tutorial um I'd ask you to do two things one when you talk talk about me say nice things and the other is check out my git and GitHub tutorial that I just recently published on YouTube I've also got one on git and gitlab as well both are about two hours long and very very detailed about git and GitHub and gitlab and uh if you watch them you'll become very very dangerous with these tools if you're interested in me you can always follow me on Twitter at Cameron mcz I do have a newsletter where I'm talking about some of the latest things going on world of software development including this upcoming new programming language named Mojo which is going to replace Python and completely change the artificial intelligence machine learning landscape so sign up for that cuz if you don't get up to speed on Mojo you're going to get left behind in the next few years um and of course uh I do have a couple of books behind me you see my hibernate made easy book also one in the Simpson called Pickering and Springfield they're both on Amazon and of course you also see uh Darcy Clute scrum Master certification guide it's available on Amazon a number of people that have read that book have been scoring 100% on the scrum Master certification exam so if you're agile or you know someone who's interested in getting scrum Master Certified that's definitely the book that you want to uh get uh and start reading um and of course if you're watching this video you're probably watching it on YouTube so the last thing I'd ask is subscribe on the YouTube
Info
Channel: Cameron McKenzie
Views: 2,205
Rating: undefined out of 5
Keywords: cameronmcnz, theserverside, java, jakarta, jakartaee, git, github, gitlab, dvcs, versioncontrol, tortoisegit, gitgui, atlassian, bitbucket, linux, azure, devops, devopstools, gitops, gitopstools, azuredevops
Id: mSrxBJaJwGA
Channel Id: undefined
Length: 12min 24sec (744 seconds)
Published: Wed Jan 31 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.