[Git] When Github uploads files to the remote warehouse, network errors often occur, a relatively stable connection method and my example

1. Introduction to the problem

When using git to transfer project files to remote warehouses, network connection errors often occur, such as:

fatal: unable to access 'https://github.com/biluko/RegionCLIP.git/': Failed to connect to github.com port 443 after 21106 ms: Timed out

Another example:

error: failed to push some refs to 'https://github.com/biluko/RegionCLIP.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Another example:

fatal: unable to access 'https://github.com/biluko/RegionCLIP.git/': OpenSSL SSL_read: Connection was reset, errno 10054

There are many similar situations like this, and I feel disturbed every time. I tried to upload five times this afternoon, but all failed due to network problems. I wanted to find a way to upload stably to solve this problem.

The following is my smoothest upload process:

Two, a complete process

2.1 Initialization

Open a command line terminal or Git Bash, change to the directory you want to initialize as a Git repository, and run the following command:

git init

Git will create a subdirectory called ".git" in the current directory and set it up as the Git repository. At this point, you can start using Git for version control.

If all goes well, you should see output similar to the following:

Initialized empty Git repository in /path/to/your/repository/.git/

insert image description here
At this point, let's take a look at the corresponding folder:

insert image description here

2.2 Pull the latest changes from the remote warehouse and merge them into the current branch

The git pull command is used to pull the latest changes from the remote repository and merge them into the current branch. You can pull changes from the remote repository https://github.com/biluko/RegionCLIP.git to your local branch with the following command:

git pull https://github.com/biluko/RegionCLIP.git

Please make sure you have initialized a Git repository (can be initialized by git init) before executing this command. Also, make sure you have the appropriate access rights to access the remote repository.

After executing the git pull command, Git will try to automatically merge the latest changes from the remote repository into your current branch. If there are conflicts, you need to manually resolve the conflicts and commit. After the merge is complete, your local branch will contain the latest changes from the remote repository.

Note that if you have previously associated with the remote repository (using the git remote add command), you can directly use the alias of the remote repository instead of the URL:

git pull origin

Replace origin with the remote alias you are using.

But when I use this command, an error will be reported, as follows:

insert image description here

2.3 SSH URL of the remote warehouse

Using the above HTTP protocol, network instability will occur, so I improve the method and try to use SSH URL:

git pull git@github.com:biluko/RegionCLIP.git

insert image description here
git pull [email protected]:biluko/RegionCLIP.git is a command to pull updates from a remote warehouse using the SSH protocol. This command will try to pull the latest changes from the remote warehouse corresponding to the SSH URL [email protected]:biluko/RegionCLIP.git and merge them into your current branch.

Make sure you have properly configured SSH keys and added them to your GitHub account.

When executing this command, Git will communicate using the SSH protocol, using the SSH key you configured for authentication. If everything is fine, Git will pull the latest changes from the remote repository and try to merge them into your current branch.

Please make sure that you have executed the git init command in the local Git warehouse directory to initialize and set the correct remote warehouse address before executing this command.

If you encounter any errors, make sure your SSH keys are configured correctly and you have access to the remote repository.

Remarks:

When executing the git pull [email protected]:biluko/RegionCLIP.git command, you used the SSH URL of the remote repository. However, the syntax of this command is incomplete, missing the alias (remote) and branch name of the remote warehouse.

The correct syntax should be:

git pull <remote> <branch>

You need to replace with the alias of the remote repository and with the name of the branch you want to pull. If you haven't added an alias to the remote repository, you can use the git remote add command to add a remote repository:

git remote add origin git@github.com:biluko/RegionCLIP.git

This will set the alias of the remote repository to origin.

Then, you can use the following command to pull the latest changes of the specified branch of the remote repository:

git pull origin <branch>

Make sure to <branch>replace with the name of the branch you want to pull.

Note that you need to initialize locally (using the git init command) and configure the correct SSH keys to ensure you have access to the remote repository.

2.4 Add to the staging area

The git add . command is used to add all changes in the current directory (including newly added files and modified files) to Git's temporary storage area for subsequent submissions.

After executing the git add . command, Git will scan all files in the current directory and its subdirectories, and add them to the temporary storage area. This means that Git will track changes to these files and include them in your commit when you perform a commit operation.

Note that the git add . command adds all files in the current directory and its subdirectories to the staging area, including untracked files. If you want to add only modified files to the staging area, not newly added or untracked files, you can use the git add -u command.

After executing git add . or git add -u, you can use the git status command to view the status of the temporary storage area and confirm whether the added files are correct.

Remember, you may also need to execute the git commit command to submit the changes in the temporary storage area to the local warehouse before executing the commit.

git add .

insert image description here

The above are only some of the results shown!

2.5 Submit operation

git commit -m "RegionCLIP"

git commit -m "RegionCLIP" is the command to submit the operation, it will submit the changes in your temporary storage area to the local warehouse, and add a descriptive submission information to the submission.

When executing this command, please make sure you have executed the git add command to add the files to be submitted to the temporary storage area.

To explain the various parts of the command:

git commit: Indicates the commit operation.
-m "RegionCLIP": Use the contents of the double quotes following the -m parameter as the commit description. You can replace "RegionCLIP" with more specific submission information to describe the content of this submission.
After executing the git commit -m "RegionCLIP" command, Git will submit the changes in the temporary storage area to the local warehouse and assign the specified submission information to the submission.

Note that this will only save the commits in your local repository and will not sync to the remote repository. If you want to push your local commits to the remote warehouse, please execute the git push command.

By performing a commit operation, you can record the changes to the staged files in the version history, and provide description information about this change. This is great for tracking project development and collaborating with team members.

insert image description here

2.6 Add a remote warehouse as a remote alias of the Git warehouse

git remote add origin git@github.com:biluko/RegionCLIP.git

insert image description here

git remote add origin [email protected]:biluko/RegionCLIP.git is to add a remote warehouse as a remote alias (remote alias) of the Git warehouse. Use this command to associate a remote alias named origin with the URL [email protected]:biluko/RegionCLIP.git of the remote repository.

To explain the various parts of the command:

git remote add: Indicates the operation of adding a remote alias.
origin: It is an alias you gave to the remote warehouse, you can choose a meaningful name according to your needs. Normally, we use origin as the default remote warehouse alias.
[email protected]:biluko/RegionCLIP.git: is the SSH URL of the remote warehouse.
By executing the git remote add origin [email protected]:biluko/RegionCLIP.git command, you will add a remote alias named origin to your local Git repository, which is associated with the URL of the specified remote repository.

After adding the remote alias, you can use the origin alias to refer to the remote warehouse, which is convenient for interactive operations with the remote warehouse such as push and pull. For example, you can use to git push origin <branch>push your local branch to the origin remote repository.

Make sure you have initialized a Git repository (use git init to initialize) and configured the correct SSH keys before executing this command to ensure you have access to the remote repository.

2.7 Push to remote warehouse

git push origin master

insert image description here
git push origin master is the command to push the local master branch to the remote repository named origin.

To explain the various parts of the command:

git push: Indicates the push operation.
origin: It is an alias you give to the remote warehouse, which is used to refer to the URL of the remote warehouse. When executing the git remote add command, we usually set the alias of the remote warehouse to origin.
master: Indicates the name of the local branch to push. In this case, you will push the local master branch to the remote repository.
By executing the git push origin master command, Git will push the commits on the local master branch to the remote warehouse named origin.

Please note that if you have not created a master branch in the remote warehouse, Git will create a new master branch and push the commits on the local master branch to the remote warehouse.

Make sure you have added the remote repository's URL as a remote alias (using the git remote add command) before executing this command, and have sufficient permissions to push to that remote repository.

Before performing the push operation, it is recommended to perform git commit to submit the local changes to the local warehouse. This ensures that the latest changes are being pushed.

2.8 Final Results

insert image description here

3. Understanding of HTTP and SSH

3.1 The difference between the two

GitHub's HTTP and SSH are two different protocols for communicating with remote repositories and for pushing and pulling code. The main differences between them are as follows:

3.1.1 Authentication method

HTTP uses username and password based authentication, while SSH uses public and private key pairs for authentication.

  • HTTP: When using the HTTP protocol, you need to provide your GitHub username and password for authentication. This means that you will need to enter your password every time you communicate with the remote repository.
  • SSH: When using the SSH protocol, you need to generate a pair of SSH keys (public and private). Add the public key to your GitHub account, while the private key will be kept locally. Use the private key for authentication every time you communicate with the remote repository. This method is more secure because there is no need to enter a password for each communication.

3.1.2 Transmission protocol

HTTP uses the HTTPS protocol for data transfer, and SSH uses the SSH protocol for data transfer.

  • HTTP: Use HTTPS for data transmission, and ensure communication security through encryption and authentication.
  • SSH: Use the SSH protocol for data transmission, which also has encryption and authentication functions, and ensures communication security through the use of public and private keys.

3.1.3 Warehouse clone method

When cloning a remote repository, you can choose to use HTTP or SSH.

  • HTTP: When cloning using HTTP, you can use the HTTPS URL of the repository.
git clone https://github.com/username/repository.git
  • SSH: When cloning using SSH, you can use the SSH URL of the repository.
git clone git@github.com:username/repository.git

Overall, the SSH protocol is relatively more secure because it uses public and private keys for authentication and data transmission is also encrypted. The HTTP protocol is simpler and suitable for some scenarios that do not require high security. Which protocol you choose to use depends on your preference and specific needs.

3.2 Why is the SSH network more stable than HTTP?

HTTP may encounter various network errors during network communication, and the reasons why SSH is relatively more stable can be attributed to the following points:

  1. Transport protocol differences: HTTP uses a text-based protocol, while SSH uses a binary protocol. Compared with the text protocol, the binary protocol has higher transmission efficiency and more reliable data transmission.
  2. Long connection and short connection: HTTP is a protocol based on short connection. Each request needs to establish a connection and close it. Such frequent connection establishment and closing operations increase the overhead and delay of network transmission. However, SSH establishes a long connection, which reduces the number of connection establishment and closure, and improves the efficiency of data transmission.
  3. Encryption and authentication mechanism: SSH uses public and private key pairs for identity authentication and data encryption. This mechanism provides higher security and data integrity, and effectively prevents man-in-the-middle attacks and data tampering. The authentication mechanism of HTTP is relatively simple, only relying on user name and password, and the security is weak.
  4. Port restrictions: Some network environments may have restrictions on specific network ports, and SSH uses port 22 by default, which is less restricted. The default port used by HTTP is 80 or 443, which may be restricted or blocked depending on the network environment and firewall settings.

To sum up, SSH has higher stability and security than HTTP, especially in environments with unstable networks, high security requirements or more restrictions, SSH communication can be used for more reliable remote operations and data transfer.

Guess you like

Origin blog.csdn.net/wzk4869/article/details/131626483