1

Reading the answers to:

Git - Remove commit from history

I only see suggestions involving interactively editing a TODO file. Is there a way to drop a single commit, given its hash, and keep ("pick") all subsequent commits, without user interaction?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • 2
    It looks like it can be done using the `GIT_SEQUENCE_EDITOR` environment variable and for example `sed`: `GIT_SEQUENCE_EDITOR="sed -i '/p /d'" git rebase -i ` (the `-i` switch is required: it will edit the `/.git/rebase-merge/git-rebase-todo` file in-place). The `GIT_SEQUENCE_EDITOR` hint found here: https://stackoverflow.com/questions/12394166/how-do-i-run-git-rebase-interactive-in-non-interactive-manner – terrorrussia-keeps-killing Nov 12 '21 at 20:35
  • I also was wondering of this question earilier, but I think that `git-rebase` is "too porcelain" for this case, and it probably can be resolved by using some plumbing commands. – terrorrussia-keeps-killing Nov 12 '21 at 20:36
  • @fluffy: Don't you mean `/pick /d`? – einpoklum Nov 12 '21 at 21:30
  • When you say "remove the commit", do you want to keep what's in the commit, or do you want to discard that too? – Alecto Irene Perez Nov 12 '21 at 22:15
  • 1
    https://stackoverflow.com/a/32318688/7976758 : `git rebase --onto SHA^ SHA` Found in https://stackoverflow.com/search?q=%5Bgit-rebase%5D+remove+commit – phd Nov 12 '21 at 22:16
  • @einpoklum Yes. My git is configured to use abbreviations, like p for pick, d for drop, etc. – terrorrussia-keeps-killing Nov 12 '21 at 22:29
  • @AlectoIrenePerez: "drop" is a keyword you can use when editing the rebase todo. – einpoklum Nov 12 '21 at 22:40
  • @matt: 1. Because I already know what I want to do, and I don't want to type as much. 2. For use in automation scripts. – einpoklum Nov 13 '21 at 07:51

1 Answers1

1

Basic solution:

A solution due to a comment by @phd, and originally found here:

To remove the commit with hash value SHA, write:

git rebase --onto SHA^ SHA

But remember that rebasing and merges have complex interactions, which you should read about if your history involves merges.

Cherry on top:

You can create an alias for this idiom, via ~/.gitconfig:

[alias]
    drop = !git rebase --onto $1^ $1 && :

with which you will be able to write simply:

git drop SHA
einpoklum
  • 118,144
  • 57
  • 340
  • 684