Reputation: 63836
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.
Upvotes: 1032
Views: 675335
Reputation: 3560
The easiest way to undo the last Git commit is to execute the git reset
command with one of the below options
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
Upvotes: 52
Reputation: 1766
Here is a modern way to undo your last commit if you are a VSCode user.
Ctrl + Shift + G G
)....
on the left of circlish update icon. Refer to the screenshot below:
All it does is it restores your repository just as it was before you made the commit, with your changes untouched.
Upvotes: 23
Reputation: 560
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.
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':
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?)git push
After the push, this is how it looked like:-
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)
Upvotes: 0
Reputation: 10921
2020 simple way :
git reset <commit_hash>
Commit hash of the last commit you want to keep.
Upvotes: 12
Reputation: 20969
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
Upvotes: 1740
Reputation: 1956
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.
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.
Upvotes: 13
Reputation: 6187
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.
Upvotes: 1
Reputation: 12668
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
Upvotes: 5
Reputation: 24650
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.
Upvotes: 18