Note: this doesn't work if you've already pushed the commit to a public/shared branch, and if you tried other things first, you might need to git reset instead of HEAD~.
![git revert commit git revert commit](https://3.bp.blogspot.com/-xhNpNJJyQhk/XIe4GY78RQI/AAAAAAAAItc/ouueFUj2Hqo5dntmnKqEaBJR4KQ4Q2K3ACK4BGAYYCw/s1600/logo%2Bgit%2Bicon.png)
# your commit lives in this branch now :) # remove the last commit from the master branch
![git revert commit git revert commit](https://cdn.substack.com/image/fetch/h_600,c_limit,f_auto,q_auto:good,fl_progressive:steep/https:%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F76085cfc-63a4-484c-b37e-3a9239a6a282_396x300.gif)
Oh shit, I accidentally committed something to master that should have been on a brand new branch! # create a new branch from the current state of master Stupid commit message formatting requirements. # follow prompts to change the commit message Oh shit, I need to change the message on my last commit! git commit -amend Warning: You should never amend commits that have been pushed up to a public/shared branch! Only amend commits that only exist in your local copy or you're gonna have a bad time. You could also make the change as a new commit and then do rebase -i in order to squash them both together, but this is about a million times faster. and FML, I didn't put a space after an equals sign. This usually happens to me if I commit, then run tests/linters. # now your last commit contains that change! Mega hat tip to the many many many many many people who suggested adding it! Oh shit, I committed and immediately realized I need to make one small change! # make your change You can use this to get back stuff you accidentally deleted, or just to remove some stuff you tried that broke the repo, or to recover after a bad merge, or just to go back to a time when things actually worked. # each one has an index find the one before you broke everything # you will see a list of every thing you've Oh shit, I did something terribly wrong, please tell me git has a magic time machine!?! git reflog So here are some bad situations I've gotten myself into, and how I eventually got myself out of them in plain english.
#Git revert commit how to
Git documentation has this chicken and egg problem where you can't search for how to get yourself out of a mess, unless you already know the name of the thing you need to know about in order to fix your problem. I've added - older commit and - newer commit to make it clear, you won’t find those notes in the editor.Git is hard: screwing up is easy, and figuring out how to fix your mistakes is fucking impossible. Note that it might be confusing at first since they are displayed in reverse order, where the older commit is on top. When you perform git rebase -i HEAD~4 will pop up an editor, showing the list of commits you want to merge.
![git revert commit git revert commit](https://www.exam4training.com/wp-content/uploads/2021/03/image012-48.jpg)
You can read the whole thing as- Merge all my ‘api-login’ branch commits on top of the commit which is 4b47ceb. Where commit-hash 4b47ceb is added remember me checkbox. So in our example, the command would be: git rebase -interactive 4b47ceb Where is the hash of the commit just before the first one you want to rewrite from. Luckily, there is another way: git rebase -interactive I have tons of commits to squash, do I have to count them one by one?Ī downside of the command git rebase -i HEAD~ is that you have to guess to an exact number of commits, by counting them one by one. So, in this case, the command would be : git rebase -i HEAD~4īecause I want to combine the last four commits into one, and the commit-hash 7ccb146 added Google API Login is the fourth commit. So, In our case, we will write the value of is 4, because we want to join these four commits into one commit.