Git is an important tool for maintaining your code and cooperating with others. It is the default source code version control tool for github. To use github, you need to learn how to use git.

Github connection

Here, I will discuss how to set up git connection to Github server. Obviously, you need to get a github account first. This process will not be described here. What’s more, if you have a linux server, eg a cloud server, you can even host your own git sever. Here, to contribute to open source project, I will upload part of my codes to github, if these codes are proper to be shared with the general public.

Setup local git configuration

Check git installation

You can test whether your linux system has installed git with the command

1
git

If there are information about git, your are luck to already have git on your local machine, or you will need to install git to your local linux machine. It should be a easy task, I will not discuss it currently.

Switch between SSH and HTTPs

You need to choose a connection method from local linux to github. There are two options: SSH and HTTPs. Follow this link [3] to make this change.

Connecting to GitHub with SSH

To be able to commit local change to github, you first need to establish a remote connection with your github account. There are two approaches to do this, SSH and HTTPS. I will mainly talk about the SSH method on linux system. There is a very precise introduction on how to connect to github with SSH [1]. The above introduction is a brief version of that one.

Setup local SSH key

You can setup the local SSH key with these commands.

1
ssh-keygen -t rsa -b 4096 -C "name@mail.com"

Copy SSH key on github

Follow the instructioin on [1], you can copy the key content in the file rsa.pub to github. Note, just copy ALL the content of this file. By doing so, your local linux IP will be confirmed as safe ssh connection by github server. Thus, you will be able to connect github git server from your local linux system.

Test connection

After successfully testing the connection with the below command [1], you should be able commit local files to github.

1
ssh -T git@github.com

Git commands

There are several important commands you should remember to clone remote codes to local machine or to commit local changes to github [2].

Clone github repository to local

1
2
3
4
# using HTTPS method
git clone https://github.com/user/repository1.git
# using git method
git clone ssh://git@github.com/user/repository1.git

Push local change to github

1
2
3
4
5
6
# Add new file to local git log
git add .
# commit the new file
git commit -m "Add new file"
# push the local file to github
git push

After these operations you should see the new file has been uploaded.

Reference

[1] https://help.github.com/en/articles/connecting-to-github-with-ssh

[2] https://stackoverflow.com/questions/10364429/how-to-commit-to-remote-git-repository

[3] https://help.github.com/en/articles/changing-a-remotes-url