Using BitBucket as a mechanism for code recovery

 In Backup, Bitbucket

In this tutorial you will see how to use Bitbucket for your projects. We’ll try to show you essential commands for pushing/pulling files or directories from/into Bitbucket, adding or deleting files and update your repositories.

In this scenario, we have a linux machine with apache installed and we’ll try to use Bitbucket for versioning, backup and restore website files from repository.

BitBucket is a online repository system mainly used for version control for software and code. It uses the inbuilt linux module GIT, to communicate with the online repo. In this example, not only can it be used for version control of your code / sets of code, it can also be used to restore a set of code, should a failure or issue occur. For example, if some of your files get corrupted you can restore  the files or even all your project from Bitbucket repository.

Before using git, you should first create a website on one of the webservers (link) and a Bitbucket repository.

1. Creating a new repository on Bitbucket

Once you’ve got the website running (e.g. you need to access your Bitbucket account and create a new repository for it.

Browse and press the Log in button. Use your credentials to get into your account:

Once you’re logged in, you’ll see all your existing repositories. To create a new repository, press:

Create -> Repository




Next you need to give a name and a description to your new repository and click Create repository. Usually, it’s a good practice to name your repository as your website. (e.g. if your website is, your repository should be named mywebsite).



After you clicked Create repository you should be able to see few lines of instructions on the screen provided by Bitbucket. Although these instructions are coreect, there is no need to follow them as we’ve provided bellow.



Done with the Bitbucket website. Now follow the next step to add your to your repo.

2. Add website files to your Bitbucket repository

Once you have created the repository you should be able to populate it with your project files (website files).

Now you can go back to your webserver and navigate to your project directory:

cd /var/www/html/mywebsite

Once you’re in mywebsite directory use the following command to initialize git:

sudo git init

If you list the files now (use ll command) you’ll see a .git directory has been created. This directory contains data about your repository you have created on Bitbucket website and that’s what Git uses to determine where to push your changes.

Now you need to set your remote origin from bitbucket using this command:

sudo git remote add origin

Note: myrepo.git is the name of the repository so each time you create a new repository, you need to specify the origin. Just replace myrepo with your repository name (e.g. sudo git remote add origin

Next use the following commands to add your files to the repository (remember you’re still in the mywebsite directory):

sudo git add *

The star symbol (*) means that you want to add all the files in the directory on your repository.

If you need to add only one file, just type the name of the file instead of the star symbol. If your file has an extension (e.g. file.txt) type that too:

sudo git myfile.txt

3. Commit changes and push the files to Bitbucket

Now you need to commit all the changes you’ve made giving a description:

sudo git commit -m ‘My initial commit’

and push your files up to bitbucket:

sudo git push -u origin master

You’ll be prompted for your Bitbucket. Enter your password.

At this moment, if you navigate back to your Bitbucket account, you’ll be able to see your repository populated with your website files.

4. How do you update your bitbucket repository after you have made changes to a file?

If you need to update changes to your repository you need to follow 3 steps: add, commit and psuh.

After modifying a file (e.g. myfile.txt) you need to add it to commit:

sudo git add myfile.txt

At this point, if you type git status you’ll see that your file (myfile.txt) has been added to commit:

On branch master
Your branch is up-to-date with ‘origin/master’.

Changes to be committed:
(use “git reset HEAD …” to unstage)

modified: newfile.txt

Next you need to commit the changes and push it to the bitbucket repository:

sudo git commit -m ‘myfile changes’

sudo git push

Note: when you push files you will be prompted for the bitbucket password. Enter your password.

5. What if I delete a file or a directory accidentally from the project? (Restore files and directories)

If you delete for example myfile.txt accidentally you could bring it back with the following command:

sudo git checkout myfile.txt

You will use the same command for a directory:

sudo git checkout mydirectory

After restoring the file or directory you will need to change the owner back to www-data (in my case www-data is the apache user). When you restore a file, it will restore with the root owner. You can see this by using ll command while in the project directory. The output should look like this:

-rw-r–r– 1 root root 12 Oct 1 14:37 myfile.txt

To change the owner use the following command:

sudo chown www-data:www-data myfile.txt

For directories use:

sudo chown www-data:www-data -R mydirectory

Now if you run the ll command again you should see this output:

drwxr-xr-x 5 www-data www-data 4096 Oct 1 15:35 myfile.txt

Notice the owner user and the owner group has changed from root to www-data. 

Remember: If you didn’t commit and push the changes you have made to the file, the checkout command will bring back the file with your last commit. For example, if you add some text to myfile.txt, save it and delete it, when you bring it back with git checkout command the file will not have the text you added before you deleted it.

That is all you need to do.

Three Common Processes when using BitBucket Version Control


  • Create the website and initialize git

    Create your new website along with the database on one of the existing webservers. Navigate to your project directory 'cd /var/www/html/mywebsite' and initialize git using: 'sudo git init'

  • Create the repository

    Go to Bitbucket website and create your new repository for the webiste. It must have the same name as your new website.

  • Add files to repository

    Populate the new Bitbucket repository with your wbsite files using 'sudo git add *' command

  • Commit changes

    Commit your work after adding the files, using the following command: sudo git commit -m 'my new commit'. You can write anything between the single quotes.

  • Push files to repository

    Push all the files up to your repository using 'sudo git push'. Now you should be able to see all your project files on the Bitbucket repository.


  • Add files to commit

    Add your changed files to commit using 'sudo git add filename or directoryname'

  • Git status

    Use 'git status' command to see if your files have been added to commit. You should be able to see in green.

  • Commit your changes

    Commit your work after adding the files, using the following command: sudo git commit -m 'my new commit'. You can write anything between the single quotes.

  • Push files to repository

    Push all the changed files up to your repository using 'sudo git push'.


  • Restore your file or directory

    Bring back a deleted file using 'sudo git checkout myfile.txt' command. This command can be used for a directory too so instead of 'myfile.txt' just type the directory name.

  • Change the file or directory owner

    When you bring back a file or directory you must change its owner back using 'sudo chown www-data:www-data myfile.txt'

  • Check if the owner has been changed

    To see if your command has succeeded use ll command while you are in the project folder.

Recent Posts

Leave a Comment

Contact Us

We're not around right now. But you can send us an email and we'll get back to you, asap.

Not readable? Change text. captcha txt

Start typing and press Enter to search