How do I copy commits from one branch to another?

Asked

Viewed 17,336 times

14

I’m using the following workflow with git:

  1. I set a task
  2. I create a branch for her from the master
  3. Implement the task
  4. I pull on the master
  5. I merge my branch into master
  6. I push the master

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

  • 2

    Either swap commits for the master and then take out the original ones from the master or leave them there?

  • @luiscubal I want to take the commits from the master, and put in a new branch.

4 answers

11


Get the SHA1 from the first commit you want to move. 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> 

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


P.S.: I’m assuming you haven’t executed step 6 yet (push), otherwise you will have to make a push -f and align with the people who have already executed the pull and obtained these commits remote repository. They should also discard the commits locally before making push, or they will reintroduce the commits in the master.

P.S. 2: I’m also assuming that you have a "clean" sequence of commits to move to the new branch. That is, the HEAD is the last commit which must belong to the branch and there are no commits intermediaries that should not belong to the branch since the commit initial (e.g., there was no pull in the middle of the way introducing commits unrelated to Brach). If that’s not true you should do cherry-pick only of commits desiring.

 # 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?.

  • 1

    Note: git branch does not change branch (it just creates it). So if it’s already in master, you don’t have to run git checkout master.

  • I was editing :). Fixed

  • Sometimes the "dumbest" way is wiser, because it avoids getting lost in the process. It can be simpler to generate patches with git format-patch, and then manually apply with git am or git am -3.

  • Hello epx, the patches are a good alternative, but do not think reduce the work in this case. In the case of commits linear it is still necessary to get the hash of the first commit, generate the set of patches that commit until the HEAD, create the branch, apply the patches and reset the master.

4

Heed: You should not perform this procedure if you have already forced the push to the remote repository, as other developers may already have pull of that repository.

You don’t need to remove 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 who were already in the remote master were above their commited changes recently, 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 the iterative mode
  • The HEAD is the last commit in the branch
  • The ~ is a kind of subtraction

Suppose that <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, we are next in an editor as the vi, note that all lines preceded by the character # are explanatory comments on 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 lines are the commits which you must reorder, Suppose also that you want to reorder so that the third commit stay at first

Then reorder the first 3 lines 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 get out of vi ready your commits will be reordered with the latest changes in HEAD of branch

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

3

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

git checkout -b novaBranch

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

Now you should go back to a version before the task commit mentioned in master. typhoon git log to list your commits, look for the number sha1 as in the image:

inserir a descrição da imagem aqui

Typing git reset --hard 621256 (the first 6 are enough) I will return to time, in this example I went back to the first commit, and the later commits will be deleted. 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.

2

I know the question is already solved, but to increase the knowledge line of the contents already posted. In this link there is a mini tutorial on how to apply the command Cherry-pick where the purpose is to pass on commits of a branch specific to another.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.