Dachang’s rough and efficient Git practical tutorial

Copyright statement: originality is not easy, plagiarism and reprinting are prohibited in this article, and infringement must be investigated!

1. Git installation and configuration

Git installation:
official website download:

https://git-scm.com/downloads

The official website is as follows:
insert image description here

Here is a demonstration on Windows system:
insert image description here

The following three steps are default, click next directly, and then wait for the installation to check whether the installation is successful or not:
insert image description here

Git configuration:
configure the username and email address, as follows:

git config --global user.name "用户名"

git config --global user.email "邮箱名"

insert image description here

After the configuration is complete, you can see a .gitconfig configuration file under C:\Users\lenovo, which records the configured user name and email address, as follows:
insert image description here

2. SSH configuration and connection

SSH configuration:
Git tools can establish connections with github, gitlab, and gitee, and there are roughly three ways to download projects from the repository (warehouse) to the local: HTTPS, SSH, and ZIP. The first two methods are often used during development, but HTTPS is used to establish Authorization verification is required when connecting, and user name and password need to be entered, which is cumbersome, so we can directly use SSH to connect. Here we use github as an example to

demonstrate. In order to perform key-free login between the local warehouse and the remote warehouse, you can configure ssh

1. Generate a key
The previously used key generation method:

ssh-keygen -t rsa -C  邮箱名

Now this method is no longer feasible, and an error will be reported, as follows:

ERROR: You‘re using an RSA key with SHA-1, which is no longer allowed.

Because github has upgraded the ssh key, the original SHA-1, rsa, etc. are no longer supported. Here we can use the ed25519 method to generate a new key, as follows:

ssh-keygen -t ed25519 -C your-email

At this time, two files will be generated in the C:\Users\lenovo.ssh directory, a public key and a private key:
insert image description here


2. Configure the key
Before configuring the key, we first change the default branch origin on github to In enterprise development, the main branch is named after master.

Under the [github]-[settings]-[Repository] path, just change origin to master, as follows:
insert image description here
Then change the public key id_ed25519.pub in the file Copy the content to github (github – settings – SSH and GPG keys – NEW SSH key), as follows:
insert image description here

insert image description here
Note : When copying in, try not to keep spaces

3. Test connectivity
Sentences to test connectivity (fixed writing):

ssh –T git@github.com

If the known_hosts file appears in the C:\Users\lenovo.ssh directory, it means that it can be connected:
insert image description here

3. Rough and efficient workflow

Inefficient way:

  • Build the project locally first
  • Then create a warehouse remotely
  • Finally, associate the local project with the remote warehouse
git remote add origin SSH

This operation will be cumbersome and inefficient, rude and

efficient way:
directly build a warehouse on github, as follows:
insert image description here

then copy the SSH link of the warehouse, as follows:
insert image description here

finally clone (clone) to the local, very simple and efficient, as follows:

git clone <git@github.com:MakerChen66/JavaDevelopment.git>


Non-destructive working method:

1. Go to the root directory of the project and
refer to the branch:

git branch

Create a branch:

git branch new_branch

Create and switch to the branch you just created:

git -b branch new_branch


2. After the project is modified,
see which files have been modified:

git status

Put the file in the staging area:

git add file
git add .  // .表示当前目录下的所有文件,慎用

Add comment content: the purpose of the submission or what changes have been made

git commit –m “注释内容”

Push to remote:

git push

The above is the general submission process. In general, there is no problem. For other special cases, please see the following

4. Other major issues

Special circumstances:
There will inevitably be other special problems in official work, which are roughly as follows:

1. If you are not the only person in charge of the branch of the current project, before you push to the remote warehouse, a colleague has already submitted it to the remote warehouse one step ahead of you. You need to pull (pull) the code submitted by your colleagues, and your code will be merged by default before you can push it to the remote, as follows:

git pull

2. Due to some reasons, you push wrongly, and you need to roll back to the specified version at this time, as follows:
Method 1:

git log // 查看提交记录
git reset -hard HEAD^  // ^表示回到上一个版本
git reset -hard HEAD~100  // 表示回退到前100个版本

insert image description here

Method 2:

git reflog  // 查看版本号
git reset -hard 版本号  //回退到指定的版本号,如下:
git reset -hard 4ff91ad

insert image description here

3. Suppose you created the test branch based on the master branch to test the function of the project. After a period of testing, the function can be used normally. You need to merge the code of the test branch into the master branch to run the official server. At this time, check out to the master first. Branch, perform the merge operation, as follows:

git merge test

insert image description here

Of course, you can also manually merge on the remote

5. Link to the original text

read the original text

6. Author Info

Author: Xiaohong's Fishing Daily, Goal: Make programming more interesting!

Original WeChat public account: " Xiaohong Xingkong Technology ", focusing on algorithms, web crawlers, website development, game development, data analysis, natural language processing, AI, etc., looking forward to your attention, let us grow and code together!

Copyright Note: This article prohibits plagiarism and reprinting, and infringement must be investigated!

Guess you like

Origin blog.csdn.net/qq_44000141/article/details/125352659