Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: moving commits from master to another branch

Tags:

git

I made a series of commits after a good commit on the master branch, which in hindsight I should have made in another branch. Can I move those commits, beginning with a specific commit, to another branch, and keep the good commit as the last commit on master?

like image 303
Filo Stacks Avatar asked Oct 21 '11 19:10

Filo Stacks


People also ask

Can I move commits from one branch to another?

If you want to move commits to an existing branch you need to merge your changes into the existing branch before executing git reset --hard HEAD~3 (see Moving to an existing branch above). If you don't merge your changes first, they will be lost.

How do I move my master branch to another branch?

First, make sure that the target branch exists by running the “git branch” command. Now that you made sure that your branch exists, you can switch from the master branch to the “feature” branch by executing the “git checkout” command. That's it!

Can a commit be taken from one branch and moved to a different branch in git?

You can move commits from one branch to another branch if you want changes to be reflected on a different branch than the one to which you pushed the changes.


1 Answers

Sure:

$ git branch new-branch-name                       # Create a new branch from the current commit
$ git reset --hard <last good commit on master>    # Reset master to the good commit
like image 177
mipadi Avatar answered Sep 30 '22 17:09

mipadi