Fix last n commits to previous commit automatically I often find myself doing rebase manually in an interactive form in a way as follows. Say I want to squash two last commit to third commit.
First I execute:
$ git rebase -i HEAD~3

Now editor pops up, I mark two last commits as fixup. I close the editor and then rebasing is done (if there are no errors). I'd like to automate this by executing a script with a parameter that gives a number of commits, i.e. when 2 is given as parameter, last two commits are fixup to third commit. Please see an example:

some other commits...
add nice feature
fix 1
fix 2

In that scenario I'd like add nice feature to incorporate changes from fix 1 and fix 2, and I like to fix 1 and fix 2 not be present at all.
I wrote a bash script that does what I want. It works seems to work fine.
UPDATE
In this review, I'm most interested in knowing whether my approach to git is a proper one, especially if it doesn't break anything during rebasing.
fixup() {
  local no_of_commits=&quot;$1&quot;
  if [ -z &quot;$no_of_commits&quot; ]; then
    echo &quot;You must provide a number of commits to fixup!&quot;
    return 1
  elif ! [[ $1 =~ ^[0-9]+$ ]]; then
    echo &quot;$no_of_commits is not a number!&quot;
    return 2
  fi

  #git stash save

  git reset --soft &quot;HEAD~$(no_of_commits)&quot; &amp;&amp;
    git add --all &amp;&amp;
    git commit --fixup &quot;$(git rev-parse HEAD)&quot; &amp;&amp;
    GIT_SEQUENCE_EDITOR=true git rebase --interactive --autosquash --no-fork-point &quot;$(git rev-parse HEAD~2)&quot; &amp;&amp;
    echo &quot;Rebased $(no_of_commits) succesfully!&quot;

  #git stash pop
}
The error messages should go to standard error, rather than standard output:

  if [ -z "$no_of_commits" ]; then
    echo "You must provide a number of commits to fixup!" &gt;&amp;2
    return 1                                              ### HERE


I don't think there's a good case for returning distinct error codes for missing argument and invalid argument.  Are you ever going to make use of the different status values?