Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to remove a commit from Atlassian Bitbucket

Hello!

In this article I will show you how to delete a commit from a Bitbucket repository.

Why would we need to delete a commit?

It can happen if you accidentally pushed sensitive information into your Bitbucket repository.

For example, you forgot to exclude a file with passwords from adding to git or you provided your password in one of the source files to test how the program works and then you forgot to remove your password from this source file. You pushed commits from your local repo to your Bitbucket repo and that is it. Your sensitive information is there. Everyone with access to this repository can see your password, for example. How to remove this sensitive information?

You can delete your repository and create a new one. But it would make you apply all settings from the deleted repo back and warn your mates, that you created a new repo. It sounds too complicated.

A simpler way would be to remove the wrong commit.

In this tutorial I will show you step by step how to do it.

Create a Bitbucket repo

First I will create a Bitbucket repository:

Screenshot 2020-05-31 at 10.47.30.png

Create a local repository, make changes, push to Bitbucket

I will create a new folder for my repository:

mkdir bitbucket-tutorial
cd bitbucket-tutorial/

I will initialise the git repository and add the remote for my Bitbucket repository:

git init
git remote add origin https://alex1mmm@bitbucket.org/alex1mmm/bitbucket-repo-article.git

I will create a new file and add some contents:

touch mynewfile.txt

Here is the contents of mynewfile.txt:

this line is initial commit

I will commit the changes and push the changes into Bibucket:

git add *
git commit -m "initial"
git push origin master

I will make changes to mynewfile.txt:

this line is initial commit
I added my password info here

I will commit changes and push to Bitbucket:

git add *
git commit -m "sensitive info"
git push origin master

If we have a look at our Bitbucket now we will see two commits:

Screenshot 2020-05-31 at 11.08.11.png

And our commit with sensitive information contains our password line:

Screenshot 2020-05-31 at 11.09.25.png

Even if we remove our password from mynewfile.txt, commit changes and push to Bitbucket, we will still be able to see our commit with sensitive info and hence our password.

We need to remove this commit completely from our Bitbucket repo

Remove commit with password

Let's first find the id of our commit:

git log --oneline --graph --decorate

Here is the output:

Screenshot 2020-05-31 at 11.18.31.png

I marked the id of our commit with a red rectangle.

Now let's remove this commit. We need to reset our git repository to the commit which took place before our wrong commit. The id of the previous comment is 3e90065:

Screenshot 2020-05-31 at 11.23.08.png

Now let's reset our repository

git reset --hard 3e90065

Now mytextfile.txt looks like this:

this line is initial commit

Correct! We do not have our information about the password.

Our commit history looks like this:

Screenshot 2020-05-31 at 11.26.25.png

We do not have our comment with sensitive information. Also correct!

But if we look at our Bitbucket repository, we still have our sensitive info comment:

Screenshot 2020-05-31 at 11.28.11.png

That is because we did not push our changes form our local repository to our Bitbucket repository. Let's do it:

git push --force origin master

Screenshot 2020-05-31 at 11.31.53.png

And now if you have a look at our repository in Bitbucket we will have only our initial commit:

Screenshot 2020-05-31 at 11.33.19.png

Success! We removed the commit with our password from our Bitbucket repository.

6 comments

Thank you very much for a very clear explanation!

I tried this on our Bitbucket and it did not truly work.

Although the commit disappears from the list, if you know the commit ID you can still look at it and see the password file by manually entering the URL.

Like # people like this

Simple explanation, great result !!! THX

Thanks this worked. I had to get rid of a rouge commit from a rogue dev!

eric-914 I'm New Here Sep 03, 2021

Will this work if the commit in question is not the last, most recent commit?

Comment

Log in or Sign up to comment
TAGS
Community showcase
Published in Bitbucket

New improvements to user management in Bitbucket Cloud 👥

Hey Community! We’re willing to wager that quite a few of you not only use Bitbucket, but administer it too. Our team is excited to share that we’ll be releasing improvements throughout this month of...

3,725 views 10 16
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you