Move recent commits to another branch
Sometimes I end up making commits on a main
branch, while I wanted to make them on a feature branch.
Say we made three commits we want to move to another branch. We can follow those steps:
sh
# If the feature branch does not exist, create itgit switch -C feature-branch# Otherwise, switch to itgit switch feature-branchgit merge maingit push origin feature-branch # Be sure to save commits before deleting them from main branchgit switch - # Go back to main branchgit reset --hard HEAD~3 # Go back 3 commits, so all the commits we made are deleted from main branchgit switch - # Go back to feature branch
This snipped is adapted from an excellent StackOverflow post.