-2

Suppose I have a branch and 5 commits. I've done some changes and noticed, that commits in range from 2 to 4 are relevant to the same fix and I'd like to squash them into a single one. Hovewer, I want to leave everything outside the borders untouched. Is it possible to achieve such behaviour?

Here is an example:

<sha-5> commit {5}        <---- this one should be untouched
|
|
|
<sha-4> commit {4}        <---- squash from this one 
|
|
|
<sha-3> commit {3}
|
|
|
<sha-2> commit {2}        <---- to this one
|
|
|
<sha-1> commit {1}.       <--- everything that goes before, including this commit should be untouched as well

CraZyCoDer
  • 367
  • 1
  • 2
  • 16

1 Answers1

0

You can use git rebase -i to start an interactive rebase session. In this case, you would want to start it from the parent of the first commit you want to squash.

Here's how to do it:

  1. You need to get the SHA of the commit before the commits you want to squash, in this case that's the SHA of commit {1}. Let's call it <sha-1>.

  2. Start an interactive rebase session using this SHA:

    git rebase -i <sha-1>
    
  3. In the text editor that opens, you will see a list of commits between the one you specified and the latest commit. It should look something like this:

    pick <sha-2> commit {2}
    pick <sha-3> commit {3}
    pick <sha-4> commit {4}
    pick <sha-5> commit {5}
    
  4. You want to squash commits {2} to {4}, so you should change pick to squash (or s for short) on those lines. The first commit of the group you want to squash should remain as pick, and all the following ones should be marked as squash. It should now look like this:

    pick <sha-2> commit {2}
    squash <sha-3> commit {3}
    squash <sha-4> commit {4}
    pick <sha-5> commit {5}
    
  5. Save and close the editor (if you're in vim, you can do this by typing :wq and hitting Enter).

  6. An editor window will open for you to change the commit message of the new squashed commit. After you’ve entered the new commit message, save and close the editor.

  7. Git will then reapply the commits that followed your squashed commits. In this case, commit {5}.

  8. If there are conflicts between the squashed commit and the reapplied commits, Git will pause and allow you to resolve those conflicts before continuing.

With this, you have successfully squashed your commits. You can verify this using git log, where you should now see the squashed commit and commit {5}, plus all the commits before commit {2}.

neo-jgrec
  • 167
  • 8