unsafe_where_true
unsafe_where_true

Reputation: 6320

I need to delete a commit to a fork

OK, I did something stupid.

Now I wanted to issue a pull request, and all of a sudden I see my "check I can commit" commit. I'd rather not like that to appear on the pull request. :)

Can I entirely delete that commit? Can I issue a pull request on a single commit or will it pull all my commits?

I know I can locally git reset --hard HEAD~1(it's a small fix I could redo quickly) but that only fixes my local repo, not my github (forked) repo.

Upvotes: 12

Views: 24990

Answers (2)

Tuxdude
Tuxdude

Reputation: 49583

I believe the following sequence of commands should work assuming that your invalid commit was just one commit before the current HEAD, and the branch name is master.

NOTE: The following commands will be re-writing history which is not recommended if anyone has already cloned your repo. If that not be the case push -f should not be a big deal.

# Switch the current working tree to master branch
git checkout master
# Soft reset to get all the changes since HEAD~2 into the index/staging area
git reset --soft HEAD~2
# Remove the blafile from the staging area
git rm --cached blafile
# Commit the changes you actually intended to make
git commit
# Update the refs on the remote forcefully to push the newer commit.
# Note that if any one else has pulled master already with your blafile
# commit, they would be really pissed off with you now.
git push -f origin master

Upvotes: 3

nneonneo
nneonneo

Reputation: 179707

Lots of options.

The best option is probably to make a new branch and cherry-pick your fix into that branch:

git checkout -b my-fix-branch origin/master
git cherry-pick master
git push -u origin my-fix-branch

then do a pull request from my-fix-branch on GitHub. (This assumes your working branch is named master, based off the remote master; change the branch names as appropriate).


IF nobody has pulled or cloned your fork, you can rewrite history forcefully. Do git rebase -i HEAD~2 and delete the offending commit, then git push --force. This will break any other repo based on your fork, so do not do this if you suspect anyone else is using your repo.

Upvotes: 14

Related Questions