we want to undo the last 2 commits which were both pushed to origin master. in tortoise git, we can view the history, and click on the 3rd from last commit, and chose "reset master to this" which we are guessing means revert to this version.
however, when you select this option you have to choose from one of these three options:
Unfortunately, we dont know which of these 3 options we should choose - we just want to undo the last two commits and go back in time.
The next question is does this operation happen on the remote origin, your repo, or your working directory, or a combination of these 3? E.g. does a commit and push have to be done after reverting, or does it do this for you?
We would take a guess that HARD is what we want - it will revert the repository (not sure which) and our local source code back to a previous version. If this is the case, why is this not the default, and what is the use case for the other two options? Either you want to revert or not, and if you only revert the remote repo, and not your local working files then you are going to be in a messy state.
We don't have any locally modified files.
Note, we are quite desperate for a solution which doesnt break git (we have broken git in the past and had to craete a new repo). Perhaps another method possible, such as checkout out a prevous version, then checking it in over the top of the latest version, but we dont know how to do this.
We get that we can do something like:
git checkout [revision]
But how do we then tell git that we want to make this version the new head or replace the head with this? I am guessing we cant just commit, as there is nothing to commit, as we are no longer on the master head. As an aside, if you check out a prev. version, and modify it, and commit, what are you committing and to where?
I have read https://www.atlassian.com/git/tutorials/undoing-changes but it does not address the relationship between local and remote repos. If the commits to be undone are all pushed to the remote origin master, it is unclear what the recipe is to revert the remote origin, the local repo and the working directory - all 3 must be reverted to the commit 3 commits ago. We dont really care if this is done with a reset or revert - we just need a way to do it which works across all 3 locations.
It might be that the corrrect recipie is something like this:
git status (we are on master with a clean working dir)
git git revert HEAD~2
git commit -m "revert"
git push origin mater
But we have never seen this set of steps in the examples, and would rather do it with tortoise git if possible.
Instead of going through all the changes manually, you can simply tell git to revert a commit, which does not even have to be the last one. Reverting a commit means to create a new commit that undoes all changes that were made in the bad commit.
There are two ways you can do this.
For the 1st Solution, you can use the following commands:
git reset --hard <commit-id>
This will bring the Head for the branch in you are currently to that specific "commit-id" which as per you is correct and proper.
git push -f origin <branch-name>
This command will forcefully push to the branch you are in. The combination of this command and the above command can enable you to remove all the unnecessary commits you have in your branch starting from the very top and till this "commit-id"
Note: This won't remove the "commit-id" everything up till the "commit-id" will be removed from your branch.
For the 2nd Solution, you can use the following commands:
git revert <commit-id>
This command is used to revert the specific "commit-id" you are trying to undo.
If by mistake you have merged another branch into your different branch then you can use this command to revert the merge request:
git revert -m 2 <commit-id>
This command is used to revert or remove all the changes that took place in the merge commit "commit-id". This will allow reverting the merge commit "commit-id", also it will remove all the commit's changes which came with this merge. But it won't remove the commits which came with the merge
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With