1

I am looking for an easy way to rearrange snippets between patches. Say I have some repo with two consecutive commits to it:

git init
echo -e "foo\n\nbar\n\nbaz" > file
git add file 
git commit -m 'initial'
sed -i 's/bar/boom/' file 
git commit -am 'first'
sed -i 's/boom/qux/' file 
sed -i 's/baz/quux/' file 
git commit -am 'second'

so my commit history looks like this:

   first

--- a/file
+++ b/file
@@ -1,5 +1,5 @@
 foo

-bar
+boom

 baz


   second

--- a/file
+++ b/file
@@ -1,5 +1,5 @@
 foo

-boom
+qux

-baz
+quux

but I want to look it like this instead:

   first

--- a/file
+++ b/file
@@ -1,5 +1,5 @@
 foo

-bar
+boom

 quux


   second

--- a/file
+++ b/file
@@ -1,5 +1,5 @@
 foo

-boom
+qux

-baz
+quux

Is there an easy way to do this, with rebase and various commands with -p options? I think I could do an interactive rebase, edit the first comment, use git reset -p to remove the last part of the change, save it as a temporary commit, finish the rebase, then do another interactive rebase to squash the temporary commit into the following one. That takes a lot of thinking and effort though in a real-world situation where I want to rearrange two (or more) big commits and need to carry many snippets in both directions, possibly skipping some intermediate commits. Is there an easier way to do this, without creating temporary commits?

Tgr
  • 27,442
  • 12
  • 81
  • 118

1 Answers1

0

Unless I'm misreading the quesiton...

You could create a separate branch, make the intermediate commits to that, then merge it back in using --squash which combines all commits to a single commit.

See Squash my last X commits together using Git

Community
  • 1
  • 1
Ray
  • 40,256
  • 21
  • 101
  • 138
  • That doesn't seem any simpler, but I might be misunderstanding what you mean. Doing an interactive rebase and changing `pick` to `squash` is not the hard part. – Tgr Aug 17 '15 at 23:22