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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
rebase --ontocan help here, especially if you have a range of commits to move back.Do not forget a
git stashto 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“):(
first_SHA-1_of_commits_for_rightBranch~1is the parent commit offirst_SHA-1_of_commits_for_rightBranch, that is the commit on top of which all the commits for rightBranch have been incorrectly applied)rightBranchwill be again at the top of:tmpbranch)wrongBranch, which have just been replayed on top oftmp(tmpbeing the previousrightBranch HEAD).Then you can reset
wrongBranchto some previous commits.Caveat:
wrongBranchhas 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)