The common mistake I make in git is
- not check on which branch I am
- commit changes to a wrong branch (on branch B, thinking I'm on A, commiting a change for feature A)
How do I get back, and commit the edits to the proper branch?
The common mistake I make in git is
How do I get back, and commit the edits to the proper branch?
rebase --onto
can help here, especially if you have a range of commits to move back.
Do not forget a git stash
to save any current uncommitted changes which are relevant to "wrongBranch
" (the one where commits were appropriately applied), in order to pop them back at the end of the process.
Basically, you need to apply a commit or range of commits to another branch (called here "rightBranch
"):
# Checkout the right branch where the commit should have been applied
git checkout rightBranch
# Checkout a new temporary branch at the current location
git checkout -b tmp
# Move the rightBranch branch to the head of patchset which should have been on rightBranch
git branch -f rightBranch last_SHA-1_of_commits_for_rightBranch
# Rebase the patchset onto tmp, the old location of rightBranch
git rebase --onto tmp first_SHA-1_of_commits_for_rightBranch~1 rightBranch
(first_SHA-1_of_commits_for_rightBranch~1
is the parent commit of first_SHA-1_of_commits_for_rightBranch
, that is the commit on top of which all the commits for rightBranch have been incorrectly applied)
rightBranch
will be again at the top of:
tmp
branch)wrongBranch
, which have just been replayed on top of tmp
(tmp
being the previous rightBranch HEAD
).Then you can reset wrongBranch
to some previous commits.
git checkout wrongBranch
git reset --hard some_older_commit
# if needed, re-apply what you were working on vefore this all proccess
git stash pop
Caveat:
wrongBranch
has already been published (pushed to a public repo), that can be awkward for the people pulling for that repo (having to rebase all their changes on top of that "new" branch)