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?

ruohola
  • 21,987
  • 6
  • 62
  • 97
Mojie11
  • 85
  • 1
  • 3
  • 11
  • also I want to reduce the git repo size by merging commits. – Mojie11 Feb 11 '23 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 Feb 11 '23 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 Feb 11 '23 at 10:22
  • https://stackoverflow.com/search?q=%5Bgit%5D+combine+multiple+commits – phd Feb 11 '23 at 10:51
  • `git rebase -i A` or `git reset A && git commit` – phd Feb 11 '23 at 10:51
  • So you had the right idea, but with `git rebase -i A D` you leave `E` and `master` alone, and end up with a detached head at the rebased version of `D`. – j6t Feb 11 '23 at 17:30

1 Answers1

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.

ruohola
  • 21,987
  • 6
  • 62
  • 97