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
Answer from Isantipov on Stack OverflowThere 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
Remove a git commit which has not been pushed - Stack Overflow
How to un-commit all un-pushed git commits without losing the changes - Stack Overflow
How do I undo the last commit without losing changes?
Can I delete (or undo) a git commit but keep the changes? - Stack Overflow
How do I undo a commit that has already been pushed?
How can I recover a deleted Git commit?
How do I remove the last commit in Git?
Videos
Hello everyone! I'm working on a project in a team. I'm using GUI git client Fork, while also learning command line git.
I finished a task and made a commit (not pushed), but soon realized that I didn't get it done properly, and that I needed to put more work into it. So I left my initial commit as it was, and just kept on working on the task. Right now I'm finally finishing it, but I'm asked to make only one commit with the whole task in it.
When I try to Revert the initial commit, it gives me an error:
Your local changes to the following files would be overwritten by merge Revert failed
What should I do to keep the changes in the initial commit and reapply the new ones? I may have rewritten some code from the initial commit, but not fully, so some parts must stay, and the new changes to be added.
Thank you for your attention!
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
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
You can use git reset to get back to previous commits.
Make sure to use the --mixed option, so it keeps your changes but removes the commits:
git reset --mixed HEAD~3
What I usually suggest in this case is to just create a new branch from your current commit, and then hard reset the current branch back to the point before you made the new commits.
# from master
git branch feature
git reset --hard HEAD~3
The above assumes that you mistakingly made your commits to the master branch, when you actually intended to be on some feature branch. It also assumes that you made 3 commits. Change the hard reset command to the correct number of commits if not 3.
It's as simple as this:
git reset HEAD^
Note: some shells treat ^ as a special character (for example some Windows shells or ZSH with globbing enabled), so you may have to quote "HEAD^" or use HEAD~1 in those cases.
git reset without a --hard or --soft moves your HEAD to point to the specified commit, without changing any files. HEAD^ refers to the (first) parent commit of your current commit, which in your case is the commit before the temporary one.
Note that another option is to carry on as normal, and then at the next commit point instead run:
git commit --amend [-m … etc]
which will instead edit the most recent commit, having the same effect as above.
Note that this (as with nearly every git answer) can cause problems if you've already pushed the bad commit to a place where someone else may have pulled it from. Try to avoid that
There are two ways of handling this. Which is easier depends on your situation.
Reset
If the commit you want to get rid of was the last commit, and you have not done any additional work you can simply use git-reset
git reset HEAD^
Takes your branch back to the commit just before your current HEAD. However, it doesn't actually change the files in your working tree. As a result, the changes that were in that commit show up as modified - its like an 'uncommit' command. In fact, I have an alias to do just that.
git config --global alias.uncommit 'reset HEAD^'
Then you can just used git uncommit in the future to back up one commit.
Squashing
Squashing a commit means combining two or more commits into one. I do this quite often. In your case you have a half done feature commited, and then you would finish it off and commit again with the proper, permanent commit message.
git rebase -i <ref>
I say above because I want to make it clear this could be any number of commits back. Run git log and find the commit you want to get rid of, copy its SHA1 and use it in place of <ref>. Git will take you into interactive rebase mode. It will show all the commits between your current state and whatever you put in place of <ref>. So if <ref> is 10 commits ago, it will show you all 10 commits.
In front of each commit, it will have the word pick. Find the commit you want to get rid of and change it from pick to fixup or squash. Using fixup simply discards that commits message and merges the changes into its immediate predecessor in the list. The squash keyword does the same thing, but allows you to edit the commit message of the newly combined commit.
Note that the commits will be re-committed in the order they show up on the list when you exit the editor. So if you made a temporary commit, then did other work on the same branch, and completed the feature in a later commit, then using rebase would allow you to re-sort the commits and squash them.
WARNING:
Rebasing modifies history - DONT do this to any commits you have already shared with other developers.
Stashing
In the future, to avoid this problem consider using git stash to temporarily store uncommitted work.
git stash save 'some message'
This will store your current changes off to the side in your stash list. Above is the most explicit version of the stash command, allowing for a comment to describe what you are stashing. You can also simply run git stash and nothing else, but no message will be stored.
You can browse your stash list with...
git stash list
This will show you all your stashes, what branches they were done on, and the message and at the beginning of each line, and identifier for that stash which looks like this stash@{#} where # is its position in the array of stashes.
To restore a stash (which can be done on any branch, regardless of where the stash was originally created) you simply run...
git stash apply stash@{#}
Again, there # is the position in the array of stashes. If the stash you want to restore is in the 0 position - that is, if it was the most recent stash. Then you can just run the command without specifying the stash position, git will assume you mean the last one: git stash apply.
So, for example, if I find myself working on the wrong branch - I may run the following sequence of commands.
git stash
git checkout <correct_branch>
git stash apply
In your case you moved around branches a bit more, but the same idea still applies.
I accidentally included a key file in the "doc update" commit so I need to remove it before pushing to the server. However, there are 2 more commits after that one. I know I could cancel the commits and make them all as working tree changes. I just wonder if there is a better way to preserve the last 2 commits.
P.S. this is a personal project so I am the only one who use this. That's why I have 3 commits queued up here. I know its a bad move. If I push my last commit before starting a new one, I could have avoid this situation.