-2

How can I create a new branch before last 3 commits and move last 3 commits into the new branch? From :

a -> b ->c -> d-> e -> f (master)

To:

a-> b->c(master)
        \
         d-> e-> -> f(new_branch)
  • 2
    This question has been answered many times. I've noted one of the mroe highly voted discussions. Since you point out that you've pushed these commits, I would highlight that you would be rewriting the history of `master`, and need to understand the costs of doing that. – Mark Adelsberger Feb 22 '21 at 15:18
  • @MarkAdelsberger OK I get the downvote now. You're right, I've answered too hastily. – Romain Valeri Feb 22 '21 at 16:44

1 Answers1

2

If you're the sole owner of the repo, without the need to preserve a shared history with coworkers :

From your master branch currently pointing at f :

# create your new branch
git branch new_branch

# reset master where it should
git reset --hard c

# then push the rewritten version of master to gitlab (assuming "origin" here)
git push -f origin master

If, in the other hand, you're sharing this repo, breaking master's history is probably to be avoided (discuss it with your team).

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61