简体   繁体   中英

Remove a Commit from a PR in Git

I created a PR but accidentally branched off the wrong commit.

commit 4
commit 3
commit 2
commit 1 <-- Want to get rid of first commit  

Commit 1 is actually part of another PR. I want to get rid of the first commit because it has nothing to do with the PR i created. What is the best way to go about this?

The simplest, don't-really-have-to-think-about-it way is usually an interactive rebase. Do git rebase -i <pr-base> (long: git rebase --interactive <pr-base> ), delete the first line, save and quit. That'll rewrite commit 1 out of history. Then it's probably git push --force-with-lease as usual to sync the changes in your case.

This will remove the commit like it never was there ie the changes will be undone and it will be removed from history for that branch. Logically this will not work if newer commits are based on changes in the commit you want to remove. (Git will tell you about this "conflict".)

On the branch of your pull request:

  1. git rebase HEAD~4 --interactive (4 is the number of commits to show)
  2. change the first word from pick to drop for the commit you want to remove. [Warning: Risk of data loss. Make sure this commit is part of another branch if you do not want to lose its content.]
  3. Save and close the "file".
  4. git push --force (This will forcefully overwrite the remote. Imo that is no problem if you do not expect anyone else working on this remote branch.)

(Well it looks like I was too slow.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM