Move the most recent commit(s) to a new branch with Git – Stack Overflow
Posted by jpluimers on 2019/03/05
Below are the git statements I used to solve this ASCII art problem from me (as I work in Git Flow feature branches):
old situation:
commit-1..4 - commit-5 - commit-6 - commit-7 - commit-8 - commit-9
^ ^ ^ ^ ^
| | | | |
master develop feature/A feature/old
to:
commit-1..4 - commit-5 - commit-6 - commit-7 - commit-8 - commit-9
^ ^ ^ ^ ^
| | | | |
master develop feature/A feature/old feature/new
git branch
git rev-parse HEAD
git log --pretty=format:'%H' -n 2
git checkout -b feature/new hash-of-commit-8
git branch --set-upstream-to=feature/old
git cherry-pick ..feature/old
git branch --force feature/old hash-of-commit-8
Step by step, this is what happens:
branchlists the current branchesrev-parse HEADshows the hash of the current commit (commit-9)log --pretty=format:'%H' -n 1shows the hash of the previous two commits (from top to bottom:commit-9andcommit-8)checkoutcreates a new branch based on the pastcommit-8branch --set-upstreamensures the new branch tracks the old branchcherry pickensures the new branch gets all the commits from the old branchbranch --forceensure the old branch looses the extra commits you wanted to only be in newBranchName
Based on
- [WayBack] Move the most recent commit(s) to a new branch with Git – Stack Overflow (thanks [WayBack] john-mellor)
- [WayBack] Moving commits to another branch
–jeroen






Leave a comment