Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reorder/combine commits using Git rebase?

Tags:

git

rebase

After quite a few hours playing with rebase, the repo still looks different from what I need:

I would like to accomplish the following tasks:
[some of which were OK before I started messing with rebase :( ]

  • Move The top commit ("Removed extraneous...") to before the branch off (Right above "fix for #226").
  • Combine the two commits that are in the 'twist/main' branch.   "comma" and "Moved loaded..." should be the same commit, and I don't need the commit message of "comma" at all.
  • Merge the newly combined "Move loaded" commit into the 'backup' branch, and get rid of 'twist'.
  • Move 'master' to where it now says 'backup'.
  • What does that "remote/origins/master" tag mean?

enter image description here

I realize that this is asking a lot, but please include actual GIT commands.

I don't mind reading and trying on my own, but am a bit confused by the results not matching what I would've expected, and I really don't want to accidentally destroy any commits.

like image 610
SamGoody Avatar asked Jan 17 '10 08:01

SamGoody


People also ask

Can I reorder commits with rebase?

Interactive Rebase also allows you to reorder commits. Simply drag and drop a commit between two existing commits to reorder history.

Can you reorder git commits?

By rearranging the commits in the file, git will rearrange the commits in the test branch. Now it will look the following: "Commit 3" is now the first commit.

How do I reorder commits in a branch?

SourceTree makes reordering commits really easy. Right click on the last commit of the remote branch (origin/master for example), and choose “rebase children of <hash> interactively…” from the context menu. A dialog will appear with a list of the commits that are above the one you selected.

Does rebasing remove merge commits?

Git has two different methods to sync branches: rebase and merge . Rebase offers a cleaner way to maintain the repository, while merge offers a one-click solution. Rebase is powerful, but it comes with a price. One problem I face with rebase is, it removes merge commits.


1 Answers

First, re-ordering the backup commits.

# 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's parent (i.e. without the moved commit)
git reset --hard backup-save^

# Rebase onto the moved commit (HEAD@{1} is where HEAD was 1 step
# ago i.e. 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 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, remove the twist branch.

git checkout backup
git merge twist
git branch -d twist

Move master. There are multiple fancy ways, but this is simplest. I'm assuming that you want master to point to the edited backup position and not where it originally was.

git checkout master
git reset --hard backup

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

like image 87
CB Bailey Avatar answered Sep 27 '22 18:09

CB Bailey