IF you have NOT pushed your changes to remote
git reset HEAD~1
Check if the working copy is clean by git status.
ELSE you have pushed your changes to remote
git revert HEAD
This command will revert/remove the local commits/change and then you can push
Answer from Jeril Kuruvila on Stack OverflowIF you have NOT pushed your changes to remote
git reset HEAD~1
Check if the working copy is clean by git status.
ELSE you have pushed your changes to remote
git revert HEAD
This command will revert/remove the local commits/change and then you can push
Actually, when you use git reset, you should refer to the commit that you are resetting to; so you would want the db0c078 commit, probably.
An easier version would be git reset --hard HEAD^, to reset to the previous commit before the current head; that way you don't have to be copying around commit IDs.
Beware when you do any git reset --hard, as you can lose any uncommitted changes you have. You might want to check git status to make sure your working copy is clean, or that you do want to blow away any changes that are there.
In addition, instead of HEAD you can use origin/master as reference, as suggested by @bdonlan in the comments: git reset --hard origin/master
How to undo pushed commits
How to un-commit last un-pushed git commit without losing the changes - Stack Overflow
Is there a way to remove all local commits while keeping the changes?
How do I get back to the most recent push/commit?
Videos
If you have already pushed this commit, then it is possible that someone else has pulled the branch. In this case, rewriting your branch's history is undesirable and you should instead revert this commit:
git revert <SHA-1>
git push origin branch
Here <SHA-1> is the commit hash of the commit you want to remove. To find this hash value, simply type git log on your branch and inspect the first entry.
Using git revert actually adds a new commit which is the mirror image of the commit you want to remove. It is the preferred way of undoing a commit on a public branch because it simply adds new information to the branch.
If you are certain that you are the only person using this branch, then you have another option:
git reset --hard HEAD~1
followed by
git push --force origin branch
But you should only use this option if no one else is sharing this branch.
The way I go about it is by typing git status, which allows us to verify the branch we're currently on, followed by:
git log
Then, you'll see something like this:
commit aa09a82fb69af2d1aebde51d71514f7a03c3a692
Author: User <user@useremail.com>
Date: Fri Nov 4 15:36:22 2016 -0400
Fix issue with vertical scroll being forced
commit 411771837efe3ed555395e77fd35105a500ab758
Author: User <user@useremail.com>
Date: Thu Nov 3 15:50:42 2016 -0400
Add user notifications
commit f43b262f4e02b5a7268280e1230d44e36d1e547b
Author: User <user@useremail.com>
Date: Thu Nov 3 12:11:00 2016 -0400
All your base are belong to us
So, this tells us that commit aa09a82f is your last one, and commit 41177183 is the one before it, then:
git reset --hard 41177183
...brings us back to that commit, retaining the remote backup. With another git status to make sure that everything is all set for the double push (I'm personally a bit obsessive compulsive about verifying my current branch, especially when multitasking):
git push origin :<branch_name>
git push origin <branch_name>
At this point, you should be all set, but it's always good to follow that up with:
git fetch --all --prune
git branch -av
...which cleans up your branch list and shows you both local and remote to compare the commit messages.
Also, if working with a team, make sure that they're aware of this before moving forward. You don't want them to pull or push the branch on their end before you remove the last commit and push.
-
One of our artists managed to push 1 month of progress without merging
-
As you can see I tried to undo the commits one by one in reverse order
-
I couldn't undo the merge commit of Arvincle because it's empty
-
When I try to revert the commit before that I get one file I need to merge first, but the rest of the changes are not undone
Is there a way to reset the HEAD of main to the last valid commit?
There are a lot of ways to do so, for example:
in case you have not pushed the commit publicly yet:
git reset HEAD~1 --soft
That's it, your commit changes will be in your working directory, whereas the LAST commit will be removed from your current branch. See git reset man
In case you did push publicly (on a branch called 'master'):
git checkout -b MyCommit //save your commit in a separate branch just in case (so you don't have to dig it from reflog in case you screw up :) )
revert commit normally and push
git checkout master
git revert a8172f36 #hash of the commit you want to destroy
# this introduces a new commit (say, it's hash is 86b48ba) which removes changes, introduced in the commit in question (but those changes are still visible in the history)
git push origin master
now if you want to have those changes as you local changes in your working copy ("so that your local copy keeps the changes made in that commit") - just revert the revert commit with --no-commit option:
git revert --no-commit 86b48ba (hash of the revert commit).
I've crafted a small example: https://github.com/Isantipov/git-revert/commits/master
The easiest way to undo the last Git commit is to execute the git reset command with one of the below options
- soft
- hard
- mixed
Let's assume you have added two commits and you want to undo the last commit
$ git log --oneline
45e6e13 (HEAD -> master) Second commit
eb14168 Initial commit
–soft option undo the last commit and preserve changes done to your files
$ git reset --soft HEAD~1
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: file.html
$ git log --oneline
eb14168 (HEAD -> master) Initial commit
–hard option undo the last commit and discard all changes in the working directory and index
$ git reset --hard HEAD~1
$ git status
nothing to commit, working tree clean
$ git log --oneline
eb14168 (HEAD -> master) Initial commit
--mixed option undo the last commit and keep changes in the working directory but NOT in the index
$ git reset --mixed HEAD~1
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: file.html
no changes added to commit (use "git add" and/or "git commit -a")
$ git log --oneline
eb14168 (HEAD -> master) Initial commit
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.