Introduction#
Suitable for systems: Linux and macOS
Reading this article requires some knowledge of Linux and a little understanding of Linux commands.
What is GitHub?#
To understand what GitHub is, you first need to understand what Git is:
Git (pronounced as /gɪt/) is an open-source distributed version control system that can efficiently and quickly handle version management of projects ranging from small to very large. The biggest difference compared to centralized systems is that developers can commit to their local repositories. Each developer can clone (git clone) a complete Git repository on their local machine.
GitHub:
GitHub is a hosting platform for open-source and private software projects. It is named GitHub because it only supports git as the unique version control format for hosting.
In simple terms, it allows you to upload the code projects you write at any time on any device, and you can download them on any device to achieve multi-device synchronization.
Main Content#
Create your own git project repository#
After registering on GitHub, go to your personal account page and click the New button to create a Repository.
Name it something you can understand, preferably similar to your local project folder. You will need it later. For example, I created the content of my own R language learning here.
Local deployment of git#
The installation is very simple, just need to understand some commands.
$ sudo apt install git-all #for Debian-based systems
$ git --version #for macOS, run this command after installing Xcode Command Line Tools. The installation of Xcode Command Line Tools is also very simple, please find it yourself.
For installation on other systems, please refer to the git official website: Getting Started with Git
Associate your machine with GitHub#
I believe friends who have played with servers should know what ssh is. Since git completes file upload and download through ssh, you need to fill in an ssh key file to GitHub to authenticate your machine.
$ cd .ssh
If prompted "No such file or directory", it means that no ssh key has been generated on this machine yet. Execute the following command:
$ ssh-keygen -t rsa -C "[email protected]"
When this step is completed, a rectangular ASCII art will appear, indicating that the generated ssh key has been saved in the .ssh folder.
Then open the key file and copy all its contents:
$ cat ~/.ssh/id_rsa.pub
#or if you know how to use vim
$ vim ~/.ssh/id_rsa.pub
Of course, you can also directly open it with a text editor. The .ssh hidden folder is located in your user folder. On macOS, the shortcut to show hidden files is Ctrl+Shift+.
Go back to your GitHub user page, click the top right corner, go to settings, and click SSH and GPG keys on the left side.
Click newsshkeys in the top right corner, give it a title, and paste the contents of the copied key file into the box below to save it. This completes the GitHub authentication.
After completion, use the following command in the terminal to confirm whether the SSH key is successful:
$ ssh -T [email protected]
#If the following content appears at the end, it means success:
Hi your_username! You've successfully authenticated, but GitHub does not provide shell access.
Configure and upload the project folder to git#
In the terminal, use cd to enter the project folder you want to synchronize, and run the following commands:
$ git init #initialize, after running this step, your terminal will display git:(master) ✗
$ git add . #The official guide says to add README.md, but that's just an example. It only uploads the README.md file. The significance here is to synchronize all the contents of this folder.
$ git commit -m "first commit" #The content in quotes is the reason for your upload update
$ git branch -M main
$ git remote add origin https://github.com/your_username/your_repository_name.git
#If you forget the repository name, go to your repository page in the browser, copy the URL and add .git at the end
$ git push -u origin main #push to upload
Done! Now go back to the repository page, and you will see the contents of the folder inside!
In the future, just use cd to go to the project folder and execute the following commands to upload updates.
$ git add .
$ git commit -m "reason for update"
git push -u origin main
To synchronize the code from the cloud to your local machine, use the command:
$ git pull --rebase origin master
Now you can happily upload all your projects to GitHub!
Some content in this article is referenced from 3y's article: https://github.com/ZhongFuCheng3y/3y with some modifications and improvements.