Use of git and github

Introduction to git and github

What is Git

Git is an open source [distributed] [version control system] for agile and efficient processing of any small or large project

Version controller
CVS/SVN/Git

SVN
client/server

GIT
client/code hosting website (for example: github)

Note 1: There are also similar code hosting sites: github/Code Cloud/Alibaba Cloud Code Source Code Management
Note 2: Similar technology GitLab, but it is different from
GitHub. GitLab and GitHub belong to third-party Git-based works. , Free and open source. Similar to Github, you can register users, submit your code, add SSHKey, etc.
The difference is that GitLab can be deployed on your own server. All information such as the database is in your hands. It is suitable for collaborative development within the team.
You can never put the wisdom of the team on someone else's server, right? Simply put, GitLab can be regarded as a personal version of GitHub.

The difference between Git and SVN (key record 1/2/3 points)

1.
Decentralized GIT is distributed, SVN is not: This is the core difference between GIT and other non-distributed version control systems, such as SVN, CVS, etc.
2. Any copy of the git repository can be used independently as a server.
3. There are four statuses of files in Git:
untracked: indicates that the file is newly added and
modified: indicates that the file has been modified. But it has not been saved in the git repository.
Staged: Indicates that the current version of a modified file is marked to be included in the next submitted snapshot.
Committed: Indicates that the file has been saved in the git repository.
4. Other
GITs do not have a global version number, but SVN has: So far this is the biggest feature that GIT lacks compared with SVN.
GIT's content integrity is better than SVN: GIT's content storage uses the SHA-1 hash algorithm. This can ensure the integrity of the code content and reduce the damage to the version library in the event of disk failures and network problems.
5. Directly record snapshots instead of differences.
GIT stores the content as metadata, while SVN is filed: all All of the resource control systems hide the meta information of files in a folder like .svn, .cvs, etc.
Directly record the snapshot instead of the difference
6. Directly record the snapshot, not the difference The
GIT branch is different from the SVN branch. The branch is not special in SVN, it is another directory in the repository.
Insert picture description here

Simple use of Github

Registration is relatively simple, so I won’t introduce too much here, and then log in to the github website

Insert picture description here
Create a private library,
Insert picture description here
create a public library,
Insert picture description here
Insert picture description here
create a file,
Insert picture description here
Insert picture description here
then submit,
Insert picture description here
Insert picture description here
create a folder,
Insert picture description here
Insert picture description here
if I want to add a description when creating a file,
Insert picture description here
Insert picture description here
Insert picture description here
delete the warehouse
Insert picture description here
Insert picture description here

Git download and install

Download address:
https://git-scm.com/downloads After the
Insert picture description here
download is complete:
Insert picture description here
Insert picture description here
create the start menu directory name, default git, no need to modify,
Insert picture description here
select the editor used by git,
Insert picture description here
Insert picture description here
Insert picture description here
line break conversion at the end of the line, use the default value
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Install to
Insert picture description here
Insert picture description here
detect installation success, Right-click on any window or desktop, and the following two options will appear
Insert picture description here
Insert picture description here

Git common commands

Git configuration account and email (local machine)
Generally, the user name and password must be configured when you just install Git, because you will use it when you submit code to the local warehouse (upload code to a remote warehouse).
If there is no configuration, it will be used when you submit Remind you. However, if you only clone (ie download) public projects, you don't even need to register-the account we just created is to
fork other projects and push our own changes in the future .

Command line configuration

0. " Git bash here " to open the command window
1. View the git configuration list
git config --list
Note 1: If there is no configuration, it should be empty. After completing the configuration according to the following steps, you can try the command again, and a series of configuration information will appear
. 2. Set the username/email/password
git config --global user.name “username”
git config --global user. email "email"
git config --global user.password "password"

We don’t need a password. Then use ssh key
git config --global user.name "xieminglu02"
git config --global user.email "[email protected]"

 注1:全局变量
       --global  表示全局的,即当前用户都有效,该配置会出现在 ~/.gitconfig 文件中,~表示当前用户的目录,
      比如我的是:C:\Users\Administrator\.gitconfig,打开该文件你会发现如下图所示的内容: 
       [user]
   name = xxx
   email = [email protected]
 注2:局部变量
      git config user.name  "username"  
      git config user.email  "email"
      局部是只对当前仓库起效的,它的配置信息会在当前仓库根目录/.git/config文件下
      注意:局部变量覆盖全局变量!!!和编程语言里面的变量关系是一样的。
 注3:删除某个全局配置项
      git config --global --unset user.name

Insert picture description here
Insert picture description here

Git command usage scenarios

Git file status and work area (emphasis) The
git file status is the core content of git. After understanding it, it will be of great help to subsequent operations. Different file statuses are stored in different work areas.
1. File status
The file in git has the following status.
Untracked: indicates that the file is newly added.
Modified : indicates that the file has been modified but has not been saved to the git repository.
Staged: Indicates that the current version of a modified file is marked to be included in the next submitted snapshot.
Committed: Indicates that the file has been saved in the git repository.

2. Working area
According to local computer and remote computer, there are the following working areas:
1) Local computer
Working Directory: the status of the file being edited. The file status is untracked (untrack) and modified (modified) in this area.
Staging Area (temporary storage area): save the file list information that will be submitted next time. The file status is staged in this area.
Repository (local warehouse): files submitted to the local warehouse.
2) Remote computer
Repository: files that have been submitted to the remote computer.

Insert picture description here
Common commands (emphasis)
git clone: ​​clone the remote warehouse to the local computer.
git status: Display files in different states in the workspace and temporary storage area.
git add: Add content from the working directory to the staging area.
git commit: All files temporarily stored by git add are submitted to the local warehouse.
git push: Submit the records of the local warehouse to the remote warehouse.
git reset HEAD: Remove the specified file from the temporary storage area.
git checkout -: restore the specified file from the local warehouse.
git pull: pull data from remote warehouses.
git init

Practical operation: Use git to submit files to a remote warehouse (key)
first clarify the general process:
1. Get the Git warehouse of
the project. The first is to clone a new warehouse (local) from the existing github warehouse (remote).
The second is to Initialize a new warehouse (local) in the working directory, and then associate it with a warehouse (remote) on Github
2. Make the changes you need on the local clone library, modify file content, add files, etc.;
3.add, Commit, push trilogy.

To clone a new warehouse (local) from an existing github warehouse (remote)
:
1. Obtain the project's Git warehouse (clone from an existing github warehouse)
## Here url is changed to the address of your own warehouse
git clone https:/ /github.com/xieminglu02/library_pub.git

 注1:此命令,会在当前目录下创建一个名为library_pub的目录,其中包含一个 .git 的目录,用于保存下载下来的所有版本记录。
      如果进入这个新建的 library_pub目录,你会看到项目中的所有文件已经在里边了,准备好后续的开发和使用。

 注2:克隆仓库的命令格式为 git clone [url],Git支持多种协议,包括https,但通过ssh支持的原生git协议速度最快    
      git clone [email protected]:fsliurujie/test.git          --SSH协议
      git clone git://github.com/fsliurujie/test.git        --GIT协议
      git clone https://github.com/fsliurujie/test.git      --HTTPS协议 
      提问:那平时使用哪个协议最好呢???

 注3:你熟悉其他的 VCS 比如 Subversion,你可能已经注意到这里使用的是 clone 而不是 checkout。这是个非常重要的差别,
      Git 收取的是项目历史的所有数据(每一个文件的每一个版本),服务器上有的数据克隆之后本地也都有了。实际上,
      即便服务器的磁盘发生故障,用任何一个克隆出来的客户端都可以重建服务器上的仓库,回到当初克隆时的状态 
 注4:如果希望在克隆的时候,自己定义要新建的项目目录名称,可以在上面的命令末尾指定新的名字
      git clone https://github.com/xieminglu02/library_pub.git mytest1

Note 5: How to view the .git directory (the default is a hidden folder)
window menu: "View" -> "Options" -> Select, tab "View" -> Check "Show hidden files, folders and drives"

2. After viewing the initial status of
git clone a warehouse, run git status.
Note 1: Another way to view the status of the git warehouse is to
enter the test1 directory. Use the ls -ah command to display the .git directory, which means that the clone has been successful, and This directory is already a warehouse that git can manage

Insert picture description here
3. Add a file
Add an add.txt file t in the source directory of this warehouse, and run git status
Note 1: You can see "Untracked files" above the file, which means that this file is "untracked"

4. Add the newly created file to the "temporary area" and
execute git add source/add.txt, the newly created add.txt will be submitted to the "temporary area", and run git status after execution.
Note 1: There is Description Use git reset HEAD to remove the specified file from the temporary storage area.
Insert picture description here

5. Submit the contents of the "temporary area" to the "local warehouse" to
execute git commit, and submit the contents of the "temporary area" to the "local warehouse", and run git status after execution:

 注1:如果没有配置帐号或密码,会出现以下提示
      *** Please tell me who you are.
 注2:git commit 时出现:please enter the commit message for your changes,
      默认会启用 shell 的环境变量 $EDITOR 所指定的软件,一般都是 vim(没错啦,就是linux课程使用过滴) 或 emacs,

      即询问你是否要添加“提交说明”
      要输入“提交说明”的话就需要
      1.按键盘字母 i 进入insert模式
      2.修改最上面那行黄色合并信息,可以不修改
      3.按键盘左上角"Esc"
      4.输入":wq",注意是冒号+wq,按回车键即可

      也可以使用 -m 参数后跟“提交说明”方式,在一行命令中提交更新。
      git commit -m “提交说明”
      另外,提交说明在团队合作中是非常重要的一点,要尽量写的简洁而语意清晰

Insert picture description here

6. Submit the contents of "local warehouse" to "remote warehouse" and
execute git push, and submit the contents of "local warehouse" to "remote warehouse". After execution, run git status
prompt: you can see that the git status is back to initialization after submission status.

Note: Enter the username (email or login account xieminglu02) and password of the github website here
Insert picture description here
Insert picture description here
Insert picture description here

This is when the file is modified and the comment is submitted directly
git commit -a||-m "The modified file, skip the temporary storage area and put it directly into the local warehouse comment description"
Insert picture description here
Insert picture description here

Initialize a new warehouse (local) in the working directory, and then associate with a warehouse (remote) on Github

1. Get the Git repository of the project (initialize a new repository in the working directory).
To start Git management on an existing project, just go to the directory where the project is located, for example: D:\initPath\git_repository\library\ library_pub2,
execute: git init
2. Same as above
3. Same as above
4. Same as above
5. Same as above
6. Submit the content of "local warehouse" to "remote warehouse" to
execute git push, and submit the content of "local warehouse" to "remote warehouse", The following error will be reported
fatal: No configured push destination.
Either specify the URL from the command-line or configure a remote repository using
This is because the local warehouse is not associated with any remote repository, so it must be provided when submitting for the first time The URL of the remote warehouse, the specific operations are as follows:

 ## 在github先建仓库test3,目前,在GitHub上的这个仓库还是空的,GitHub告诉我们,可以从这个仓库克隆出新的仓库,
 ## 也可以把一个已有的本地仓库与之关联,然后,把本地仓库的内容推送到GitHub仓库。
 1.打开浏览器,登陆github后,新建一个仓库test3(目前在GitHub上的这个仓库还是空的),它的地址为:
   https://github.com/xieminglu02/library_pub2.git

   注1:在GitHub主页创建test3仓库,注意不要添加README.md等任何文件

 2.将本地仓库的当前分支与远程仓库相关联 
   git remote add origin https://github.com/lixiao12/test224_pub2.git

   注1:语法:git remote add <name> <url>
        远程库的名字就是origin,这是Git默认的叫法,也可以改成别的,但是origin这个名字一看就知道是远程库
   注2:https://github.com/lixiao12/test224_pub2.git改成自己仓库对应的URL
   注3:补充命令,如果url打错了,可以通过如下命令修改远程仓库关联
        git remote set-url origin https://github.com/lixiao12/test224_pub2.git

 3.将本地代码库的某一分支(eg:master)推送到远程的代码库
   git push -u origin master
   注1:把本地库的内容推送到远程,用git push命令,实际上是把当前分支master推送到远程
        由于远程库是空的,我们第一次推送master分支时,加上了-u参数,Git不但会把本地的master分支内容推送的远程新的master分支,
        还会把本地的master分支和远程的master分支关联起来,在以后的推送或者拉取时就可以简化命令。
   注2:补充命令,查看git仓库的远程代码库的地址:git remote -v
   注3:再次添加文件b.txt,依次add/commit/push操作,则与之前没有区别了  

Use git to pull from a remote warehouse to a local warehouse (focus)
git pull

Git conflict

1. First pull the code from the remote warehouse to the local, then modify the conflict code, git pull
Note 1: git status to view the warehouse status, a red error message "both modified: conflict.txt" will be displayed

2. Open the conflict.txt, and manually modify the code of the conflict part
<<<<<<< HEAD
library_pub directory to modify
=======
library_pub2 directory to modify
>>>>>>>> b6d61fdf3b4088b12c83e0a72dc5a431a023c0c2
Note 1: <<< The code between <<<< HEAD >>>>>>> is the conflicting code, you can modify it manually

3. In turn add/commit/push code to the remote warehouse
commit execution is completed, recheck the warehouse status git status, the red error message "both modified: conflict.txt"
is no longer there, indicating that the conflict has been resolved

Guess you like

Origin blog.csdn.net/xieminglu/article/details/102934355