Delete commits from a branch in Git

后端 未结 30 2020
醉话见心
醉话见心 2020-11-21 07:17

I would like to know how to delete a commit.

By delete, I mean it is as if I didn\'t make that commit, and when I do a push in the future, my changes wi

30条回答
  •  执笔经年
    2020-11-21 07:54

    The mistake:

    I git rebase -i --root'ed my branch, ignorantly thinking I could reword the first commit differing from the master (the GitHub for Windows default view is the comparison to master, hiding it's entirety).

    I grew a Silicon Valley beard while 900+ commits loaded themselves into Sublime. Exiting with no changes, I charged my battery then proceeded to shave, as all 900+ individual commits nonchalantly rebased - resetting their commit times to now.

    Determined to beat Git and preserve the original times, I deleted this local repository and re-cloned from the remote.

    Now it had re-added a most recent unneeded commit to master I wished to remove, so proceeded like so.

    Exhausting the options:

    I didn't wish to git revert - it would create an additional commit, giving Git the upper hand.

    git reset --hard HEAD did nothing, after checking the reflog, the last and only HEAD was the clone - Git wins.

    To get the most recent SHA, I checked the remote repository on github.com - minor win.

    After thinking git reset --hard had worked, I updated another branch to master and 1... 2... poof! the commit was back - Git wins.

    Checking back out to master, time to try git rebase -i , then remove the line... to no avail, sad to say. "If you remove a line here THAT COMMIT WILL BE LOST". Ah...glossed over new feature troll the n00b in the 2.8.3 release notes.

    The solution:

    git rebase -i then d, drop = remove commit.

    To verify, I checked out to another branch, and voila - no hiding commit to fetch/pull from the master.

    https://twitter.com/holman/status/706006896273063936

    Good day to you.

提交回复
热议问题