If your excess commits are only visible to you, you can just do
git reset --hard origin/<branch_name>
to move back to where the origin is. This will reset the state of the repository to the previous commit. WARNING: This also discards all your uncommitted local file changes.
Doing a git revert makes new commits to remove old commits in a way that keeps everyone's history sane.
If your excess commits are only visible to you, you can just do
git reset --hard origin/<branch_name>
to move back to where the origin is. This will reset the state of the repository to the previous commit. WARNING: This also discards all your uncommitted local file changes.
Doing a git revert makes new commits to remove old commits in a way that keeps everyone's history sane.
Delete the most recent commit, without destroying the work you've done:
git reset --soft HEAD~1
Delete the most recent commit and remove changes:
git reset --hard HEAD~1
I would like to organize my PR after my feature is done by removing all of my 'work in progress'-commits and creating new commits that group files to improve the readability of my PR.
I have tried using git reset, but that only unstages changes that have been added using git add . & all files that are already committed seem to be out of reach.
version control - How do I undo the most recent local commits in Git? - Stack Overflow
How do you revert your code back to your last git commit?
Is there a way to remove all local commits while keeping the changes?
Switching branch without commiting changes
Think we have code.txt file.
We make some changes on it and commit.
We can undo this commit in three ways, but first you should know what is the staged file...
An staged file is a file that ready to commit and if you run git status this file will be shown with green color and if this is not staged for commit will be shown with red color:

It means if you commit your change, your changes on this file is not saved.
You can add this file in your stage with git add code.txt and then commit your change:

Undo last commit:
Now if we want to just undo commit without any other changes, we can use
git reset --soft HEAD^
If we want to undo commit and its changes (THIS IS DANGEROUS, because your change will lost), we can use
git reset --hard HEAD^
And if we want to undo commit and remove changes from stage, we can use
git reset --mixed HEAD^or in a short formgit reset HEAD^
Usually, you want to undo a commit because you made a mistake and you want to fix it - essentially what the OP did when he asked the question. Really, you actually want to redo a commit.
Most of the answers here focus on the command line. While the command line is the best way to use Git when you're comfortable with it, its probably a bit alien to those coming from other version control systems to Git.
Here's how to do it using a GUI. If you have Git installed, you already have everything you need to follow these instructions.
NOTE: I will assume here that you realised the commit was wrong before you pushed it. If you don't know what pushing means, then you probably haven't pushed. Carry on with the instructions. If you have pushed the faulty commit, the least risky way is just to follow up the faulty commit with a new commit that fixes things, the way you would do it in a version control system that does not allow you to rewrite history.
That said, here's how to fix your most recent fault commit using a GUI:
- Navigate to your repository on the command line and start the GUI with
git gui - Choose "Amend last commit". You will see your last commit message, the files you staged and the files you didn't.
- Now change things to how you want them to look and click Commit.