How to host your code with Bitbucket

This article will show you how to install and configure Git on your local Linux computer (CentOS in this example) or a remote Dev server, and then clone a working copy of your existing project to Bitbucket. I assume you already created an account and a Git repo with Bitbucket. If not just follow this instruction and then come back to this page.

Installing Git

To install Git under CentOS:

# yum install git-core

The global configuration file ‘.gitconfig‘ is located in the user’s home directory. All global settings are stored in this file.

The --global option need to be used for global configuration, otherwise your configuration is specific for the current Git repository.

If you want to configure Git system wide (for every user and repository on the system), use the --system option. All system configuration settings are stored in /etc/gitconfig file.

Basic configuration

Let’s configure the basic stuff like username, email and the default editor.

# git config --global "FirstNam LastName"
# git config --global "Email_Address"
# git config --global core.editor vim

You can list the existing global settings with the following command:

# git config --list

For more settings check out this page: git-config manual page

How to ignore certain files

If you want to ignore certain files, you need to create a file called ‘.gitignore‘ with the name of the files and/or directories that you want to exclude. This file can be anywhere (user’s home directory for example).

# touch ~/.gitignore
# echo "whatever_filename" >> .gitignore
# echo "whatever_directory" >> .gitignore

To ignore all files that end with ‘.jpg‘ and ‘.bak‘:

# echo "*jpg" >> .gitignore
# echo "*bak" >> .gitignore

Now configure Git to use this file as global .gitignore:

# git config --global core.excludesfile ~/.gitignore

Clone a working project

Let’s say you have a working copy of your project under /var/www/repos/project1 on your local system or Dev box, and you want to put the source code under version control.

1. Change to the working directory:

# cd /var/www/repos/project1

2. Create git repository:

# git init

3. Connect your repository:

# git remote add origin ssh://[email protected]/your_username/your_repo.git

your_username – is your Bitbucket’s username.
your_repo – is the new repository you created with Bitbucket.

4. Add and commit your source files:

# git add .
# git commit -m "Initial commit"

5. Push all changes:

# git push -u origin master

To check the log:

# git log

To check the status:

# git status

Removing files

If you want to remove a file. Use following commands:

# git rm test.txt
# git commit -m "Removed test.txt"
# git push origin

Editing files and pushing changes

# echo "Some code changes" >> test.txt
# git commit -m "Some code changes to test.txt"
# git push origin

Clone a repository to local system

If someone is working with you on the same project. He/she can clone the project like so:

git clone [email protected]:your_username/your_repo.git