Git quick start (1)

1. What does git do?

Are the students often troubled by the confusion of file versions in the process of completing projects in groups, and some modified content is lost due to modification by multiple people? Can't find the latest version?

Seeing the picture below, do students feel familiar? In order to keep the change record, we will save copies of many versions, and in this way, the modified historical version will be preserved. In this way, in case there is a problem with a certain version, we can still find the previous version.

Backing up files by copy is easier to operate , but this method has some obvious flaws :

1. It is difficult to know what has been modified between versions. The document must be open for comparison viewing.

2. If many people edit and modify the document at the same time, it is often prone to file confusion and partial information loss, especially for everyone's final design, when multiple students complete the code, they often encounter such confusion.

The version management tool can help me record the evolution history of the file, which can be traced back to the historical version.

2. SVN vs GIT

Before we learned the configuration management tool svn, we know that svn is a centralized version management tool.

Let's take a look at the difference between git and svn, git is a distributed version management tool

In SVN , the warehouse is deployed on the server, and there is a single point of failure , that is, if the server crashes, the development cannot submit or check out the code.

In Git , there are version libraries on the remote server and the development computer, and there is no single point of failure .

Next, let's use the most popular distributed version management tool git to manage our change records and let multiple people work together. This skill is an indispensable skill for an IT person! ! !

git is an open source distributed version management tool. Compared with the centralized version management tool SVN, it can also avoid the problem of single point of failure,

The content of this article is completed on the windows operating system . For other operating systems, please refer to the git official website or other documents

为了感受远程仓库的学习,建议同学们在码云gitee上注册一个账号

3 git的下载与安装

1)下载git安装包

下载路径:https://git-scm.com/

我的操作系统是window,64位的,我下载的Git-2.33.0-64-bit.exe,从官网下载或者从网址下载链接:

链接地址:https://pan.baidu.com/s/1jkxEJ3pI3JIyDyYJ2uHIkw

提取码:1234

2)安装git

windows上,直接运行安装包,傻瓜式安装即可。

安装完成后,在桌面上点击右键菜单,可以看到 git菜单​

打开git bash,输入 git --version ,如果如下图所示,返回了版本信息,就说明git安装成功了。

3)安装net framework4.7.2

机房的计算机push代码时,系统提示需要安装此组件。傻瓜时安装下即可。教室机房千万不要重启,不然计算机会恢复的。

这个组件如果不安装,后面与远程仓库交互总是会弹出账户输入窗口,安装之后只需要输入一次张宏。

百度网盘链接地址:https://pan.baidu.com/s/1jkxEJ3pI3JIyDyYJ2uHIkw

提取码:1234

4)账号需要反复输入的解决办法

关于账号一次登录。在不用输入账号的方法

https://blog.csdn.net/whereismatrix/article/details/126517683

在git bash中执行下该句。告诉git记住git账号。

git config --global credential.helper store

然后再git push 或者 git pull时,输入账号,系统记住账号

4 git的使用

可以参考git官网的帮助来学习git

Git - Book https://git-scm.com/book/zh/v2

1)gitee上注册账号

https://gitee.com/

2)建议gitee账号绑定一下你的邮箱

3)在gitee上创建仓库

在gitee的右上角的“+”,可以添加一个仓库 (每个项目创建一个仓库,仓库可以保留项目的历史文件)

期中分支模型有很多种,大家可参考 https://www.sohu.com/na/446599111_115128 了解各种分支模型的区别。参考该文中的叙述,结合我自己的理解,各种分支大致含义如下:

master 用于存放对用户发布的版本、类似与svn的tag

develop 用于存放开发的过程版本。它从master分出,然后完成开发后合并到master

release 用于存放交付测试的版本。它来自develop,当develop开发到一定程度需要测试时,把测试版本放到release分支上。当release版本测试中有bug时,修复bug,产生新版本,需要时再次交付给测试,最终release通过测试后,合并到master分支。当然也会合并到develop,将期间修复的缺陷的代码合并到develop。

feature 用于开发某个功能而拉出的分支,它来自develop,开发完成后合并到develop

hotfix 用于修改某个用户版本的缺陷而拉出的分支。它来在master分支,合并回master分支。当然为了避免后期版本没有此问题,该hotfix也会合并到develop。

新建仓库界面上,可以复制仓库的地址。也可以看到git的账户设置命令。这些后面都会用到

点击上面的【初始化readme文件】 ,界面如下。现在你通过浏览器其实就可以维护你的仓库了奥!

也就是你可以通过浏览器维护你的仓库,新建、修改、删除文件或者文件夹,

以后,你登录gitee,在“我的”中就可以找到仓库,打开仓库了。

4) 查看git命令的帮助

有时使用某个命令时,我们会忘记了该命令的格式,那么这时候就可以借助与git 的在线帮助功能

a) 命令后面加一个 -h参数

例如,如果想查看git log 命令的参数情况,就在其后追加 一个 -h参数

a) 命令后面加一个 --help参数

5) 本地配置用户信息

初次使用git前,需要配置用户信息

在任意目录下点击右键菜单git bash here

设置用户信息,这样后期操作文档时就能记录是谁操作的。这里的信息你在giee上之前看到过把

git config --global user.name "username"

git config --global user.email "[email protected]"

设置完之后,可以使用 git config ---list查看配置信息

6)开发将仓库内容克隆到本地

本地创建文件夹demo,在文件夹中点击右键菜单 【Git Bash Here】

输入命令: git clone https://gitee.com/caohx666/cmo_week03

机房如果安装了 net framework的同学,在这里输入 gitee账号,如果没有安装的同学,后面每次与远程服务器交互都需要输入用户名和密码。安装之后后面就不需要反复输入账号了

如果没有适当设置,每次从远程仓库交互都需要输入账号的用户名、密码,如下图所示。

输入gitee账户的密码

如果账号正确,则可以成功将远程仓库的信息下载到本地。

注意:有的计算机上执行时,会提示安装net framework,请下载安装。

7)添加文件后提交到本地仓库

在git中添加文件,需要先用git add命令将文件加到暂存区,然后再将修改git commit提交到本地仓库

git add 文件名 将指定文件添加到暂存区

git add . 将所有文件添加到暂存区

git commit -m "message" 提交暂存区的内容到本地仓库,日志信息为:message

注意:

有时,提交时忘记指定提交日志(少了 -m "xxxx"),

则有的系统会打开如下的记事本。输入日志保存即可。而有的系统会打开vim编辑器让你输入日志信息

有的系统则是用vim打开了文件。大家点击键盘上的i,进入插入模式,输入要添加的日志信息后,点击ESC键后,输入 :wq 即可保存修改并退出编辑,如果不想保存退出,则输入 :q

在vim中。

任意时刻,点击esc键 可以进入到普通模式

普通模式下输入 i、a 、o 可以进入插入模式,可以编辑文本信息

普通模式下输入v,进入可视模式,可以选择操作的文字,一个、一段或者多段等,进行删除、替换等等操作

普通模式下输入V。进入行可视模式。进行行选择

普通模式下输入 control +v ,进入块选模式

8)本地仓库信息提交到远程仓库

因为我们之前从仓库clone出来的,所以push的时候自动push到那个远程仓库了。

如果你想提交到其他远程仓库,需要是使用git remote 来添加和查看远程仓库。

9) 修改文件后提交到本地仓库

git add finename 将修改过的文件保存到暂存区,等待提交

git commit -m “提交日志xxxx” 将暂存区文件提交到仓库

git push 将本地仓库的内容提交到远程仓库

具体过程如下。

先修改001.txt 的第一行,修改为如下内容。

修改文件后保存。保存后文件的图标变为黄色惊叹号,表明该文件已经修改。

之后,进行git push即可将修改提交到远程仓库了。

10) 删除文件提交到仓库

为了方便演示,我们就演示刚才添加的文件001.txt

git rm filename 删除文件提交到暂存区

git commit -m “编写提交日志” 提交到本地仓库

git push 提交到远程仓库

11)查版本更新日志

git log 可以查看提交历史日志。它有很多参数可以设置查看的效果,下面是常用的几个参数

如果直接使用git log 显示信息较多,查看效果如下所示,并不是太友好

日志按照从最新到最旧的倒序时间顺序显示。

当日志较多时,会出现冒号,大家可以通过键盘的上下方向键 或者 回车键 来查看日志

如果要结束日志查看,冒号后输入q即可。

git log 也提供了一些参数来设置显示效果。通过这些参数可以让显示更美观,更好看些

--all 显示所有分支

--pretty=oneline 将提交信息显示为一行

--abbrev-commit 仅显示commitId的前几个字符,而非40个字符,使得更简短

--oneline 简洁形式显示提交日志

--graph 显示分支与合并历史

--reverse 按照从远到近的顺序显示提交日志

git log --pretty=oneline 和 git log --oneline 都是每行显示一个版本,但是后者的版本号更为简洁。

git reflog

除了日志,还显示了 head@次数 查看回退到指定版本的次数

git log --oneline --graph --decorate --all 是常用的日志查看命令

12) 更新远程仓库文件到工作区

git pull 将远程仓库的内容拉取更新到工作区

为了做这个实验,大家可以通过浏览器 去修改文件内容 。我修改了1.txt的内容

可以看到 git pull 之后 ,本地工作区内容更新了

13) 更新最新的远程仓库文件到本地仓库,然后再更新工作区

关于游离态的说明

http://www.360doc.com/content/21/0827/17/19244573_992865850.shtml

git fetch 将远程仓库 代码 更新 到本地仓库

git chekout 将本地仓库内容更新到工作区

先使用 git fetch 拉取到本地 仓库,

为了做这个实验,大家可以通过浏览器 去修改文件内容 。我修改了1.txt的内容的前8行

然后再使用git checkout 更新到工作区

14) 更新最新的远程仓库文件到本地仓库

git fetch 将远程仓库 代码 更新 到本地仓库

15)文件冲突的解决--待续

16)分支的操作——待续

不错的博客

https://blog.csdn.net/itworld123/article/details/120220097

17)本地文件提交到远程的新仓库

# 1.初始化
git init
 
# 2.添加所有文件到本地仓库
git add .
 
# 3.提交到本地仓库(备注自行修改)
git commit -m "init"
 
# 4.新建远程仓库
git remote add origin https://git仓库地址/分组名/仓库名.git
# 示例: git remote add origin https://github.com/xx-group/first-project.git
 
# 5. push到远程仓库

5.常见问题的解决方法

1) 若git未记住账号

每次push等都需要输入账号,则可以修改.git 下的 gitconfig文件

修改.gitconfig配置文件,把以下内容放置到最后

[credential]

helper = store

[credential]
          helper = store  

2) ideal 进行团队合作开发

大家可以学习 使用gitee进行团队合作

https://blog.csdn.net/zhang19971014/article/details/124631247

3)git文件不显示图标

若仓库上不显示图标

参考博客https://blog.csdn.net/Aaron_King/article/details/126153694

4) git push 或 pull时访问被决绝403

git config --system --unset credential.helper

5) TLS certificate verification has been disabled!

6)清除账号信息

7)在线绘制图片

https://www.processon.com/diagrams

Guess you like

Origin blog.csdn.net/caohongxing/article/details/129356880