1.Git简介
Git是目前世界上最先进的分布式版本控制系统,在处理各种项目时都十分高效
Git是分布式版本控制系统,它就没有中央服务器的,每个人的电脑就是一个完整的版本库,这样,工作的时候就不需要联网了,因为版本都是在自己的电脑上。
2.Git安装(仅列出在Windows系统下的安装过程)
打开Git官网下载安装程序,然后按照默认选项安装即可。安装完成后,打开Git bash软件,弹出一个类似cmd的命令行窗口,证明安装成功。
安装完成后,需要进行设置,在命令行输入以下代码:
git config --global user.name "name"
git config --global user.email "name@mail.com"
顾名思义,这是设置你的名字和Email地址。
我们可以查看一下用户名和密码:
git config user.name
git config user.email
假如我们这时候报错,证明Git的用户名和密码没有配置成功,我们还可以这样做:
在用户主目录下找到 .git 文件夹: 然后打开 config 文件,这是专门用来配置和读取相应的工作环境变量的,在里面加上如图所示内容:
这样也就完成了对Git用户名和邮箱的配置。
3.创建版本库
版本库(repository)也叫仓库,可以看做一个目录,这个目录里的所以文件都由Git进行管理,每个文件的修改、删除,Git都能跟踪。
1.选择一个合适的地方,创建一个空目录:
- $ mkdir 20200214ZTE //创建一个名叫20202014ZTE的空目录
- $ cd 20200214ZTE 进入20200214ZTE目录
- $ pwd //查看当前目录
如果使用Windows系统,要保证目录名不包含中文。
2.通过如下命令把这个目录变成Git可以管理的仓库:
- $ git init
这样Git就把仓库建好啦,我们可以看到在当前目录下多了一个 .git 的目录,这个目录是Git来跟踪管理版本库的。
3.把文件添加到版本库
我们在 20200214ZTE 目录下编写一个 helloworld.txt 文件,内容如下:
this is demo for git command
helloworld is popular world!!
(1) 用 git add 命令,把文件添加到仓库:
$ git add helloworld.txt
(2) 用 git commit 命令,把文件提交到仓库:
$ git commit -m " xxxxxxx " // -m 后面输入的是本次提交的说明, 可以输入任何内容
4.修改文件
我们已经成功添加并提交了一个 helloworld.txt 文件,继续修改 helloworld.txt 文件,改成如下内容:
运行 git status 命令:
$ git status // 查看仓库当前的状态
- $ git status
- On branch master
- Changes not staged for commit: // 没有文件要被提交
- (use "git add/rm <file>..." to update what will be committed)
- (use "git restore <file>..." to discard changes in working directory)
- modified: helloworld.txt
- deleted: kevin.txt
- no changes added to commit (use "git add" and/or "git commit -a")
上面的命令告诉我们,helloworld.txt 文件被修改过了,但还没有准备提交的修改。
如果我们想知道上次是怎么修改readme.txt 文件的,需要用 git diff 命令:
$ git diff helloworld.txt
接下来还是那两步:
(1) git add
git add helloworld.txt
这时候可以用 git status 查看一下当前仓库状态:
(2) git commit
再用 git status 查看一下当前仓库状态:
nothing to commit, working tree clean //当前没有需要提交的修改,而且,工作目录是干净的。
5.版本回退
如果我们继续对 helloworld.txt 文件进行修改,改成如下内容:
- add modify for heloworld is a sample for git demo
- this line is fixed mark
- for reback check
然后添加并提交:
git add helloworld.txt
git commit -m "for reback check"
到目前为止,readme.txt 文件一共有4个版本被提交到了 Git 仓库里,我们可以用 git log 命令进行查看:
git log // 查看历史记录
我们还可以加上 --pretty=oneline 参数:
git log --pretty=oneline
-
NKG-KEVIN-LUENG+Kevin-Lueng@NKG-Kevin-Lueng MINGW64 /d/==Work/Gitlab/20200214ZTE (master) $ git log --pretty=oneline c9926b1cdcad69a92f476b5ebcb55d05322d6467 (HEAD -> master) for reback check 4c36becccc142edde808841d22e331da1c719204 2nd modify 204f3768157f9a61a903afee49be718c72a10a4f this is a helloworld file d4ee04bbea261dcd240e6c5d7d8c3dfe6c75b116 wrote a kevin temp file 好了,现在如果我们想把 readme.txt 文件退回到上一个版本,就可以使用 git reset 命令:
好了,现在如果我们想把helloworld.txt 文件退回到上一个版本,就可以使用 git reset 命令:
HEAD表示当前版本,HEAD^表示上一个版本,那么上上版本就是HEAD^^
这时候用 cat 命令查看一下 helloworld.txt 的内容:
果然 helloworld.txt 文件返回到了上一个版本。
我们现在想要回到最新的版本,还是使用 git reset 命令:
git reset --hard c9926 //这里不能用HEAD,而必须用commit ID, 因为最新版本在之前返回时已经被删除了,而commit ID可以在之前的代码中查到。
这时再查看一下 readme.txt 文件, 确认回到最新修改后的内容
6.工作区和暂存区
工作区(Working Directory)
20200214ZTE 文件夹就是一个工作区。
版本库(Repository),工作区有个隐藏目录 .git ,这个不算工作区,而是 Git 的版本库。
版本库里面的 index(stage) 文件叫暂存区,还有Git为我们自动创建的第一个分支 master ,以及指向 master 的一个指针叫做 HEAD。
前面我们提到过,如果我们想把文件添加到Git里面时,需要分两步:
- 第一步是用 git add 把文件添加进去,实际上就是把文件修改添加到暂存区(stage)。
- 第二步是用 git commit 提交更改,实际上就是把暂存区(stage)的所有内容提交到当前分支。(我们现在只有唯一一个分支 master,所以现在就是往 master 分支上提交更改)
练习:修改helloworld.txt里面的内容,并且新建一个文件(如 goodbye.txt, 添加任何内容)
使用两次 git add 命令分别把 helloworld.txt 和 goodbye.txt 都添加后,可以用 git status 命令查看一下:
现在,暂存区的状态就变成这样了:
再使用 git commit 命令把暂存区的所有修改提交到分支:
这时候的工作区就是干净的:
这时候版本库就变成了这样:
7.管理修改
Git 如此的优秀是因为,Git 跟踪并管理的不是文件,而是修改。
我们对 helloworld.txt 文件进行修改:
$ cat helloworld.txt
add modify for heloworld is a sample for git demo
this line is fixed mark
for reback check
for add and commit check
1st modify for GIT manage
然后,添加:
$ git add helloworld.txt
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: helloworld.txt
然后再修改 readme.txt 文件:
$ cat helloworld.txt
add modify for heloworld is a sample for git demo
this line is fixed mark
for reback check
for add and commit check
1st modify for GIT manage
###
2nd modify for GIT manage
提交:
$ git commit -m "1st and 2nd modify, check which be added"
[master f8ab4c9] 1st and 2nd modify, check which be added
1 file changed, 1 insertion(+)
这时候我们查看一下状态:
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: helloworld.txt
no changes added to commit (use "git add" and/or "git commit -a")
我们可以发现,第二次修改并没有被提交。因为在工作区的第一次修改被放入暂存区,准备提交;而在工作区的第二次修改并没有被放入暂存区,所以, git commit 命令只负责把暂存区的修改提交了。
提交后,我们可以用 git diff HEAD -- readme.txt 命令去查看工作区和版本库里面最新版本的区别:
$ git diff HEAD -- helloworld.txt
diff --git a/helloworld.txt b/helloworld.txt
index b8f31fa..b98cfcb 100644
--- a/helloworld.txt
+++ b/helloworld.txt
@@ -2,4 +2,6 @@ add modify for heloworld is a sample for git demo
this line is fixed mark
for reback check
for add and commit check
-1st modify for GIT manage
\ No newline at end of file
+1st modify for GIT manage
+###
+2nd modify for GIT manage
\ No newline at end of file
8.撤销修改
假如说你在 readme.txt 文件中添加了一行内容如下:
$ cat helloworld.txt
add modify for heloworld is a sample for git demo
this line is fixed mark
for reback check
for add and commit check
1st modify for GIT manage
###
2nd modify for GIT manage
for check reback
最后一行是万万不能让BOSS看到的,应该怎么撤销呢?
(1) 没有 git add 之前
可以手动删除最后一行,手动把文件恢复到上一个版本的状态。然后再用 git checkout -- file 命令丢弃工作区的修改:
(2) git add了,但没有git commit
这时候的修改添加到了暂存区,但没有提交到分支,用 git status 查看一下:
这时候我们可以使用 git reset HEAD file 命令把把暂存区的修改撤销掉,重新放回工作区:
$ git reset HEAD helloworld.txt // git reset 既可以回退版本, 也可以把暂存区的修改回退到工作区,HEAD表示最新版本
Unstaged changes after reset:
M helloworld.txt
现在再用 git status 查看一下:
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: helloworld.txt //暂存区是干净的,工作区有修改
no changes added to commit (use "git add" and/or "git commit -a")
这时候再丢弃工作区的修改就OK了:
$ git checkout -- helloworld.txt //丢弃工作区的修改
$ git status
On branch master
nothing to commit, working tree clean
终于大功告成了。
(3) 既 git add 了,也 git commit 了
可以回退到上一个版本,见回退版本内容。
9.删除文件
在工作区即 learngit 文件夹下新建一个 test.txt 文件,并添加和提交到Git:
$ vim new0217.txt
$ git add new0217.txt
warning: LF will be replaced by CRLF in new0217.txt.
The file will have its original line endings in your working directory
$ git commit -m "new file txt"
[master 27420c0] new file txt
1 file changed, 1 insertion(+)
create mode 100644 new0217.txt
这时候可用 rm 命令删除:
$ rm new0217.txt
这时工作区和版本库就不一样了。
现在又分两种情况:
(1) 确实要从版本库中删除该文件,那就用 git rm 命令删除,并且 git commit:
$ git rm new0217.txt
rm 'new0217.txt'
$ git commit -m "remove new file 0217"
[master 0d736f4] remove new file 0217
1 file changed, 1 deletion(-)
delete mode 100644 new0217.txt
这时候文件就从版本库被删除了。
(2) 文件被删错了。因为版本库里有,所以很好恢复:
$ rm new0217.txt
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: new0217.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ git checkout -- new0217.txt // 用版本库里的版本替换工作区的版本
$ git status
On branch master
nothing to commit, working tree clean
10.远程仓库准备工作
在开始这部分之前,我们需要自行注册Gitlab/github账号。而且,因为你的本地Git仓库和Gitlab/hub仓库之间的传输是通过SSH加密的,所以需要设置:
(1) 创建SSH Key。在用户主目录下C:\Users\Kevin-Lueng\,看看有没有.ssh 目录,如果有的话,看此目录下有没有 id_rsa 和 id_rsa.pub 这两个文件,如果有,直接跳到下一步。如果没有,打开Git Bash,创建SSH
(2) 登陆GitHub/Lab,关于如何创建账户及设置, 网上专业的教程太对了, 兄弟们可以自己搜索
下一篇再分享如何远程提交和克隆git
总结 git 基本命令
git init // 对文件进行 git 操作
git add README.md // 将 工作区 的文件移出到 暂存区
git checkout README.md // 将 暂存区 的文件移出到 工作区
git reset HEAD~ README.md // 将 Git 仓库 上一版本的文件移出到 暂存区
git commit -m "add README.md" // 将 工作区 的文件移出到 Git 仓库
git status // 查看文件状态
git log // 查看历史提交
git reset HEAD~ // 将 暂存区 变成 Git 仓库 的上一个版本,并删除这个版本的 Git 仓库
git reset --sort HEAD~ // 将 Git 仓库 变成上一个版本,并删除这个版本的 Git 仓库
git reset --hard HEAD~ // 将 工作区 变成 Git 仓库 的上一个版本,并删除这个版本的 Git 仓库(危险)
git diff // 比较 暂存区 和 工作区 的不同(显示的 - 代表 暂存区, + 代表 工作区)
git diff Git 仓库版本ID Git 仓库版本ID // 比较 Git 仓库两个版本间的不同,注意中间是一个空格,ID 是前 6 位
git diff Git 仓库版本ID // 比较 Git 仓库 和 工作区 的不同
git diff --cached Git 仓库版本ID // 比较 Git 仓库 和 暂存区 的不同,注意中间也是一个空格
git rm README.md // 删除 暂存区 和 工作区 的文件
git rm -f README.md // 当 暂存区 与 工作区 同名文件不一致,强制删除两者此文件
git mv README.md readme.md // 重命名,左边旧名,右边新名
git branch dev // 创建一个 dev 分支( 对立的是 master 分支 )
git branch // 查看所有分支
git checkout master // 切回 master 分支
git merge dev // 将 dev 分支与当前所在分支合并
git branch -d dev // 只能删除已经 merge 的分支,对于未 merge 的分支是无法删除的
git branch -D dev // 强制删除分支
————————————————