We all know how to commit, but the two most important things to know is how to work with a branch and how to undo a screwup.
Rolling Back.
There are tons and tons of ways to do this, but I have found the most intelligent and understandable way to do it is by using the following method:
How to restore from a previous commit
- Open the branch:
git checkout features/my-branch
pull from origin to make sure you have the latest
git pullcheckout the exact commit you want to roll back to, but be careful and make sure you have the trailing period (you don't need the entire sha)
git checkout 2de0a898 .all the differences should now be in your working directory as if you made them onto the latest pull. So, just commit them
git commit -m "restoring from prev commit
"2de0a898
We’ve probably all committed something, realized we didn’t mean to do that, then gone onto github or devops and just cut-pasted the last commit on top of our regular code. Then it shows the change in our staged changes, we commit it as a new change and we’ve essentially “rolled back”, when the reality is that we just typed over the previous commit and made a new commit. But, sometimes you have 50 files that have changed. That’s not so easy. By checking out the commit hash that you want to go back to, but appending a period at the end, it will actually write over the files in your current directory with that commit. So, it’s basically pasting those files right on top of your working directory without actually leaving the branch you were in.
Then, you can just put in a message and commit them. Then, you haven’t lost anything, you’re not fiddling with the HEAD, you’re not going to mess up other developers, etc…