0

I have a branch "master" which has 5 commits:

A --> B --> C --> D --> E (master)

Now I want to merge the intermediate commits B, C, and D into one because their changes are trivial. I wish the result after merging is:

A --> B' --> E (master)

I tried the command git rebase -i A D and it results in a detached head that is not in the master branch. But I want to stay in the master branch, how can I do that?

6
  • also I want to reduce the git repo size by merging commits.
    – Mojie11
    Commented Feb 11, 2023 at 10:02
  • git rebase -i is the right thing here. Which instructions did you give in the todo-list? They should have been pick A, pick B, squash C, squash D, pick E.
    – j6t
    Commented Feb 11, 2023 at 10:03
  • @j6t so this is how I tried: git rebase -i A D, and it asked me to modify the commits B, C, D where I picked B and squashed C and D. And after saving, I was led to a detached head. I failed to do git push origin.
    – Mojie11
    Commented Feb 11, 2023 at 10:22
  • git rebase -i A or git reset A && git commit
    – phd
    Commented Feb 11, 2023 at 10:51

1 Answer 1

3

You use:

git rebase -i A

which will open a list of commands in a text editor for you, like this:

pick A Message of A
pick B Message of B
pick C Message of C
pick D Message of D
pick E Message of E

# instructional comments
# ...

In that, you'll change the texts in front of C and D to be squash (or just s), and you'll save and close the file. Then you'll be prompted to form the new commit message for B', and after you save and close that text editor, you'll be back on master with your now rewritten commits.

1
  • 1
    I followed your answer and succeed! Thank you.
    – Mojie11
    Commented Feb 11, 2023 at 10:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.