Most of the time, I have to use git rebase -i HEAD~<n>
to merge many commits to be one commit, then I have to manual edit many pick
to squash
,
it seems like too unintelligent,so I tried to use git rebase -i HEAD~<n> | dosomething
, but this way not work for my expect...

- 1,233
- 1
- 15
- 36

- 101
- 1
- 1
-
Use the search and replace feature of your editor? – Felix Kling Jun 24 '20 at 08:35
-
Does this answer your question? [How do I run git rebase --interactive in non-interactive manner?](https://stackoverflow.com/questions/12394166/how-do-i-run-git-rebase-interactive-in-non-interactive-manner) – LeGEC Jun 24 '20 at 09:06
-
Hi, welcome to SO. If I understand your question correctly : you want to edit the "rebase sequence" with a script, rather than an editor. Can you confirm that the question I linked as duplicate answers your need ? – LeGEC Jun 24 '20 at 09:07
-
https://stackoverflow.com/a/52007605/7976758 Found in https://stackoverflow.com/search?q=%5Bgit%5D+squash+many+commits – phd Jun 24 '20 at 13:55
-
`git reset --soft HEAD~
; git add -A; git commit --amend --no-edit` – phd Jun 24 '20 at 13:56
2 Answers
If you’re using rebase just to squash some commits, you can do that with git-reset and git-commit:
git reset --soft HEAD~<n>
git commit --amend --no-edit
You don’t need to git add
because reset’s --soft
stages reset changes.

- 5,023
- 3
- 34
- 50
If you always want to squash your current commit into the previous, you can use git commit --amend
. If you want to use the previous commit's message, just append the --no-edit
flag.
If you only want some commits to be squashed into a previous one, there's the --fixup
flag for commits. For every commit that you want squashed in, you'd make the commit as normal, but with that flag, like so
git commit --fixup PREV_COMMIT
This will automatically prefix your commit message with a fixup!
string. Then, when you you're done and want to clean up, you can git rebase --interactive --autosquash
and it will have the FIXUP
s already filled in for you.

- 1,013
- 1
- 9
- 21