I'm using the following workflow with git:
However, I skipped step number (2) and got all my code in the master. How can I do to change my commits from the master to another branch?
I'm using the following workflow with git:
However, I skipped step number (2) and got all my code in the master. How can I do to change my commits from the master to another branch?
Get SHA1 from the first commit to 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 commits and the master will be reset to not contain commits branch (you can do a merge
again if you want).
PS: I'm assuming you have not yet performed step 6 ( push
), otherwise you'll have to push -f
and align with people who have already run the pull
and got those commits from the remote repository. They should also discard the commits locally before doing push
, or they will reintroduce commits into master
.
P.S. 2: I'm also assuming you have a "clean" sequence of commits to move to the new branch . That is, HEAD
is the last commit that should belong to the branch and there are no intermediate commits branch from the initial commit (eg, there was no pull
in the middle of the path introducing commits not related to brach ) . If this is not true you should 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? .
Warning : You should not do this if you have already forced push
to the remote repository, as other developers may already have done pull
of that repository.
You do not need to remove the commits
from the local master to another branch
if you do not really want to. Your problem is that you have done pull
of the remote master, only commits
that were already in the remote master were above your commit commit 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>
-i
is the last HEAD
no commit
branch
is a kind of subtraction Suppose that ~
is 3, the meaning of <número_de_commits_que_devem_ser_reordenados>
is to perform the task from the penultimate HEAD~3
.
The commit
will be displayed, next in an editor like commits
, note that all lines preceded by the vi
character are explanatory comments about what you can do with the #
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 git rebase -i HEAD~3
you should reorder,
Also assume that you want to reorder so that the third
first
Then reorder the first 3 lines for 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 from commits
ready your commits will be reordered with the last changes in commit
of vi
If you really want them to be in another HEAD
ai, make branch
from the copy branch.
To perform step 2, you must first be in the master branch
you can do this with git checkout master
After that you should 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, then return to branch master with git checkout master
.
You should now go back to a version before committing the task mentioned in the master.
type git log
to list your commits, search for the # sha1 as in the image:
Entering git reset --hard 621256
(the first 6 are already enough) I will return to the time, in this example I returned to the first commit, and the subsequent commits will be deleted. But remember, you have made a copy of the master branch to another branch with the task done.
From now on you can follow through with your correct procedure.
I know that the question is already solved, but to increase the knowledge of the contents already posted. In this link there is a mini tutorial on how to apply the cherry-pick command where the intention is to pass commits from one specific branch to another.