Git and Github detailed introductory tutorial (Don’t tell me you don’t know Git and Github)

Foreword: Success is not in the future, but is accumulated from the moment you decide to do it. Hello, I’m Meng Yangchen, let’s get Git and Github together with me.


Article Directory

  • 01.Git overview
  • 02.Git local warehouse operation
    • Git version rollback operation
  • 03. Remote warehouse
  • 05.Git branch operation
  • 06. The generation and resolution of conflicts
  • 07. Git utility functions
  • 08. Ignore files
  • 09. The basic concepts of Github
  • 10. github Pages build website


Insert picture description here


01.Git overview

1. Git is currently the most advanced distributed version control system in the world .

Problems solved:
Recorded each modification: version, content, operating user, modification time, document name, etc.

2. The difference between
Git and Github Git is a distributed version control system. Simply put, it is a piece of software used to record changes in the content of one or several files in order to check the revision of a specific version of the software.

Github is a website that provides users with Git services. Simply put, it is a place where you can put code (and other content as well). In addition to providing a web interface for managing Git, Github also provides rich functions such as an online editor for subscription, follow, and discussion groups.

Git does not have a central server like SVN.

The Git commands we currently use are all executed locally, if you want to share your code through Git or cooperate with other developers. You need to put the data on a server that other developers can connect to. This example uses Github as a remote warehouse

Git installation

02.Git local warehouse operation

1. Workflow
Three areas of Git local operation:
Insert picture description here
Workflow:
Insert picture description here

Temporary storage area: it is equivalent to the shopping cart storing the items to be purchased and the final payment together.

2.
What is a local warehouse operation ?
The warehouse is also called the version library, and the English name is repository. We can simply understand it as a directory for storing code. All the files in this directory can be managed by Git. Git can be used to modify and delete each file. Track to.

The first use after installation requires global configuration:

1. Click "Git Bash Here" to open the Git command line window:

$ git config --global user.name "Username"
$ git config --global user.email "email address"

Set the user information, and record the user name when the project is modified

2. Create a local warehouse
When we need Git to manage a new project/existing project, we need to create a warehouse.

It is recommended to use an empty directory to learn Git, because operation errors will cause unnecessary errors. Try to use English names for directory names.

3. Create a directory

$ mkdir directory name

It can also be created directly locally in a new way.
4. Enter the project directory pre_git on the command line

$ cd directory name

5. The initialization of
the Git warehouse lets Git know that it needs him to manage this directory

$ git init

After execution, open the project directory, click on the hidden directory, and you will find an extra .git folder. It cannot be deleted or changed at will.

You can develop inside here.
6. Git common command operations to
view the current working status:

$ git status

Function: When we forget where the project is done, such as coming back from the toilet, coming back after a meeting, etc. You can use this command to determine what to do next.

7. Can be developed in the project directory

8. Add the workspace file to the cache area:

Description: The git add command can add one file or multiple files at the same time.
Syntax 1: $ git add file name
Syntax 2: $ git add file name 1 file name 2 file name 3 ......
Syntax 3: $ git add. [Add the current directory to the cache area]

9. Submit to the repository

$ git commit -m "comment content"

If a new file is created, start with add and repeat the above operations.

If you modify the content of the file that has been submitted, just resubmit it from add.

Operations such as submission here are just submitted to the Git local warehouse.

Git version rollback operation

Version rollback is divided into two steps:
1. First check the version and determine the point of time you need to return to
:

$ git log
$ git log --pretty=oneline

The second instruction: simplified information.
2. Rollback operation:
instruction:

$ git reset --hard commit number

After performing the operation, the files in the project directory will return to the specified time.

After going back to the past, what should I do if I want to go back to the latest state?
First: query the previous numbering
instructions:

$ git reflog

Then execute

$ git reset --hard commit number

Summary:
To go back to the past, get the commit id first, and then go back to the past through git reset --hard.

Back to the future: You need to use git reflog to query historical operations and get the latest commit id. (Numbering)

It can be found that when writing the rollback instruction, the commit id does not need to be written all, and git will automatically recognize it. At least the first 4 bits need to be written.

03. Remote warehouse

For online warehouse operations, take Github as an example. (Currently there are many websites that provide remote warehouses for Git warehouses)

1. Creation of Github online warehouse

Insert picture description here
2. Use online warehouse to
add remote warehouse (add github here)

Note: Enter cd /D in git bash. Note that the name of the disk must be capitalized. If you do not enter this statement and do not locate git bash, the default local file location is in the c drive. Then cd to the folder with the same name as the remote warehouse.

第一种方式:基于https协议:
仓库就是目录:
1.创建空目录,名称为shop(任取)
2.复制https对应得地址。
然后在当前目录新建shop目录

$ mkdir shop
$ cd ./shop

3.使用clone指令克隆线上仓库到本地。

$ git clone 线上仓库地址

执行后,线上的仓库就被clone下来了,如果clone下来的是一个空目录,克隆下来的目录只有隐藏的.git目录。

4.在上述操作完成后,就可以在本地仓库做对应的操作

比如:(提交至暂存区,提交到本地仓库,提交到线上仓库(远程仓库),拉取线上仓库
前面几个操作跟上面的本地仓库操作相同。

然后提交到线上仓库:

如果是首次提交,首先应获取权限:(否则会出现403的错误)
因为不是任何人都可以往线上仓库提交内容。
获取权限:
修改".git/config 文件内容:
在url这一项中:
在github.com前面加上:用户名:密码@
如:

url=https://用户名:密码@github.com/用户名/仓库名.git

其它不变。

指令:
提交到线上仓库的指令

$ git push

提交成功之后,你会发现,线上仓库中多你提交的内容。

注意:如果你提交成功下班后:同事也向该远程仓库提交了新的内容。第二天上班时,你需要拉去线上的仓库的最新版本
使用命令:

$ git pull

因此在每天工作的第一件事就是git pull 拉取线上的最新版本,下班要做的就是 git push,将本地代码提交到线上仓库。

第二种方式:基于ssh协议(推荐使用)
该方式与前面https方式相比,只是影响github对于用户的身份鉴定方式,对于git的具体操作没有任何改变。

步骤:
1.生成客户端公私钥文件。
2.将公钥上传到Github。

实际操作:
1.创建公私钥文件

你的本地 Git 仓库和 GitHub 仓库之间的传输是通过SSH加密的,所以我们需要配置验证信息:使用以下命令生成 SSH Key:

在这里直接打开Git Bash:(也可以在桌面单击右键打开)

Insert picture description here

然后输入命令:

ssh-keygen -t rsa -C "注册邮箱"

如果你无法生成密钥(须先自行安装OpenSSH)然后在执行以上命令。
网上的一些教程使用命令:

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

注意:其实[email protected]表示的就是注册邮箱,即两者是相同的。

之后会要求确认路径和输入密码,我们这使用默认的一路回车就行。

2.找到id_rsa.pub文件。
成功的话会在 ~/ 下生成 .ssh 文件夹,进去,打开 id_rsa.pub,复制里面的 key。
回到 github 上,进入 Account => Settings(账户配置)。
为了验证是否成功,输入以下命令:

$ ssh -T [email protected]

结果:Hi xxxx! You’ve successfully authenticated, but GitHub does not provide shell access.

3.然后再本地新建目录(用来保存线上的项目),然后进入目录。

$ mkdir 目录名称
$ cd ./目录名称/

然后克隆目录到新建的目录中:

$ git clone ssh地址

线上的仓库ssh和https地址可以在clone和download中查看
4.后续操作跟https方式一样。

相比于https,这种方式一旦配置好后,以后的项目就不需要一个个去配置用户名和密码了,可以说一劳永益。所以推荐使用这种方式

05.Git的分支操作

什么是分支 ?
即项目的分支(不同的功能):
所有的分支组成一个项目。
Insert picture description here
在版本回退的内容中,每次提交都会有记录,Git把他们串成时间线,形成类似于时间轴的东西,这个时间轴就是一个分支,我们称之为master分支。

在开发的时候往往是团队协作,多人进行开发,因此光有一个分支是无法满足多人同时开发的需求的,并且在分支上工作并不会影响其他分支的正常使用,会更加安全,Git鼓励开发者使用分支去完成一些开发任务。

分支的相关指令:

查看分支:git branch
创建分支:git branch 分支名
切换分支:git checkout 分支名
删除分支:git branch -d 分支名
合并分支:git merge 被合并的分支

注意:可以使用"git checkout -b 分支名" 指令来切换分支,-b选项表示创建并切换,相当于是两个操作指令。

含"*"表示当前分支。

可以看出我们都是对分支进行操作。

删除分支之前,需要退出该分支,既不能在使用中删除。

06.冲突的产生与解决

案例:模拟冲突
1.我下班之后,同事对线上项目内容进行了修改,此时本地仓库的内容与线上仓库内容不一致,第二天我忘记做git pull操作,而是直接对本地文件进行修改。
当进行提交时,会报错。
2.解决冲突
先进行指令"git pull"操作。

3.打开冲突文件,解决冲突。
和提交内容的用户商量保存哪些内容。删除哪些内容。

然后重新提交后,在进行push操作即可。

这里可以看到上班前进行 git pull 的重要性。

07.Git实用功能

1.图形管理工具

Github for Desktop
对于经常使用Github的开发人员来说是非常便捷的工具。

Source tree
老牌的Git GUI管理工具,适合初学者。

TortoiseGit
对于熟悉SVN的开发人员来说,非常友善。其前辈TortoiseSVN。

自带的Git GUI管理工具。

08.忽略文件

项目中存在万年不变的目录,例如css,js,images等,或者还有些目录即便有改动,我们也不想让其提交到远程的文档,此时我们可以使用"忽略文件”机制来实现需求。

忽略文件需要新建一个名为 .gitignore的文件,该文件用于声明忽略文件或不忽略文件的规则,规则对当前目录及其子目录生效。

注意:该文件因为没有文件名,没办法在windows目录下直接创建,可以通过命令行Git Bash来创建。

常见的规则如下:
1.过滤整个文件夹      /mtk/
2.过滤所有.zip文件    *.zip
3.过滤某个具体文件    /mtk/do.c
4.不过滤具体某个文件  lindex.php

新建.gitignore的文件
指令:

$ touch .gitignore

配置文件.gitignore:
如:
1.忽略/js目录
/js/

09.Github的基本概念

作用:借助github托管项目代码。
1.仓库(Repository)
仓库即你的项目,你想在Github上开源一个项目,那就必须要新建一个Repository,如果开源的项目多了,你就拥有多个Repositories。

2.收藏(star)
意为收藏项目的人数。方便下次查看。

3.复制克隆项目(Fork)
浏览他人项目时,点击fork,会在自己的账号里新建一个相同的仓库,该仓库是独立存在的,但是会显示 forked from 谁的仓库。
Insert picture description here
4.发起请求(Pull Request)
这是基于Fork的,李四在自己克隆的项目里进行改进完成后,想要将自己改进的项目合并到原来的项目中,于是他就发送了一个Pull Request,给原项目的创建人。假如你是这个项目的创建人,你就可以收到这个请求,这时候你会仔细review他的代码,如果觉得不错,就可以接受他的请求,这时候他做的改进,在你的项目里就有了。
Insert picture description here
步骤:
1.fork项目
2.修改fork的项目
3.新建pull request
4.等待项目新建者合并项目
5.关注(Watch)
如果你关注了某个项目,那么以后只要这个项目有任何的更新,都会接收到通知。

6.事务卡片(Issue)
就是你开源了一个项目,别人再查看你的项目时发现了Bug,或则有些地方做的不好,他就可以给你提个Issue,即问题,然后你看到了这些问题就可以去完善,完善好了就可以将其一个个的Close掉。
即发现代码Bug,但是目前没有成型的代码,需要讨论时使用。
如:别人给Itcastphpgit2提交一个issue
Insert picture description here

主页概念:
1.Github主页
显示用户动态以及关注用户或关注仓库的动态。
Insert picture description here

2.仓库主页
项目信息
Insert picture description here

3.个人主页
个人信息

官方网址:Github.com

新建Github账户:
**Bold style**
Insert picture description here
注意:github服务器在国外,所以访问较慢或者无法访问。

根据操作验证邮箱,创建远程仓库。

远程仓库的操作:

1.新建文件
Insert picture description here
2.编辑文件
Insert picture description here

点击文件名,然后点击“小笔"图案。

3.上传文件
点击上传文件,然后加描述。(Upload files)

4.搜索仓库文件
点击搜索(Find file)

5.下载项目
点击Clone or download

10.github Pages搭建网站

个人站点
访问:
https://用户名.github.io

搭建步骤
1.创建个人站点–>新建仓库(注:仓库名必须是【用户名.github.io】

2.在仓库下新建index.html的文件即可。

Note: github pages only supports static web pages
. 2. Only .html files can be stored in the warehouse.

I hope I can grow up with you, make progress together, and become a better version of myself. I am Meng Yangchen, a student at school! Hope to be a friend by your side.

Pay attention to the public account [Easy to play programming] and reply to the keyword activation code to get: The family bucket general activation code (the activation code supports idea, pycharm, webstorm, phpstorm... activation) is updated regularly. Applicable to the latest and below versions. (No need to crack and modify the hosts, if you have cracked, please uninstall and reinstall the official original version, if you have modified the hosts, please delete the URL you added, and then reactivate)

Reply to the keywords "ebook", "computer resources", "Java from entry to advanced", "JavaScript tutorial", "algorithms", "Python learning resources", "artificial intelligence", etc. to obtain learning resources.Insert picture description here


Guess you like

Origin blog.51cto.com/14854864/2553506