Thanks to Joseph Brown for putting this together
You only have to do this once and you only need to do this if, when you go to the command line and type git you get
bash: git: command not found
If you do not have git (needed for both BitBucket and GutHub) installed, you can get it from here.
If this is your first time using git, you will need to configure it. In a Terminal, type:
git config --global user.name "first last"
git config --global user.email firstname.lastname@example.org
Set Up A Public SSH Key
You only have to do this once as well and if you have done it for another task you don’t need to do it again for git. This is to make it so you don’t have to type passwords. Pushing code is to Bitbucket/GitHub is very easy and with this setup, you don’t need to type passwords, it will just accept things from your computer. To do this, we will create and upload a public ssh key.
You can see if you already have one by looking at what you have in the directory ~/.ssh/. If you have the files id_rsa id_rsa.pub then you don’t need to do this step. You can just go to setup bitbucket or github below.
If you don’t have those files and you need to do this step, in the terminal type
ssh-keygen -t rsa -C "email@example.com"
Use the email associated with your Bitbucket/Github account. It will ask you for a passphrase. Just hit Enter (for empty passphrase). Your public key will be saved in ~/.ssh/id_rsa.pub.
Go to bitbucket.org and sign up if need be. Make sure to use an academic email address, as this entails free unlimited space.
Select “Manage Account” from your user menu. Then, select “SSH keys”, and click on “Add key”. It is important that the key is formatted exactly (e.g. does not contain spaces), so we need a dependable way to copy/paste. We’ll use the clipboard.
sudo apt-get install xclip
xclip -sel clip
In the Key window, paste in the key and click “Add key”. You can now test this in Terminal
ssh -T firstname.lastname@example.org
You only need to do the steps above once. Any new repositories will use this key.
Setting up a GitHub account is similar to Bitbucket.
Creating a repository
You can always setup repositories without linking them to bitbucket or github. Those sites just manage ways for collaboration and code communication. If you setup a new repository on those sites, they will often give you instructions for how to start them on your machine or if you already have one and want to link it online, it will also give you instructions for that.
The basic instructions are as such. In bitbucket, click on “Repositories” at the top, and select “Create repository”. Configure name, description, private/public, language, etc. Use Git as Repository type.
Done on the web. Switch over to a Terminal. cd into the folder containing your code. Type:
git remote add origin ssh://email@example.com/YOUR_USER_NAME/YOUR_REPO_NAME.git
1. Add files to be pushed:
git add .
“.” adds everything that differs from the repository. To restrict it to file “foo.txt”:
git add foo.txt
2. Commit files:
git commit -m "some message about the commit"
3. Push already:
git push origin master
There are, of course, a lot of other commands, but these are the ones you will use often. There are tons of git cheatsheets online.
Sometimes when cloning a repository, git is configured to use https rather than ssh URLs. This means that it will not use your public ssh key, and so you will need to type in your password every time you push. To remedy this, go to the .git directory in your code folder “foo”:
You will see something like:
url = https://YOUR_USER_NAME@bitbucket.org/YOUR_USER_NAME/YOUR_REPO_NAME.git
Edit this to:
# url = https://YOUR_USER_NAME@bitbucket.org/YOUR_USER_NAME/YOUR_REPO_NAME.git
url = firstname.lastname@example.org:YOUR_USER_NAME/YOUR_REPO_NAME.git
Pushing will now not require a password.