GitHub installation and use

In the world of programmers, GitHub is undoubtedly a very important platform, and how to upload your own code and share it with others is one of the skills that must be mastered. For beginners, uploading code may feel difficult. Therefore, this article will introduce how to upload code to GitHub.

If it is installed, find the installation location of Git on your computer

  • Mac platform: Enter which git in the command line, the installation location of git will be displayed;

  • Windows platform: Open cmd, enter where git and the installation path of git will be displayed.

Client installation

Download and install the client Git – Downloads from the official website, and keep next until it is completed.

The installation is complete and bin is configured to path.

Right-click on the desktop, right-click on the path to open bash, and .git will be created on that path.

1. Register a GitHub account

First, you need to register a GitHub account. If you already have an account, you can skip this step directly. When registering, you need to enter your username, email address, password and other information. After agreeing to the terms, you can click “Sign up for GitHub” to complete the registration.

Git environment configuration

  • 1. Register an account on GitHub or Gitee official website. After registration, right-click on the desktop and select Git Bash to configure the account. The command is as follows:

# Configure username (“username” is your own username)

git config --global user.name "username"

# Configure email (“[email protected]” is the email used when registering the account)

git config --global user.email "[email protected]"

After executing the above command, you can use git config –global –list to check whether the configuration is successful.

2. Execute: ssh-keygen -t rsa, (note that there are no spaces in ssh-keygen) to generate SSH (a secure connection for your computer to communicate with Gitee)

ssh-keygen-trsa

3. After execution, go to the users directory on the system disk (win: C:\Users\your username.ssh\) and view the generated ssh file

  • mac location /Users/may/.ssh

may is the user name, fill it in according to the actual situation

4. Add the public key (id_rsa.pub) to Github or GitLab platform, taking GitHub as an example here

2. Create a new warehouse

After registration is complete, click “New repository” in the “+” sign in the upper right corner of the GitHub page to create a new repository. In the new window that pops up, enter your repository name and add a brief description, select public or private, check “Initialize this repository with a README” and select “MIT License”, and finally click “Create repository” Complete creation.

3. Upload local code to GitHub

After creating the warehouse, the next step is to upload the local code to GitHub. First create a folder locally as a local Git repository, then use Git to add files and submit them. (Projects can be stored in the git folder first)

In the command line, enter the local warehouse folder and execute the following command to add code: (Fill in the instructions in the submission as needed)

1

2

3

git init # Initialize the local Git repository

git add . # Add all files to the Git repository

git commit -m “Initial commit” # Submit changes and add instructions

At this point, the local warehouse is ready, and you need to connect to the warehouse on GitHub. On the warehouse page, find the warehouse address, such as https://github.com/username/repository.git.

Warehouse address view

Execute the following command on the command line to push the local warehouse to GitHub:

git remote add origin https://github.com/username/repository.git #Associate the local warehouse with the warehouse on GitHub

git branch -M main # Rename the branch of the local warehouse to main

git push -u origin main # Push the local warehouse to GitHub. You need to add the “-u” parameter for the first execution.

git remote add origin https://github.com/username/repository.git
git branch -M main
git push -u origin main

Check whether the upload is successful: Open the git web page and check whether newly pushed files have been added to the warehouse

Common errors in the last step of push:

Case 1. If the following error is reported: it is because the remote warehouse and local warehouse files are not synchronized.

The solution is to first execute git pull to pull down the remote warehouse files to synchronize the remote and local files.

Solution steps:

1).Execution

git pull webbrowser master –allow-unrelated-histories

Replace webbrowser here with your remote warehouse name

2). Push again

Case 2. When pushing, an error message appears: When you git push to GitHub, an error message appears: remote origin already exists. Translated, it means error: remote origin already exists.

Reason: The local git has been configured with a remote warehouse address, so when git pushes to another warehouse address again, it will be prompted that the remote warehouse already exists.

solution:

First: View the information of the remote library: git remote -v
Second: Delete the existing remote repository: git remote rm origin
Third: Create a new remote warehouse address: git remote add origin + remote warehouse address

4. Update code

Method 1, Git Bash method

After modifying the code locally, you need to synchronize the code to GitHub. Just execute the following command:

1

2

3

git add . # Add modified files to Git repository

git commit -m “Update code” # Submit changes and add instructions

git push origin main # Push locally modified code to GitHub

In short, through the introduction of this article, we can understand the basic usage of GitHub, from registering an account to creating, uploading, and updating the warehouse. Of course, GitHub also has many other functions, such as collaborative development, branch management, etc., which need to be further explored. I hope it will be helpful to fellow programmers.

Method 2, Git GUI method

Install git-gui on mac version

brew install git-gui

If an error is reported during the installation process: curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused

Solution:

Modify hosts and add content

199.232.28.133 raw.githubusercontent.com

After entering the corresponding path (local warehouse path), execute git-gui

(windows) In the git path, right-click “Open Git GUI here”

Select the file to be modified and click “Stage Changes”, which will be loaded into the “Stage Changes” module in the lower left corner.

Add submission instructions, then click “Commit” and finally click “Push”

5. Time shuttle – version rollback

Version rollback is divided into two steps:

step:

① Check the version and determine the point in time you need to return to

Command: git log

Display the person who performed the operation and the time of operation

git log –pretty=oneline

This reflects the importance of adding reasonable comments when committing -m”comment content”.

②Rollback operation

instruction:

git reset --hard

commit number

Case: I want to take a time machine and go back to when I created the first file

Note: After going back to the past, if you want to go back to the latest version, you need to use the command to view the historical operations to get the latest commit id.

Command: git reflog

summary:

a. If you want to go back to the past, you must first get the commit id, and then roll back through git reset –hard;

b. If you want to go back to the future, you need to use git reflog to view historical operations and get the latest commit id;

c. When writing the rollback command, you don’t need to write the full commit id, git will recognize it automatically, but you can’t write too few, at least the first 4 characters are required;

6. GUI tool client

There are many types of clients: Fork (free), GitHub Desktop (free), Sourcetree (free), Tower (paid), GitKraken (free/paid), Sublime Merge (free in the early stage), etc.

There is a useful one for windows: TortoiseGit, download and install

Install GitHub Deshtop

For mac/windows version, install GitHub Desktop, download from the official website: GitHub Desktop

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Git skill treeGetting started with GitGit installation 6747 people are learning the system