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

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


Solution 1:[1]

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 2:[2]

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 3:[3]

2020 simple way :

git reset <commit_hash>

Commit hash of the last commit you want to keep.

Solution 4:[4]

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 5:[5]

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 6:[6]

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:[7]

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 8:[8]

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 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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aminadav Glickshtein
Solution 2
Solution 3 Xys
Solution 4 sanka sanjeewa
Solution 5 Beki
Solution 6 Mubashar
Solution 7 Mohit Singh
Solution 8 Jolly Good