How to un-commit last un-pushed git commit without losing the changes

Git

Git Problem Overview


Is there a way to revert a commit so that my local copy keeps the changes made in that commit, but they become non-committed changes in my working copy? Rolling back a commit takes you to the previous commit - I want to keep the changes made but I committed them to the wrong branch.

This has not been pushed, only committed.

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

If you pushed the changes, you can undo it and move the files back to stage without using another branch.

git show HEAD > patch
git revert HEAD
git apply patch

It will create a patch file that contain the last branch changes. Then it revert the changes. And finally, apply the patch files to the working tree.

Solution 3 - Git

For the case: "This has not been pushed, only committed." - if you use IntelliJ (or another JetBrains IDE) and you haven't pushed changes yet you can do next.

  1. Go to Version control window (Alt + 9/Command + 9) - "Log" tab.
  2. Right-click on a commit before your last one.
  3. Reset current branch to here
  4. pick Soft (!!!)
  5. push the Reset button in the bottom of the dialog window.

Done.

This will "uncommit" your changes and return your git status to the point before your last local commit. You will not lose any changes you made.

Solution 4 - Git

2020 simple way :

git reset <commit_hash>

Commit hash of the last commit you want to keep.

Solution 5 - Git

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

Solution 6 - Git

With me mostly it happens when I push changes to the wrong branch and realize later. And following works in most of the time.

git revert commit-hash
git push

git checkout my-other-branch
git revert revert-commit-hash
git push
  1. revert the commit
  2. (create and) checkout other branch
  3. revert the revert

Solution 7 - Git

New Update for fellow googlers from 2021 onwards.

Here is a modern way to undo your last commit if you are a VSCode user.

  1. Go to your Source Control tab on the left (shortcut: Ctrl + Shift + G G).
  2. press ... on the left of circlish update icon. Refer to the screenshot below: source control screenshot
  3. Navigate to Commit, then to Undo Last Commit. Here is a screenshot: enter image description here

All it does is it restores your repository just as it was before you made the commit, with your changes untouched.

Solution 8 - Git

> PLease make sure to backup your changes before running these commmand in a separate folder

git checkout branch_name

> Checkout on your branch

git merge --abort

> Abort the merge

git status

> Check status of the code after aborting the merge

git reset --hard origin/branch_name

> these command will reset your changes and align your code with the branch_name (branch) code.

Solution 9 - Git

Adding steps I followed hoping that it's helpful for a beginner like me.

Following picture shows the commits I have already pushed to the remote branch 'A' in bitbucket. enter image description here

From these 5 commits, I want to keep the last 2 as it is, but the first 3 commits I want them pushed to another branch 'B'.

These are the steps I followed:

Inside branch 'A':

  1. git revert <commit-hash> for each of the 3 commits. As an example, d4a3734 is the commit hash of the last commit in the picture. (If you want you can revert multiple commits at once - refer https://stackoverflow.com/questions/1463340/how-to-revert-multiple-git-commits)
  2. git push

After the push, this is how it looked like:-

enter image description here

Now, I only have the first 2 commits in my branch 'A', which is what I wanted. Next, checkout to the branch wanted. If it's a new branch, use git checkout -b <branchname>. In my case, I did git checkout B.

Inside branch 'B':

I simply cherry-picked the commits I wanted to branch 'B'. In my case I did:

git cherry-pick <commit-hash> 

for those 3 commits I reverted.

(Again, as an example, git cherry-pick d4a3734 where d4a3734 is the commit hash of the last commit in the picture)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMr. BoyView Question on Stackoverflow
Solution 1 - GitIsantipovView Answer on Stackoverflow
Solution 2 - GitAminadav GlickshteinView Answer on Stackoverflow
Solution 3 - GitEduard StreltsovView Answer on Stackoverflow
Solution 4 - GitXysView Answer on Stackoverflow
Solution 5 - Gitsanka sanjeewaView Answer on Stackoverflow
Solution 6 - GitMubasharView Answer on Stackoverflow
Solution 7 - GitBekiView Answer on Stackoverflow
Solution 8 - GitMohit SinghView Answer on Stackoverflow
Solution 9 - GitJolly GoodView Answer on Stackoverflow