How to modify a specified commit?

后端 未结 16 958
清酒与你
清酒与你 2020-11-22 01:53

I usually submit a list of commits for review. If I have the following commits:

  1. HEAD
  2. Commit3
  3. Commit2
16条回答
  •  無奈伤痛
    2020-11-22 02:39

    git stash + rebase automation

    For when I need to modify an old commit a lot of times for Gerrit reviews, I've been doing:

    git-amend-old() (
      # Stash, apply to past commit, and rebase the current branch on to of the result.
      current_branch="$(git rev-parse --abbrev-ref HEAD)"
      apply_to="$1"
      git stash
      git checkout "$apply_to"
      git stash apply
      git add -u
      git commit --amend --no-edit
      new_sha="$(git log --format="%H" -n 1)"
      git checkout "$current_branch"
      git rebase --onto "$new_sha" "$apply_to"
    )
    

    GitHub upstream.

    Usage:

    • modify source file, no need to git add if already in repo
    • git-amend-old $old_sha

    I like this over --autosquash as it does not squash other unrelated fixups.

提交回复
热议问题