How can I reorder / merge commits using Git rebase?

After hours of playing with rebase, the repo is still different from what I need:

I would like to perform the following tasks:
[some of them were fine before I started messing around with rebase :(]

  • Move the upper latch ("Deleted by an outsider ...") before the branch turns off ("fix for # 226" in the upper right).
  • Combine the two commits that are in the twist / main branches. the comma and Moved loaded ... must be the same commit, and I don't need the comma commit message at all.
  • Merge the newly merged Move Loaded command into the backup branch and get rid of the rotation.
  • Move “master” to where “backup” now says.
  • What does this tag "remote / origin / master" mean?

enter image description here

I understand this sets a lot, but please provide actual GIT commands.

I don't mind reading and trying on my own, but I'm a little confused by results that don't match what I expected, and I really don't want to accidentally destroy any commits.

+6
git rebase
Jan 17 '10 at 8:43
source share
3 answers

The backup overwritten first.

 # Safety, should be a no-op if your gitk snapshot is accurate git checkout backup # Temporary branch git branch backup-save backup # Move top commit onto 'Fix for #226: git rebase --onto origin/master HEAD^ # Go back to saved branch parent (ie without the moved commit) git reset --hard backup-save^ # Rebase onto the moved commit (HEAD@{1} is where HEAD was 1 step # ago ie before the reset.) git rebase HEAD@{1} # Don't need the saved branch any more (although you might want # to keep it for a bit just in case). This deletes it: git branch -D backup-save 

Combine the two commits on a twist, ignoring the top commit message.

 git checkout twist git reset --soft HEAD^ # Just re-save the commit message, alternatively to skip the # editor step do this: git commit --amend -C HEAD git commit --amend 

Merge the twist branch into backup , delete the rotation branch.

 git checkout backup git merge twist git branch -d twist 

Move master . There are some fancy ways, but this is easiest. I assume that you want master point to the edited backup position, and not where it was originally.

 git checkout master git reset --hard backup 

remote/origins/master is a remote tracking branch and tells you where the branch pointer for the master branch in the remote origin repository, or rather, was when you last pulled, pushed or pulled.

+8
Jan 17 '10 at 9:26
source share

How not to be afraid

I would like to show you that no matter how bad you feel, commits are never destroyed *, and you can always go back to where you started.

I created a faux git tree that has the same shape as you:

Now I'm going to destroy the last three commits from the backup branch:

 $ git log --oneline 9b41f46 Removed extraneous whitespace 981e2e8 Merged the "loadscripts" function 005bc62 Pick up a few very fringe cases 07e71d9 Merged "getDepsForScript" function ... $ git reset --hard HEAD~3 HEAD is now at 07e71d9 Merged "getDepsForScript" function $ git log --oneline 07e71d9 Merged "getDepsForScript" function ... 

Unfortunately, that was bad. Go back to where we started. First, let's see what we did:

 $git reflog 07e71d9 HEAD@{0}: HEAD~3: updating HEAD 9b41f46 HEAD@{1}: commit: Removed extraneous whitespace ... 

You can see that when we reset, all git did this so that HEAD would point to this old commit. But no commits were actually lost - they simply became orphans, and not part of any industry. Let them become part of the "backup" branch again:

 $ git reset --hard 9b41f46 HEAD is now at 9b41f46 Removed extraneous whitespace $ git log --oneline 9b41f46 Removed extraneous whitespace 981e2e8 Merged the "loadscripts" function 005bc62 Pick up a few very fringe cases 07e71d9 Merged "getDepsForScript" function ... 

Git means you didn’t have to say you were sorry.

* Free objects eventually collect garbage, but only until they are at least two weeks old.

How to do what you want.

First merge the two commits into a master:

 $ git checkout master $ git rebase -i HEAD~2 

Git will launch your editor. Change this:

 pick 6389f4e Moved "loaded" function out of "require". pick 41fb646 comma 

:

 pick 6389f4e Moved "loaded" function out of "require". s 41fb646 comma 

And save it. git will launch your editor again. Change this:

 # This is a combination of two commits. # The first commit message is: Moved "loaded" function out of "require". # This is the 2nd commit message: comma 

:

 Moved "loaded" function out of "require". 

and save.

Now reorder the commits to 'backup':

 $ git checkout backup $ git rebase -i remotes/origin/master 

When your editor appears, change this:

 pick ec7f71c moved "loaded" function out of "require" pick 4a76897 Replaced maploaded event pick 07e71d9 Merged "getDepsForScript" function pick 005bc62 Pick up a few very fringe cases pick 981e2e8 Merged the "loadscripts" function pick 9b41f46 Removed extraneous whitespace <----- 

:

 pick 9b41f46 Removed extraneous whitespace <----- pick ec7f71c moved "loaded" function out of "require" pick 4a76897 Replaced maploaded event pick 07e71d9 Merged "getDepsForScript" function pick 005bc62 Pick up a few very fringe cases pick 981e2e8 Merged the "loadscripts" function 

and save.

Now cherry - select the combined "Moved download" that completes the wizard and to the current branch ("backup")

 $git cherry-pick master 

Make a master point for the same commit as the backup

 $git checkout master $git reset --hard backup 

Get rid of the twist branch

 $git branch -D twist 
+6
Jan 17 '10 at 9:30
source share

There are a few things you are trying to achieve here, and some of them are unclear from the diagrams provided exactly what is needed, but the following pointers may help:

  • Instead of trying to move your branch, I will first combine the commits that you made in that branch, then apply the final commit to the main branch, as you end up targeting it. You may have to resolve some conflicts at this point, but you still have to do this. See the following two tips for information on how to achieve this.

  • To combine the two commits, you can do an interactive rebase .

  • To select a single commit from one branch and apply it to another, while on the target branch use git cherry-pick . You can delete the old branch with git branch -D twist when done.

I hope that these tips will help you achieve your goal.

0
Jan 17 '10 at 9:31 on
source share



All Articles