Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete commits from git on Github and Bitbucket

I accidentally pushed up files from my .idea directory in my Django project which I had in my .gitignore file. I am trying to completely delete the commit from my bitbucket repository since there is someone else Im working with on the project and he can't pull my changes without affecting his own .idea files. I have seen other SO questions where they say to use git revert, however I remember there was another command where you pushed the last good commit you made, and everything after that was deleted from the master branch. For example

Commit History:

94ca48e

55fab05

3813803

I want to delete 94ca48e, and 55fab05. There was a command I found once where you could make 3813803 the most current commit, and everything in the remote repository after that commit would be deleted, but I can't find it anywhere.

like image 824
TJB Avatar asked Jul 15 '16 17:07

TJB


People also ask

How do I delete a commit in bitbucket?

you can reset to HEAD^ then force push it. It will delete your last commit and will reflect on bitbucket as commit deleted but will still remain on their server.

How do I delete commits from github?

To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset HEAD~2 to remove the last two commits. You can increase the number to remove even more commits.

Can you delete commit history in bitbucket?

After your team has reviewed the state of the local repository, force push the changes back up to Bitbucket to overwrite the repository's existing commit history. Any users cloning or forking from this repository should be asked to git rebase any branches that contain the old repository history.

Can you delete commit history in github?

If you commit sensitive data, such as a password or SSH key into a Git repository, you can remove it from the history.


2 Answers

Use git reset --hard 3813803. This can not be undone and works locally as well as remote.

For remote push using git push --force origin master

Have a look at the git docu by Atlassian here.

Let me also quote from there:

Whereas reverting is designed to safely undo a public commit, git reset is designed to undo local changes. Because of their distinct goals, the two commands are implemented differently: resetting completely removes a changeset, whereas reverting maintains the original changeset and uses a new commit to apply the undo.

git reset is the one to be used here, though, because you are asking for complete deletion.

like image 144
harmonica141 Avatar answered Oct 23 '22 00:10

harmonica141


Even if the commits are HARD reset, unwanted commits would still show up in the history. If the history also needs to be reset, then I am afraid only way to do that is to delete and recreate the branch.

like image 1
MTEK Avatar answered Oct 23 '22 01:10

MTEK