Git download and use official website download is too slow

Git

1. What is git

Git is a free and open source version control tool.

2. What is version control

Version control is a system that records changes in the content of one or several files for future review of specific version revisions.

System specific functions

  • Record all historical changes in the file.
  • Can be restored to any historical state (version) at any time
  • Multi-person collaborative development
  • Error recovery

Version control tools

  • SVN centralized
  • Git distributed

3. Download and install

download:

Official website download https://git-scm.com/

Domestic faster address https://npm.taobao.org/mirrors/git-for-windows/

installation

Double click, next step

Check whether the installation is successful

1. Right-click anywhere on the desktop or folder, and both git bash here and git gui here appear to indicate success.

2. Open CMD and enter git --version. The version number appears to indicate success.

Insert picture description here

Global configuration

Use the following command to view the global configuration

git config --list

If the user name and email address have not been initialized, you need to set the user name and email address.

git config --global user.name "你的用户名"
git config --global user.email "你的邮箱"

The email address is best to be real and valid, but it is best to be a registered email address of GitHub or gitee.

4. Basic operation

4.1 Initialize the warehouse

Git will not automatically manage your folders. If we need to perform version management on the folders, we need to initialize the current folder to a git repository. After initializing the warehouse, this folder and all its descendant folders and sub-files will be managed by git.

  1. Open the command line in the folder that needs git management.
  2. Type git init
  3. A hidden folder of .git will appear in the folder. At this point, your folder will be managed by git.
    Insert picture description here

Git will divide the managed files into two areas and four states.

Insert picture description here

4.2 Add the contents of the workspace to the staging area

[Note] The workspace needs to have content, and empty folders will not be added.

  1. Open the command line in the current directory
  2. Input command
    1. git add file name
    2. git add folder name
    3. git add --all
      1. It means to add all the content in the current work area that has not been added to the temporary storage area to the temporary storage area.
      2. Shorthand: git add.
        Insert picture description here

4.3 Check the status of the work area and staging area

  1. Open the command line in the current directory
  2. Enter the command git status. Green indicates that the file has been added to the temporary storage area, and red indicates that it has not been added to the temporary storage area.

4.4 Withdraw content in the temporary storage area

  • Open the command line in the current directory
  • Input command
    • git reset HEAD-file name
    • git reset HEAD-folder name
    • git reset HEAD-. (Recall all the contents of the temporary storage area)
      Insert picture description here

4.5 Forming a historical version

Form a historical version of the content in the temporary storage area. [Note] There is still content in the temporary storage area. ,

  • Open the command line in the current directory.
  • Enter the command git commit -m "Description of the content of this submission."

Insert picture description here

4.6 View historical version

  • In the current directory, open the command line
  • Enter the command git log
  • The historical version in the local file will be displayed in reverse order.
    Insert picture description here
// 历史版本ID
commit 2073eca472d19ea1cbb88065550c88e1a88c0b65 (HEAD -> master)
//设置的用户名和邮箱
Author: 2047 <244389185@qq.com>
//提交的日期
Date:   Tue Mar 9 10:07:10 2021 +0800
//说明
    git第一次提交

4.7 Roll back historical version

Roll back to a previously recorded historical version

  • In the current directory, open the command line
  • Enter the command git reset --hard version id. Enter.

[Note] After rolling back, try not to modify the content and submit it.

4.8 Delete historical version

When deleting a historical version, the id of the previous version of the deleted version is used, and after deletion, all historical versions after this version will be deleted.

  • In the current directory, open the command line
  • Enter the command git rebase -i historical version id
  • It will enter the editing state, press the i key on the keyboard, move the cursor to the upper left corner to change pick to drop.
  • Press the esc key to exit the editing state
  • Input: wq means save, press enter.

5.GitHub

GitHub is the world's largest social programming and code hosting website.

GitHub can host various git repositories and provide a web interface.

Because it only supports git as the only repository format for hosting. So it's called GitHub.

There is another website with similar functions: gitee (code cloud) is a domestic website

6, git related files

readme.md documentation

Describe the contents of your warehouse.

The content format is md format. When transferring the warehouse to a remote location, the readme file will be automatically displayed under the warehouse.

.gitignore ignore files.

You can ignore some documents that do not need to be submitted.

The location of the file must be at the same level as the .git directory,

.gitkeep placeholder file

git will not manage your empty folders. If you need the current directory, you have two options

  1. In the empty folder, create a new empty file that is useless,
  2. In the empty folder, create a new .gitkeep file

7. Upload the local git repository to the remote

The git local warehouse can be uploaded to the remote warehouse. For example, GitHub or gitee

When uploading, all files in the directory where .git is located are uploaded to the remote. Instead of uploading to the upper-level folder of .git.

1. Build a remote warehouse

Log in to the GitHub page. If you do not have an account, you must first register one.

Click the +number next to the account and select new repository to create a new repository.

2. Confirm that the local warehouse has a historical version that has not been uploaded.

3. Add a remote warehouse address in the local

git remote add origin https://gitee.com/envyTheCloud/ym001.git
remote 远程
add 添加
origin 变量名,可以自由更改,值为后面写的远程仓库的地址。
	我们以后需要使用远程仓库地址时,就可以直接使用origin。

4. Push the local historical version to the remote

  • Open the command line in the current directory.
  • Enter the command git push -u origin master
    • push
    • -u Record which address and branch you pushed to this time. If you modify it next time and push it again, you don't need to write the origin master behind. Just write git push directly.
    • origin represents the address of the remote warehouse.
    • master branch

8. Pull the remote warehouse to the local

first

  1. Open the warehouse details page on gitee and copy the remote address of the warehouse.
  2. Find a folder locally
  3. Open the command line and enter the command git clone remote warehouse address

Not the first time

  1. In the current directory, open the command line
  2. git pull gets the latest version of the remote warehouse.

Conflict resolution

When the remote warehouse has a new push, if you do not update locally, when you push the new version, the two versions operate on a file at the same time, then there will be a conflict.

Steps to resolve the conflict:

  1. First git pull to get the latest version from the remote warehouse
  2. You can resolve conflicts in vscode and select the code to use.
  3. add commit Submit to generate a new version
  4. Push the new version git push to the remote warehouse.

9, branch operation

9.1 New branch

  1. Open the command line in the current directory
  2. Enter the command git branch branch name

9.2 View all branches

  1. Open the command line in the current directory
  2. Enter the command git branch -a

9.3 Switch branches

  1. Open the command line in the current directory
  2. Enter the command git checkout branch name

9.4 Delete branch

  1. Open the command line in the current directory
  2. Enter the command git branch -d branch name

9.5 Merging branches

When the development work of the branch is completed, the branch and the master need to be merged.

  1. Open the command line in the current directory
  2. Enter the master branch, git checkout master.
  3. git merge branch name. Merge branch with master
  4. Push the merged version to the remote warehouse.

9.6 Push branch to remote

When we create other branches locally, they will not be uploaded to the remote warehouse by default. By default, git uploads to the master branch.

You can specify the upload address and branch when pushing.

jgit push -u origin 分支名

9.7 Pull the remote branch to the local branch

git checkout -b dev(本地分支名称) origin/dev(远程分支名称)

9.8 Delete remote branch

The first way:

​ Open the command line, enter

git push origin --delete 分支名

The second: delete directly in the gitee page.

9.9 Branch naming convention

1.主分支 ---master
	常见git仓库时会自动生成。一般来说,不要直接在master上面写代码,它只接受dev分支的合并。
master代码在实际项目中一般与线上的正式环境中的代码保持一致。

2.主开发分支  ---dev
	初始化项目之后,从master分支上开辟的新分支。一般也不直接在上面写代码,而是从各个功能分支上合并代码。
	dev分支接受各个功能分支的代码后,进行测试,测试完成后合并到master分支上,在将master分支上的代码发布到正式环境。也就是我们常说的项目上线。
	
3.功能分支 ---feature-XXX
	是从dev分支上开辟的新分支。一般研发工作是在该分支上完成的。研发完成后,将其合并到dev分支上。假如开发购物车模块,分支名:feature-shopCar。
	
4.功能bug解决分支 ---feature-XXX-fix-XXX
	第二个XXX可以是bug的编号。 是从功能分支上开辟的新分支。bug修复后,再将其合并到功能分支上。
5.紧急bug解决分支   --- hot-fix-XXX
	直接从master分支上开辟的。进行紧急的bug修复。修复完毕以后,合并到mster分支上。

10. Solve gitee upload problem

One: There is no warehouse locally, and a warehouse is created remotely.

git clone 远程地址。

Two types: There is already a warehouse locally, and a warehouse is created remotely.

After adding the remote warehouse address, push the local version to the gitee remote warehouse

git pull origin master --allow-unrelated-histories

Enter editing state

1. Press the keyboard letter i to enter the insert mode

2. Modify the yellow merge information on the top line, you don't need to modify it

3. Press "Esc" in the upper left corner of the keyboard

4. Enter ":wq", pay attention to the colon+wq, and press Enter

11. Use keys to pull remote warehouses

1. Open the command line and enter the command

ssh-keygen -t rsa -C "邮箱地址"

[Note] The email address is the email address in the git configuration.

After the input is complete, press three enters until a new command line appears. At this time, in /c/Users/Administrator/.ssh/id_rs

Two files are generated under the directory.

2. Open the file with the pub suffix. Copy content

3. Enter the settings, find the ssh public key, and generate a new public key.

4. Under the folder that needs to be obtained, open the command line and enter

git clone ssh地址。

Guess you like

Origin blog.csdn.net/weixin_53125457/article/details/114574581