Reputation: 11
I am new to git and accidentally created new branch from another branch instead of main
.
My current map:
f-g
/
d-e
/
a-b-c
f
and g
should be coming from main
d-e
/
a-b-c
\
f-g
I need to move f
and g
to a new branch coming from main.
Note: I already pushed them to GitHub
Upvotes: 1
Views: 65
Reputation: 72177
You can move the branch to start from c
using git rebase
:
Assuming your current branch points to commit g
make sure your working tree is clean (run git status
to find out).
Create a backup branch that points to g
(run git branch backup
). It can be used to restore the original position of the current branch if you are not pleased with the result. You can remove it (git branch -D backup
) at the end.
To move the commits run:
git rebase --onto c HEAD~2
This command cuts the commits that are reachable from the current branch and are not reachable from HEAD~2
and replays them on top of commit c
.
Replace 2
with the exact number of commits you want to move.
If the git rebase
operation fails (due to conflicts) run git rebase --abort
to restore the branch to its original position (on commit g
).
Upvotes: 1