Cheetsheet of Git commands that I frequently use with my Bitbucket account. Not comprehensive!

Git is an open source version control system that forms the basis many cloud-based software development projects. I use it for software development; storing random notes and files; as well as the foundation for my continuous integration/continuous development blog. Here’s how…

Table of Contents

Prerequisites

You’ll need an account with a Git service. I use Bitbucket.org, because it provides both public and private repositories free-of-charge as well as other useful functionality like static website hosting. Another popular alternative is Github.com.

Installation (Debian / Ubuntu)

Install the Git software on your client workstation.

user@d9-dvm:~$
1
2
# Install Git and cURL
sudo apt-get install -y git curl

Configuration

Author

Assign the default author on your workstation using your username and email address associated with your Git service provider. We use the optional --global flag to assign these parameters to all projects on this workstation.

user@d9-dvm:~$
1
2
git config --global user.email {your-email-address}
git config --global user.name {your-git-username}

SSH access

Set up read-write SSH access to the Bitbucket account. This is required to easily push new content to the cloud.

user@d9-dvm:~$
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Generate the SSH key:
ssh-keygen -t rsa

# To simplify the next step, assign the public key to a terminal variable.
key=$(cat ~/.ssh/id_rsa.pub | cut -d " " -f 1,2)

# To simplify multiple successive steps, assign your Bitbucket username to a terminal 
# variable. Substitute the {} characters and contents to match your own. (In my case, 
# the next line becomes username="0x666f6f".)
username="{your-bitbucket-username}"

# Add the SSH  key to your Bitbucket account using the Bitbucket REST API. 
#   (Alternatively, this can be done via the Bitbucket web interface.)
#   Optionally, change the name for your client/localhost workstation. 
#   (In my case, I named my client "d9-workstation".) 
curl -X POST -u "$username" -H "Content-Type: application/json" -d '{"key":"'"$key"'", "label":"d9-workstation"}' "https://api.bitbucket.org/2.0/users/${username}/ssh-keys"

Bitbucket

Create a new project/repository

Create a new repository. This can be done with the Bitbucket web interface, but I prefer the command-line version. :)

With Bitbucket, you have the option to create a public or private repository. In the code below, we’ve selected a private repository with the "is_private":"true" JSON parameter. Other than the “scm” tag, all other JSON attributes are optional.

user@d9-dvm:~$
1
2
3
4
5
# Assign the new repository name
repo="my_repo"

# Create the remote repository
curl -X POST -v --user "$username" -H "Content-Type: application/json" https://api.bitbucket.org/2.0/repositories/${username}/${repo} -d '{"scm":"git", "is_private":"true", "fork_policy":"no_public_forks", "language":"markdown"}'

Git commands

Connect local project directory to the repository

user@d9-dvm:~$
1
2
3
4
5
6
7
8
9
10
11
# Create and change to a project working directory on the workstation.
mkdir -p ~/Documents/my_project
cd ~/Documents/my_project

# Initialise the directory as a local Git repository.
#   This will create a '.git' sub-directory and associated Git metadata.
git init

# Connect the local repository to the remote repository.
#   This step re-uses the {username} and {repo} variables from earlier steps.
git remote add origin git@bitbucket.org:${username}/${repo}.git

Add a file

user@d9-dvm:~$
1
2
3
4
5
6
7
8
9
# Create a file, README.md to upload to the remote repository.
echo "# My project's README" >> README.md

# Add the file to a staging area, then commit the creation/edits.
git add README.md
git commit --message "Initial commit"  # Flag: --message | -m

# Push the file to the master branch in remote repository.
git push -u origin master

Add a directory (and contents)

user@d9-dvm:~$
1
2
3
# Take a snapshot of the contents of the PWD; and add them to a staging area.
git add .
git commit --all -m "Additional commit"  # Flag: --all | -a

Inspect changes

user@d9-dvm:~$
1
2
3
4
5
6
7
8
9
10
11
# First, modify some files
echo "This is a test project." >> README.md

# Inspect any unstaged changes
git diff

# Commit
git commit -am "Additional commit #2"

# Check status of the local repository
git status

Clone a remote repository to a local directory

user@d9-dvm:~$
1
git clone <repo>

Other Commands

Download a single file from a private repository

user@d9-dvm:~$
1
wget --user="0x666f6f" https://bitbucket.org/0x666f6f/linux/raw/master/my_script.sh

Convert local repository authentication from HTTPS to SSH

user@d9-dvm:~$
1
2
3
4
5
6
7
8
9
# Option one: edit with text editor:
cd ~/my_project
nano .git/config
#> Change the config from HTTPS to SSH.
#> url = ssh://{your-bitbucket-username}@bitbucket.org/{your-bitbucket-username}/{repo-name}.git

# Option two: edit with SED:
cd ~/my_project
sed -i -r 's/https/ssh/' .git/config

References