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.
Either swap commits for the master and then take out the original ones from the master or leave them there?
– luiscubal
@luiscubal I want to take the commits from the master, and put in a new branch.
– fotanus