Delete commits from a branch in Git

2018-12-31 02:21发布

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 will not push to the remote branch.

I read git help, and I think the command I should use is git reset --hard HEAD. Is this correct?

27条回答
听够珍惜
2楼-- · 2018-12-31 02:52

To delete in local branch, use

git reset --hard HEAD~1

To delete in a remote branch, use

git push origin HEAD --force
查看更多
高级女魔头
3楼-- · 2018-12-31 02:52

If you've already pushed, first find the commit you want to be at HEAD ($GIT_COMMIT_HASH_HERE), then run the following:

git reset --hard $GIT_COMMIT_HASH_HERE
git push origin HEAD --force

Then each place the repo has been cloned, run:

git reset --hard origin/master
查看更多
千与千寻千般痛.
4楼-- · 2018-12-31 02:53

Source: https://gist.github.com/sagarjethi/c07723b2f4fa74ad8bdf229166cf79d8

Delete the last commit

For example your last commit

git push origin +aa61ab32^:master

Now you want to delete this commit then an Easy way to do this following

Steps

  1. First reset the branch to the parent of the current commit

  2. Force-push it to the remote.

git reset HEAD^ --hard

git push origin -f

For particular commit, you want to reset is following

git reset bb676878^ --hard

git push origin -f
查看更多
登录 后发表回答