Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git: rewrite history: reorder and merge commits

Tags:

Current situation:

origin/mybranch --- A1 --- B1 --- A2 --- B2 --- A3 --- B3   mybranch 

I want to clean that history up (A1 to B3), esp. since I have not pushed it anywhere yet and because I want to prepare a patch which just those B*.

What I want is:

origin/mybranch --- A1+A2+A3 --- B1+B2+B3   mybranch 

I probably will not push this at all (or if I will, only the summed B*, I must remove the A* commits at all) and while I am working further on this, I might get additional such commits, i.e. like this:

origin/mybranch --- A1+A2+A3 --- B1+B2+B3 --- A4 --- B4   mybranch 

And I want to rewrite that then again like above.

I don't just want to know any method to do this (because I would be able to get sth like in a somewhat hacky way), I am esp. asking here for the right/best/cleanest/easiest way to do this.


What I am doing esp. is: I am working on the official xorg-xserver-1.7 branch and want to prepare a patch (B*). Because I want to be able to replace my self-compiled xserver with the system one for simple testing purpose, I have applied a bunch of Debian/Ubuntu patches (A*). However, when I am going to post that patch somewhere, I want to exclude those.

like image 713
Albert Avatar asked Sep 02 '10 11:09

Albert


People also ask

Does git merge rewrite history?

Rebase is one of two Git utilities that specializes in integrating changes from one branch onto another. The other change integration utility is git merge . Merge is always a forward moving change record. Alternatively, rebase has powerful history rewriting features.

Does rebase rewrite history?

Interactive rebase is one of those tools that "rewrite" Git history – and you shouldn't do this on commits that have already been shared with others. With this little warning message out of the way, let's look at some practical examples!

What is git rewrite?

Git rebase Rebasing is the process of taking all the changes that were committed on one branch and applying them to a new branch. Run git rebase and add in the -i option to rewrite, replace, delete, and merge individual commits in the history. You can also: Rewrite a past commit message.


1 Answers

You want to do an interactive rebase.

The first thing I like to do when trying out git commands is to make a backup copy of my branch:

$ git branch my-backup-branch 

Let's say that your A1 commit has a hash of 152274b. Try this:

$ git rebase -i 152274b^ 

That command will bring up your editor (usually vim) with something like this:

pick 152274b A1 pick 14b0838 B1 pick 661b993 A2 pick a6510db B2 pick 557e171 A3 pick 85da0e4 B3  # Rebase 39a47e4..85da0e4 onto 39a47e4 # # 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. # 

From here you can change the order of the commits, delete them entirely and even squash them. In this example, you probably want to move and squash like this:

pick 152274b A1 squash 661b993 A2 squash 557e171 A3 pick 14b0838 B1 squash a6510db B2 squash 85da0e4 B3 

Give it a try. If you end up in a state that you didn't want, you can always go back to the state you saved in your backup branch:

$ git reset --hard my-backup-branch 
like image 199
Neall Avatar answered Sep 26 '22 03:09

Neall