How to delete a commit?

Rafael Santini July 26, 2013

I'm using the Bitbucket (Git) with Netbeans. For some reason, a commit included a folder (build folder that contains binaries files, such as .class and .jar files) that was marked to be ignored. Now, I'm trying to delete the commit from repository and redo the commit.

3 answers

4 votes
AgentSmith
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 30, 2013

You can look into the git revert command. More info available here:

https://www.atlassian.com/git/tutorial/undoing-changes#!revert

Keith Russell August 7, 2019

Not what the questioner is asking. `git revert` adds a new commit. The reverted commit will still be in the history -- which means, in @Rafael Santini's case, all those .class and .jar files will still be taking up space in the repo (and in everyone's non-shallow clones).

Questioner is asking how to cut the commit out of the history. Git allows this (if we don't mind downstream hashes changing), but Bitbucket apparently retains these dropped commit in its auxiliary bookkeeping system: Links from Pull Requests and JIRA items will still take you to these homeless commits.

2 votes
Russell Spivey May 8, 2017

If the commit has not yet been pushed, you can use `git reset` to reset the branch to the commit before the bad commit. Beware that this changes git history and you will lose the commit(s) on the branch that occurred after the commit to which you reset the branch to.  

Russell Spivey May 8, 2017

Note: If the commit has already been pushed, you can still technically use `git reset`, but you will then have to use `--force` when pushing, which will overwrite git history on your remote git repository. Overwriting git history can be acceptable in some cases, but it is not a good idea in many cases, such as if other developers have checked out the same branch.
Also: remote repositories can be configured to prevent history re-writing, in which case you should not use `git reset` to remove pushed commits. 

0 votes
Keith Russell August 7, 2019

---- Edit: 2020-May-12 ----

This does not appear true (anymore? Or was it ever?) for older commits. Even though the commit hashes (and possibly log messages) remain in BB, including links from JIRA Issues, the actual hyperlinks take you to a "commit not found" page. (It takes some time for this to happen -- I'm guessing BB is running `git prune` under the hood on some schedule, for each repo.)

---- Original ----

For your purposes (cleaning up history of your repo, reducing the size of your clones), this is possible. But, even though you can delete them from your repo (using `git revert` or `git rebase`, followed by `git push --force`), it appears not to be possible to delete the commits from Bitbucket. Such commits will not appear in your repo's history, but will remain browsable via direct URL, or from links from associated JIRA items (which are indestructable if they arise from keywords in commit-log messages).

I'm guessing this is "a feature, not a bug". I'm guessing the Bitbucket team assumed users would be more mad about:

A) broken links (users themselves caused) in pull requests, JIRA items, and commit messages

... than they would be mad about:

B.1) seeing 7 identical commits linked with a JIRA item because the dev rebased her feature branch six times.

B.2) Having no way to remove from Bitbucket an accidentally-committed secret (e.g. hard-coded password accidentally left in code, customer data file, etc).

Personally, I would have preferred it the other way. But I'm guessing I'm in the minority, because I'd bet they wouldn't have gone through the trouble of creating the duplicate storage system unless some focus-group indicated that's what people wanted.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events