原视频
版本控制介绍
集中式版本控制
- 问题:单点故障
分布式版本控制
Git安装
video
Git结构
Git和代码托管中心
代码托管中心的任务:维护远程库
- 局域网环境下
- GitLab服务器
- 外网环境下
- Github
- 码云
本地库和远程库
Git命令行操作
- 本地库初始化
- 命令:git init
- 注意:.git目录中存放的是本地库相关的子目录和文件,不要删除,也不要胡乱修改。
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace
$ mkdir EmptyDemo
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace
$ cd EmptyDemo/
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo
$ git init
Initialized empty Git repository in E:/IdeaProjects/workspace/EmptyDemo/.git/
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ ll .git/
total 7
-rw-r--r-- 1 lenovo 197609 130 7月 1 10:06 config
-rw-r--r-- 1 lenovo 197609 73 7月 1 10:06 description
-rw-r--r-- 1 lenovo 197609 23 7月 1 10:06 HEAD
drwxr-xr-x 1 lenovo 197609 0 7月 1 10:06 hooks/
drwxr-xr-x 1 lenovo 197609 0 7月 1 10:06 info/
drwxr-xr-x 1 lenovo 197609 0 7月 1 10:06 objects/
drwxr-xr-x 1 lenovo 197609 0 7月 1 10:06 refs/
- 设置签名
- 形式
- 用户名
- Email地址
- 作用:区分不同开发人员的身份
- 辨析:这里设置的签名和登陆远程库(代码托管中心)的账号、密码没有任何关系。
- 命令
- 项目级别/仓库级别:仅在当前本地库范围内有效
- git config user.name mrwang
- git config user.email [email protected]
- 系统用户级别:登陆当前操作系统的用户范围
- git config --global user.name mrwang
- git config --global user.email [email protected]
- 级别优先级
- 就近原则:项目级别优先于系统用户级别,二者都有时采用项目级别的签名
- 如果只有系统用户级别的签名,就以系统用户级别的签名为准
- 二者都没有是不允许的
- 项目级别/仓库级别:仅在当前本地库范围内有效
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git config user.name mrwang
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git config user.email [email protected]
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ cat .git/config
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[user]
name = mrwang
email = [email protected]
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git config --global user.name mrwang
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git config --global user.email [email protected]
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ cd ~
lenovo@Maximus-PC MINGW64 ~
$ pwd
/c/Users/lenovo
lenovo@Maximus-PC MINGW64 ~
$ ls -lA |grep git
-rw-r--r-- 1 lenovo 197609 153 7月 1 11:12 .gitconfig
lenovo@Maximus-PC MINGW64 ~
$ cat .gitconfig
[user]
name = mrwang
email = [email protected]
[core]
autocrlf = true
excludesfile = C:\\Users\\lenovo\\Documents\\gitignore_global.txt
添加提交以及查看状态操作
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ vim good.txt
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add ..." to include in what will be committed)
good.txt
nothing added to commit but untracked files present (use "git add" to track)
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git add good.txt
warning: LF will be replaced by CRLF in good.txt.
The file will have its original line endings in your working directory.
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached ..." to unstage)
new file: good.txt
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git rm --cached good.txt
rm 'good.txt'
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add ..." to include in what will be committed)
good.txt
nothing added to commit but untracked files present (use "git add" to track)
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ ll
total 1
-rw-r--r-- 1 lenovo 197609 24 7月 2 08:04 good.txt
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git add good.txt
warning: LF will be replaced by CRLF in good.txt.
The file will have its original line endings in your working directory.
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached ..." to unstage)
new file: good.txt
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git commit good.txt
warning: LF will be replaced by CRLF in good.txt.
The file will have its original line endings in your working directory.
[master (root-commit) 488d18f] My first commit. New file good.txt.
1 file changed, 3 insertions(+)
create mode 100644 good.txt
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git status
On branch master
nothing to commit, working tree clean
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ vim good.txt
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: good.txt
no changes added to commit (use "git add" and/or "git commit -a")
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git add good.txt
warning: LF will be replaced by CRLF in good.txt.
The file will have its original line endings in your working directory.
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
modified: good.txt
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git commit -m "My second commit, modify good.txt" good.txt
warning: LF will be replaced by CRLF in good.txt.
The file will have its original line endings in your working directory.
[master 7752881] My second commit, modify good.txt
1 file changed, 1 insertion(+)
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$
版本穿梭
查看历史记录的几种方式
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git log
commit 91e905998a46c668b511925aa493e4b957b8b69c (HEAD -> master)
Author: mrwang
Date: Mon Jul 2 09:30:01 2018 +0800
insert fffffff edit
commit 594e334173b17a770bbeaf60239a9d70241cc783
Author: mrwang
Date: Mon Jul 2 09:29:25 2018 +0800
insert eeeeeee edit
commit b2025ce2431e90faefd3bc474b5033151f573516
Author: mrwang
Date: Mon Jul 2 09:24:06 2018 +0800
for test history
commit 77528812922c4c114db3aec3b0a33b709a7c3ae0
Author: mrwang
Date: Mon Jul 2 09:13:35 2018 +0800
My second commit, modify good.txt
commit 488d18f388b275815b934402fca31e4a51143c56
Author: mrwang
Date: Mon Jul 2 08:55:07 2018 +0800
My first commit. New file good.txt.
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git log --pretty=oneline
91e905998a46c668b511925aa493e4b957b8b69c (HEAD -> master) insert fffffff edit
594e334173b17a770bbeaf60239a9d70241cc783 insert eeeeeee edit
b2025ce2431e90faefd3bc474b5033151f573516 for test history
77528812922c4c114db3aec3b0a33b709a7c3ae0 My second commit, modify good.txt
488d18f388b275815b934402fca31e4a51143c56 My first commit. New file good.txt.
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git log --oneline
91e9059 (HEAD -> master) insert fffffff edit
594e334 insert eeeeeee edit
b2025ce for test history
7752881 My second commit, modify good.txt
488d18f My first commit. New file good.txt.
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git reflog
91e9059 (HEAD -> master) HEAD@{0}: commit: insert fffffff edit
594e334 HEAD@{1}: commit: insert eeeeeee edit
b2025ce HEAD@{2}: commit: for test history
7752881 HEAD@{3}: commit: My second commit, modify good.txt
488d18f HEAD@{4}: commit (initial): My first commit. New file good.txt.
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$
版本前进后退
-
本质
- 基于索引值操作[推荐]
git reset --hard [局部索引值]
- 例如:
git reset --hard a6ace91
- 使用^符号:只能后退
git reset --hard HEAD^
- 注:一个^表示后退一步,^^表示后退两步,n个^表示后退n步
- 使用~符号:只能后退
git reset --hard HEAD~n
- 注:表示后退n步
基本操作
- 状态查看操作
git status
查看工作区、暂存区状态 - 添加操作
git add [file]
将工作区的“新建/修改”添加到暂存区 - 提交操作
git commit -m "commit message" [file]
将暂存区的内容提交到本地库 - 查看历史记录
git log
log多屏显示控制方式:空格 -> 向下翻页,b -> 向上翻页,q -> 退出
git log --pretty=oneline
git log --oneline
git reflog
HEAD@{移动到当前版本需要多少步} - 版本前进后退
- 基于索引值操作[推荐]
git reset --hard [局部索引值]
- 例如:
git reset --hard a6ace91
- 使用^符号:只能后退
git reset --hard HEAD^
- 注:一个^表示后退一步,^^表示后退两步,n个^表示后退n步
- 使用~符号:只能后退
git reset --hard HEAD~n
- 注:表示后退n步
- reset命令的三个参数对比
- --soft
- 仅仅在本地库移动HEAD指针
- --mixed:
- 在本地库移动HEAD指针
- 重置暂存区
- --hard
- 在本地库移动HEAD指针
- 重置暂存区
- 重置工作区
- 删除文件并找回
- 前提:删除前,文件存在时的状态提交到了本地库。
- 操作:`git reset --hard [需找回文件存在的指针位置]
- 删除操作已经提交到本地库:指针位置指向历史记录
- 删除操作尚未提交到本地库:指针位置使用HEAD
- 比较文件差异
-
git diff [file name]
: 将工作区中的文件和暂存区进行比较 -
git diff [本地库中历史版本] [file name]
: 将工作区中的文件和本地库历史记录进行比较 - 不带文件名比较多个文件
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git diff good.txt
diff --git a/good.txt b/good.txt
index baf19f6..74747ba 100644
--- a/good.txt
+++ b/good.txt
@@ -5,3 +5,4 @@ UUUUUUU
ddddddd
eeeeeee
fffffff
+*******
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git diff HEAD^ good.txt
diff --git a/good.txt b/good.txt
index baf19f6..74747ba 100644
--- a/good.txt
+++ b/good.txt
@@ -5,3 +5,4 @@ UUUUUUU
ddddddd
eeeeeee
fffffff
+*******
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git diff
warning: LF will be replaced by CRLF in apple.txt.
The file will have its original line endings in your working directory.
diff --git a/apple.txt b/apple.txt
index 4c479de..656d67d 100644
--- a/apple.txt
+++ b/apple.txt
@@ -1 +1,2 @@
apple
+-----
diff --git a/good.txt b/good.txt
index baf19f6..74747ba 100644
--- a/good.txt
+++ b/good.txt
@@ -5,3 +5,4 @@ UUUUUUU
ddddddd
eeeeeee
fffffff
+*******