Git installation and use

purpose

Manage github hosting project code through git;

Download and install

  1. Download from the official website: https://git-scm.com/downloads
    Insert picture description here
  2. Double click to install
    Insert picture description here
  3. Select the working directory for installation
    Insert picture description here
  4. Select components
    Insert picture description here
  5. Start menu directory name setting
    Insert picture description here
  6. Insert picture description here
  7. Choose to use the command line environment
    Insert picture description here
  8. I gave up, I chose next all;
  9. Check whether the installation is successful: right-click on the desktop, if there is an option for two git words, it is successful;
    Insert picture description here

Configure Git

Set global variables

Open git bush here with the right mouse button on the desktop:
Insert picture description here
enter:

$ git config --global user.name "your name" 
$ git config --global user.email "[email protected]"

Log in to GitHub, create an SSHkey

Then enter this to create an SSHkey:

$ ssh-keygen -t rsa -C "[email protected]"

Then click the Enter key three times in a row;
Insert picture description here
a .ssh folder will be generated under the local user folder, which contains two files id_rsa and id_rsa.pub:
Insert picture description here
Then open the folder:
Insert picture description here
Then open id_rsa.pub with Notepad, select all, Copy:
Insert picture description here
Then we go back to the GitHub account we just registered, click Settings:
Insert picture description here
Then click SSH:
Insert picture description here
Then click NEW SSHkey:
Insert picture description here
Then paste the key copied in the previous step and add it to the GitHub account settings:
Insert picture description here
Then go back to git, enter:

$ ssh -T git@github.com

Finally, enter yes:
Insert picture description here
OK, this completes the local and GitHub communication configuration.

Git basic workflow

Git work area
Insert picture description here
to add files to the warehouse process
Insert picture description here

Git initialization and warehouse creation and operation

Basic information settings

# 1.设置用户名
$ git config --global user.name '用户名'

# 2. 设置用户名邮箱
$ git config --global user.email '邮箱'

This setting shows who submitted the file on the github repository homepage;

Initialize a new Git repository

  1. Create a folder;
$ mkdir 文件名
  1. Initialize git in the file (create a git repository);
$ cd 文件名
$ git init 

(If you can't see .git, set the computer to display hidden files)

Add files to the warehouse

# 创建 a1.php 文件 
$ touch a1.php

$ git status

# 添加到暂存区
$ git add a1.php

$ git status

# 将文件从暂存区提交到仓库
$ git commit -m 'add a1.php'

$ git status

Modify warehouse files

$ ls

$ git status

$ vi a1.php

# 修改文件
$ cat a1.php
1111

$ git status

# 添加到暂存区
$ git add a1.php

$ git status

$ git commit -m '第一次通过git修改文件并提交到仓库'

$ git status

Delete warehouse files

# 删除文件
$ rm -rf a1.php

# 从Git中删除文件
$ git rm a1.php

# 提交操作
$ git commit -m '第一次通过git删除仓库文件'

$ git status

Git management remote warehouse

Purpose of using remote warehouse

Function: backup, realize centralized management of code sharing;

$ git status

# 创建文件
$ touch a2.php

$ git status

# 添加到暂存区
$ git add a2.php

$ git status

# 将文件从暂存区提交到本地仓库
$ git commit -m 'add a2.php'

$ git status

# 将本地仓库提交到远程仓库
$ git push

Git clone operation

  • Purpose : Copy the remote warehouse (the project corresponding to github) to the local;
  • Code :
$ git clone 仓库地址

Origin of warehouse address:
Insert picture description here

Sync local warehouse to git remote warehouse

$ git push

If it displays The requested URL returned error: 403 Forbidden while accessing, it means there is no permission;

Generally, this situation is a private project, no permission, enter the user name and password, or the remote address uses this type:

$ vi .git/config

# 将 
# [remote "origin"] 
#	url = https://github.com/用户名/仓库名.git 
# 修改为:
# [remote "origin"]
#	url = https://用户名:密码@github.com/用户名/仓库名.git

Below I use the project in VSCode as an example:

First of all, create a new warehouse on github with the same name as the local project:
Insert picture description here
enter the page as shown below, remember to click ssh:
Insert picture description here
but you can’t run git bash yet. When you use git clone or push command for the first time, connect to github will appear A warning, resulting in a dialogue:

Are you sure you want to continue connecting (yes/no)?

Because git uses ssh to connect, and when ssh connects to verify the github server key for the first time, you need to confirm whether the fingerprint information of the github server key really comes from the github server.

So we need to get an ssh key, and this step has been mentioned in the previous Git configuration;

So we open the following interface and copy the code line by line to git bash:
Insert picture description here
The new branch in the figure shows that the execution is successful:
Insert picture description here
Go back to the remote warehouse interface of github, refresh, and your own warehouse name appears to prove that the warehouse was successfully established:
Insert picture description here
Also, at the input git remote add origin **************, I reported a fault:

fatal: remote origin already exists.(报错远程起源已经存在。)

The solution is as follows:

# 1、先输入 
$ git remote rm origin
# 2、再输入 
$ git remote add origin **************

Github Pages build website

Personal site

access

https://username.github.io

Build steps

  1. Create Personal Site -> New Warehouse (Note: The warehouse name must be 用户名.github.io);
    Insert picture description here
  2. Create a new index.hitml file under the warehouse;
    Insert picture description here
    Insert picture description here
    Insert picture description here
  1. github pages only supports static web pages;
  2. Only html files in the warehouse;

Then I failed: I
Insert picture description here
don’t know why, harm;

Project Pages project site

access

https://username.github.io/warehouse name

Build steps

  1. Enter the project homepage and click settings;
  2. On the settings page, click [Choose a theme] to automatically generate a theme page:
    Insert picture description here
    Then my page has not been loaded, and I am mad at me, just write here first;

————————————————————————
Refer to two Zhihu articles:

Most of the pictures in the above steps are quoted from these two articles, and after I personally tried it, the final successful picture is the result of my own attempt;

Guess you like

Origin blog.csdn.net/Jessieeeeeee/article/details/107892585