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:33

Another possibility is one of my personal favorite commands:

git rebase -i <commit>~1

This will start the rebase in interactive mode -i at the point just before the commit you want to whack. The editor will start up listing all of the commits since then. Delete the line containing the commit you want to obliterate and save the file. Rebase will do the rest of the work, deleting only that commit, and replaying all of the others back into the log.

查看更多
不流泪的眼
3楼-- · 2018-12-31 02:33

All the commands above restore the state of your work tree and index as they were before making the commit, but do not restore the state of the repository. If you look at it, the "removed" commit is not actually removed, it is simply not the one on the tip of the current branch.

I think that there are no means to remove a commit with porcelain commands. The only way is to remove it from the log and reflog and then to execute a git prune --expire -now.

查看更多
刘海飞了
4楼-- · 2018-12-31 02:33

delete local commit

As you can see on above image i want to delete revert"test change 2" commit(SHA1 ID: 015b5220c50e3dfbb1063f23789d92ae1d3481a2(you can get SHA1 ID by using gitk command in git bash)).

For that i can use(all below command work on local only. you need to push after delete):

  1. git reset --hard 515b5220c50e3dfbb1063f23789d92ae1d3481a2 //it back-up you to that commit (SHA1 ID of test change 4 commit is 515b5220c50e3dfbb1063f23789d92ae1d3481a2)
  2. git reset --hard HEAD~1 // it back-up you before one commit.
  3. git reset --hard HEAD^ // To remove the last commit from git

after delete:

after delete commit

查看更多
墨雨无痕
5楼-- · 2018-12-31 02:34

Careful: git reset --hard WILL DELETE YOUR WORKING DIRECTORY CHANGES. Be sure to stash any local changes you want to keep before running this command.

Assuming you are sitting on that commit, then this command will wack it...

git reset --hard HEAD~1

The HEAD~1 means the commit before head.

Or, you could look at the output of git log, find the commit id of the commit you want to back up to, and then do this:

git reset --hard <sha1-commit-id>

If you already pushed it, you will need to do a force push to get rid of it...

git push origin HEAD --force

However, if others may have pulled it, then you would be better off starting a new branch. Because when they pull, it will just merge it into their work, and you will get it pushed back up again.

If you already pushed, it may be better to use git revert, to create a "mirror image" commit that will undo the changes. However, both commits will be in the log.


FYI -- git reset --hard HEAD is great if you want to get rid of WORK IN PROGRESS. It will reset you back to the most recent commit, and erase all the changes in your working tree and index.


Lastly, if you need to find a commit that you "deleted", it is typically present in git reflog unless you have garbage collected your repository.

查看更多
后来的你喜欢了谁
6楼-- · 2018-12-31 02:34

Assuming you have not pushed to the remote repository, you could re-clone the repository. This has been my method of choice a few times.

查看更多
残风、尘缘若梦
7楼-- · 2018-12-31 02:34

git reset --hard

git push origin HEAD --force

If one or more of the commits is tagged, delete the tag(s) first. Otherwise the tagged commit is not removed.

查看更多
登录 后发表回答