How to copy commits from one branch to another?

I am using the following workflow with git:

  1. I set a task
  2. I create a branch for it from master
  3. implement task
  4. pull in master
  5. merge my branch into master
  6. I push the master

However, I skipped step number (2) and committed all my code in the master. How can I change my master commits to another branch?

Author: Vinicius Brasil, 2014-02-17

4 answers

Get the SHA1 of the first commit that should be moved. You can do this with:

git checkout master
git log

# Cria o branch apontando para o HEAD
git branch nomedobranch
# reseta o master descartando commits
git reset --hard <sha1 do commit> 

In this case the new branch will have all the commits and the master will be reset to not contain the commits of the branch (you can make a merge again if you want).


P.S.: I'm assuming you haven't executed step 6 (push) yet, otherwise you'll have to do a push -f and align with people who have already run commando pull and got these commits from the remote repository. They must also discard the commits locally before doing push, or they will reintroduce the commits into the master.

P.S. 2: I'm also assuming that you have a "clean" string of commits to move to the new branch. That is, the HEAD is the last commit that should belong to the branch and not there are commits intermediates that should not belong to the branch since the initial commit (e.g., there was no pull halfway through introducing commits unrelated to the brach). If this is not true you should do cherry-pick only of the commits you want.

 # Cria novo branch apontando para o primeiro commit
 git checkout -b nomedobranch <sha1 do primeiro commit> 
 git cherry-pick <sha1 do segundo commit>
 git cherry-pick <sha1 do terceiro commit>
 git cherry-pick <sha1 do quarto commit>

Source: SOE-how can I move recent commit(s) to a new branch with git?.

 11
Author: Anthony Accioly, 2017-05-23 12:37:35

Attention : you should not perform this procedure if you have already forced push into the remote repository, as other developers may have already performed pull from that repository.

You don't need to pull the commits from the local master to another branch if you don't really want to. Your problem is that you did the pull of the remote master, only that the commits that were already in the remote master were above your recently committed changes, to reorder the commits in the local master, you can use the code below:

git rebase -i HEAD~<número_de_commits_que_devem_ser_reordenados> 
  • the flag -i is iterative mode
  • the HEAD is the last commit in the branch
  • the ~ is a kind of subtraction

Suppose <número_de_commits_que_devem_ser_reordenados> is 3, the meaning of HEAD~3 is to perform the task from the antepenultimate commit.

The commits will be displayed, as follows in an editor such as vi, note that all lines preceded by the character # are comments explanatory about what you can accomplish with the command git rebase -i HEAD~3

pick 4c39bca gemspec tweak
pick 85409cf Version bump to 0.4.1
pick eb32194 Regenerated gemspec for version 0.4.1

# Rebase 60709da..eb32194 onto 60709da
#
# Commands:
#  p, pick = use commit
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

Suppose the first 3 rows are the commits you should reorder, Suppose also that you want to reorder so that the third commit is first

Then reorder the first 3 rows to the following order:

pick eb32194 Regenerated gemspec for version 0.4.1
pick 4c39bca gemspec tweak
pick 85409cf Version bump to 0.4.1

# Rebase 60709da..eb32194 onto 60709da
#
# Commands:
#  p, pick = use commit
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

Save and Exit vi ready your commits will be reordered with the latest changes to HEAD from branch

If you really want them be in another branch ai perform the git cherry-pick <commit_hash> from the copy branch.

 4
Author: Filipe, 2014-02-18 13:21:48

To perform Step 2, you must first be in the master branch you can do this with git checkout master After that one must then create the task branch (Step 2), making a copy of the master, taking into account, that's what you originally wanted, right?.

git checkout -b novaBranch

After this command your new branch (copy of the current master) will already be selected, so return to branch master with git checkout master.

You should now go back to a version before committing the mentioned Task na master. Type git log to list your commits, look for the sha1 number as in the image:

insert the description of the image here

Typing git reset --hard 621256 (the first 6 are enough) I will return to time, in this example I returned to the first commit, and will be deleting the subsequent commits. But remember, you made a copy of the master branch to another branch with the task done.

From now on you can proceed with your correct procedure.

 3
Author: byteman, 2014-02-18 06:35:55

I know that the question is already solved, but to increase the knowledge line of the content already posted. In this Link there is a mini tutorial on how to apply the command cherry-pick where the purpose is to pass commits from one specific branch to another.

 2
Author: DNick, 2017-07-04 20:49:41