I just made a perfectly good commit to the wrong branch. How do I undo the last commit in my master branch and then take those same changes and get them into my upgrade bran
For me, this was solved by reverting the commit I had pushed, then cherry-picking that commit to the other branch.
git checkout branch_that_had_the_commit_originally
git revert COMMIT-HASH
git checkout branch_that_was_supposed_to_have_the_commit
git cherry pick COMMIT-HASH
You can use git log
to find the correct hash, and you can push these changes whenever you like!
If for you, it is just about 1 commit, then there are plenty of other easier resetting solutions available. For me, I had about 10 commits that I'd accidentally created on master
branch instead of, let's call it branch_xyz
, and I did not want to lose the commit history.
What you could do, and what saved me was using this answer as a reference, using a 4 step process, which is -
master
branch_xyz
master
Here are the above steps in details -
Create a new branch from the master
(where I had accidentally committed a lot of changes)
git checkout -b temp_branch_xyz
Note: -b
flag is used to create a new branch
Just to verify if we got this right, I'd do a quick git branch
to make sure we are on the temp_branch_xyz
branch and a git log
to check if we got the commits right.
Merge the temporary branch into the branch originally intended for the commits, i.e. branch_xyz
.
First, switch to the original branch i.e. branch_xyz
(You might need to git fetch
if you haven't)
git checkout branch_xyz
Note: Not using -b
flag
Now, let's merge the temporary branch into the branch we have currently checkout out branch_xyz
git merge temp_branch_xyz
You might have to take care of some conflicts here, if there are. You can push (I would) or move on to the next steps, after successfully merging.
Undo the accidental commits on master
using this answer as reference, first switch to the master
git checkout master
then undo it all the way back to match the remote using the command below (or to particular commit, using appropriate command, if you want)
git reset --hard origin/master
Again, I'd do a git log
before and after just to make sure that the intended changes took effect.
Erasing the evidence, that is deleting the temporary branch. For this, first you need to checkout the branch that the temp was merged into, i.e. branch_xyz
(If you stay on master
and execute the command below, you might get a error: The branch 'temp_branch_xyz' is not fully merged
), so let's
git checkout branch_xyz
and then delete the proof of this mishap
git branch -d temp_branch_xyz
There you go.
If the branch you wanted to apply your changes to already exists (branch develop, for example), follow the instructions that were provided by fotanus below, then:
git checkout develop
git rebase develop my_feature # applies changes to correct branch
git checkout develop # 'cuz rebasing will leave you on my_feature
git merge develop my_feature # will be a fast-forward
git branch -d my_feature
And obviously you could use tempbranch or any other branch name instead of my_feature if you wanted.
Also, if applicable, delay the stash pop (apply) until after you've merged at your target branch.
If you already pushed your changes, you will need to force your next push after resetting the HEAD.
git reset --hard HEAD^
git merge COMMIT_SHA1
git push --force
Warning: a hard reset will undo any uncommitted modifications in your working copy, while a force push will completely overwrite the state of the remote branch with the current state of the local branch.
Just in case, on Windows (using the Windows command line, not Bash) it's actually four ^^^^
instead of one, so it's
git reset --hard HEAD^^^^
I recently did the same thing, where I accidentally committed a change to master, when I should have committed to other-branch. But I didn't push anything.
If you just committed to the wrong branch, and have not changed anything since, and have not pushed to the repo, then you can do the following:
// rewind master to point to the commit just before your most recent commit.
// this takes all changes in your most recent commit, and turns them into unstaged changes.
git reset HEAD~1
// temporarily save your unstaged changes as a commit that's not attached to any branch using git stash
// all temporary commits created with git stash are put into a stack of temporary commits.
git stash
// create other-branch (if the other branch doesn't already exist)
git branch other-branch
// checkout the other branch you should have committed to.
git checkout other-branch
// take the temporary commit you created, and apply all of those changes to the new branch.
//This also deletes the temporary commit from the stack of temp commits.
git stash pop
// add the changes you want with git add...
// re-commit your changes onto other-branch
git commit -m "some message..."
NOTE: in the above example, I was rewinding 1 commit with git reset HEAD~1. But if you wanted to rewind n commits, then you can do git reset HEAD~n.
Also, if you ended up committing to the wrong branch, and also ended up write some more code before realizing that you committed to the wrong branch, then you could use git stash to save your in-progress work:
// save the not-ready-to-commit work you're in the middle of
git stash
// rewind n commits
git reset HEAD~n
// stash the committed changes as a single temp commit onto the stack.
git stash
// create other-branch (if it doesn't already exist)
git branch other-branch
// checkout the other branch you should have committed to.
git checkout other-branch
// apply all the committed changes to the new branch
git stash pop
// add the changes you want with git add...
// re-commit your changes onto the new branch as a single commit.
git commit -m "some message..."
// pop the changes you were in the middle of and continue coding
git stash pop
NOTE: I used this website as a reference https://www.clearvision-cm.com/blog/what-to-do-when-you-commit-to-the-wrong-git-branch/