Reputation: 11019
My Git setup is such that I work in a local "feature" branch and commit changes to this branch as I work. When the code is ready I merge into my local "develop" branch. I then push the changes in my local "develop" branch to the remote "develop" branch.
I made some changes to a file and committed them without realizing I was on my local "develop" branch rather than my "feature" branch. I realized my mistake before I pushed the "develop" branch to the remote "develop" branch, so the changes remain local.
How do I undo this commit to my local "develop" branch? I am using Tortoise Git but I can still use the Git command line if needed.
Note: This is not my first commit to the feature branch or first merge to the develop branch.
Upvotes: 5
Views: 4367
Reputation: 83517
If you forgot to create a branch for your feature, you should create one:
git branch feature
Then you can reset develop
back to where it should be:
git reset --hard HEAD~
If you already have a feature
branch where you want to move the commit that you accidentally made on develop
, then you can use git cherry-pick
:
git checkout feature
git cherry-pick develop
Then you can reset develop
as above. Just be sure to check it out first:
git checkout develop
Upvotes: 4
Reputation: 13623
So, let's talk about how to move those commits over to the feature branch and then undo them. For the purposes of this explanation, we will assume that only errant commits have gone onto your develop
branch locally.
Get the commit hashes for each of the errant commits on develop
. You can do this in your gui, or checkout develop
and run git log
in a terminal. Copy paste them someplace you can reference them later.
Checkout your feature
branch. You will now run a git cherry-pick <commit_hash>
for each of the commit hashes you saved in step #1. You want to do this in the order in which they were committed onto the develop
branch. This replays each of these commits onto your local feature
branch.
Do a hard reset on your develop
branch to get it back to the remote version. Assuming that the remote reference is origin/develop
, you would do git reset --hard origin/develop
Note there are other means of doing this, using git rebase
or using interactive rebases. This is just the easiest way I could think of to convey it for someone who has an unknown amount of git experience.
Note that if you are worried about being able to revert in case things don't go as expected, you can leverage git tag
to make rescue tags.
Upvotes: 0
Reputation: 1425
There are several ways you could resolve the problem. One way would be to cherry-pick
the commit on your feature
branch. Then reset
your develop
branch to the previous commit.
git checkout feature
git cherry-pick <commit hash>
git checkout develop
git reset --hard HEAD~1
Upvotes: 11