Key Insights: Undoing Local Changes That Have Not Been Committed
Key Insights: Undoing Local Changes That Have Not Been Committed
1. To revert the latest commit and discard changes in the committed file do:
git reset --hard HEAD~1
2. To revert the latest commit but retain the local changes (on disk) do:
git reset --soft HEAD~1
This (the later command) will take you to the state you would have been if you did git add.
If you want to unstage the files after that, do
git reset
Now you can make more changes before adding and then committing again.
1 - Undo commit and keep all files staged: git reset --soft HEAD~
2 - Undo commit and unstage all files: git reset HEAD~
3 - Undo the commit and completely remove all changes: git reset --hard HEAD~
Remove git commit which has not been pushed
Some times we need to undo code from the remote branch which is not pushed yet into Git.
There are multiple ways to undo the commit, which has not been pushed yet.
Let's assume you have a project. Which should be pushed on Git. Now you should change in
one file. Now check the status through the following command.
$ git status
git status
Step 2:- Add and commit changes
Now you should add and commit these changes to the repository through the following
commands.
// Add file
$ git add validator.js// Commit code
$ git commit -m "change some validations"
git add
git commit
git status
Now, we will undo the commit. So there are multiple methods for undo, we will perform one
by one. Before performing the different methods we should know about logs. Use git log to
check all commits then you can decide how many commits do you want to roll back.
$ git log
git log
For keeping files in the staged so we should have to use the following command.
$ git reset --soft HEAD~1
If you want to remove the last commit and unstage all files then you can use to use the
following command.
$ git reset HEAD~1
git reset
git status
In case you want to remove commit and discard all changes then you should use the following
command.
$ git reset --hard HEAD~1
git hard reset
But if the team has pushed the commits, consider git revert instead.
That way, anyone who has pulled the bad commit can also pull the revert
commits as well.
How to Undo a Commit in GitHub
A very common task performed by developers while coding is undoing
a commit. This is a common mistake which prompts developers to turn
to Google for answers! In this article, I will be teaching you how to
undo these mistakes with Git.
There are four common scenarios where I think developers perform this
task:
This will output the following; on the left you can see the commit
hashes followed by the commit messages.
In the vim editor, you can edit what commits you want to pick or
discard. By default, all commit hashes have the word pick before the
hash number, however, if you would like to delete a commit you press
the `i` key on your keyboard which allows you to edit the file and
change `pick` to `d`. To save changes, use `:wq` to close the vim editor.