Guide to commonly used features on Git and Github#
How to create a remote repository on github?#
git init
#Adds the files in the local repository and stages them for commit. To unstage a file, use 'git reset HEAD YOUR-FILE'.
git add <files and directories>
# Commits the tracked changes and prepares them to be pushed to a remote repository. To remove this commit and modify the file, use 'git reset --soft HEAD~1' and commit and add the file again.
git commit -m "First commit"
# origin is a nickname for the URL. Https will prompt for username and password always. SSH uses keys for authentication instead.
# git remote add origin <URL, either HTTPS or SSH>
git remote add origin git@github.com:macadology/cookie-cutter-compbio.git
# Verify the new remote URL
git remote -v
# Pushes the changes in your local repository up to the remote repository you specified as the origin
git push origin master
# git push
Generating a new SSH key and adding it to the ssh-agent#
Usually, to enable ssh-key login to a server, first create a key pair (public + private). Store the public key in the server by copying the key to ~/.ssh/authorized_key
. Next, ssh from the client using the private key. Typically, you will need to run ssh
with the -i
option to point to the private key file. Alternatively, add the file to ssh-agent to avoid having to type -i
everytime.
Below are instructions on how to set up ssh login to github.
- First, create a key.
# Create an ssh keygen. For usability, leave the passphrase blank
ssh-keygen -t rsa -b 4096 -C "<your_email@example.com>"
# Start ssh-agent
eval "$(ssh-agent -s)"
# Add key to ssh-agent
ssh-add ~/.ssh/id_rsa
# The key can be moved to a suitable directory
## ssh using the generated key
ssh -i ~/.ssh/id_rsa <user>@<hostname>
- Add lines to
~/.ssh/config
.
Host *.github.com
AddKeysToAgent yes
IdentityFile ~/.ssh/id_rsa
- Add a new SSH key to your GitHub account
- Test ssh conection.
- Verify that the resulting message contains your username. If you receive a "permission denied" message
# Attempts to ssh to GitHub
ssh -T git@github.com
- Verify that the resulting message contains your username. If you receive a "permission denied" message, check if keys has been added to the agent.
# start the ssh-agent in the background
eval "$(ssh-agent -s)"
# check if key has been added to agent
ssh-add -l
- (Optional) Add the key to ~/.ssh/config, which lets us access github with just the key automatically.
host github.com
HostName github.com
IdentityFile <Directory of keys>/git_id_rsa
User git