I'm working on a git branch that I branched out from the main branch and I have made a number of commits to it but now I want to reset that branch to its earlier state when I first branched it from the main branch. what I mean is that I want to remove all commits I have already committed and push in there so that it becomes exactly the same as the main.
when I did this :
git reset --hard origin/the-branch-name
It give this:
HEAD is now at 4176992 This is fifth commit to new branch
which is the last commit I made. But I want to remove all commits I have made.
Please, how can I achieve this?
CodePudding user response:
I would recommend just deleting the branch. If you do not want to do this for whatever reason, you can also revert all the commits you have made. In that case, look into the git revert
command
CodePudding user response:
You were close. You tried resetting your branch to the remote tracking branch, but since you had already pushed your branch, this had no effect because it was already on the same commit. Instead, simply use main
or origin/main
which might be more up to date than your local copy of main
. So, your command would be:
git reset --hard origin/main
Note, you can also delete your branch and re-create it, as others have already mentioned. That is slightly less efficient because you have to first move off of your branch before you can delete it. The end result will be the same though.
Note, since you already pushed your branch, after you reset it (or re-create it), the first time you try to push it you'll need to force push.