Git Cheetsheet
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.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.1
2git 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.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.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
1 |
|
Add a file
1 |
|
Add a directory (and contents)
1 |
|
Inspect changes
1 |
|
Clone a remote repository to a local directory
1 |
|
Other Commands
Download a single file from a private repository
1 |
|
Convert local repository authentication from HTTPS to SSH
1 |
|