https://git-scm.com/
Git is a free and open source distributed version control system
designed to handle everything from small to very large projects with speed and efficiency.
版本控制是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统。
中文文档:https://git-scm.com/book/zh/v2
有一个单一的集中管理的服务器,保存所有文件的修订版本,而协同工作的人们都通过客户端连到这台服务器,取出最新的文件或者提交更新,如:SVN
客户端并不只提取最新版本的文件快照,而是把代码仓库完整地镜像下来。 这么一来,任何一处协同工作用的服务器发生故障,事后都可以用任何一个镜像出来的本地仓库恢复。 因为每一次的克隆操作,实际上都是一次对代码仓库的完整备份
下载地址:https://github.com/git-for-windows/git/releases/download/v2.25.1.windows.1/Git-2.25.1-64-bit.exe
安装选择默认配置即可
安装成功,打开GitBash窗口
类Linux指令窗口,可以执行任何linux指令
配置Git使用者的email和username
Administrator@OKDV0CSXRZHF64J MINGW64 ~
$ git config --global user.name 'gaozhy'
Administrator@OKDV0CSXRZHF64J MINGW64 ~
$ git config --global user.email '[email protected]'
Administrator@OKDV0CSXRZHF64J MINGW64 ~
$ git config --list
diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
http.sslbackend=openssl
http.sslcainfo=C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
core.autocrlf=true
core.fscache=true
core.symlinks=false
credential.helper=manager
user.name=gaozhy
[email protected]
工作区:工作区域或者工作目录
暂存区:临时区域
版本库:版本管理 记录若干文件内容变化
工作区文件 —>
add
—> 暂存区 —>commit
—> Git本地版本库(只有提交到版本库的文件才会追踪文件的变化)
local version database
Administrator@OKDV0CSXRZHF64J MINGW64 /e/GitProject
$ git init
Initialized empty Git repository in E:/GitProject/.git/
.git
目录是Git本地版本库的数据文件目录,注意不要修改这个目录中任何内容,如果这样做的话会破坏版本库数据
a.txt(工作区) ---> add 暂存区 ---> commit 版本库
Administrator@OKDV0CSXRZHF64J MINGW64 /e/GitProject (master)
$ git add a.txt
Administrator@OKDV0CSXRZHF64J MINGW64 /e/GitProject (master)
$ git commit -m 'first add a.txt'
[master (root-commit) a493b7e] first add a.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a.txt
可能有两种意图:
真的需要修改,将修改的内容提交到版本库
Administrator@OKDV0CSXRZHF64J MINGW64 /e/GitProject (master)
$ git add a.txt
Administrator@OKDV0CSXRZHF64J MINGW64 /e/GitProject (master)
$ git commit -m 'add Hello Hadoop in a.txt'
[master 474fe87] add Hello Hadoop in a.txt
1 file changed, 1 insertion(+)
Administrator@OKDV0CSXRZHF64J MINGW64 /e/GitProject (master)
$ git status
On branch master
nothing to commit, working tree clean
修改错了,丢弃工作区修改内容
Administrator@OKDV0CSXRZHF64J MINGW64 /e/GitProject (master)
$ git restore a.txt
展示提交日志详情
$ git log
commit 068e4a6d1d772bfac20c9c95e187a008aa248d28 (HEAD -> master)
Author: gaozhy
Date: Wed Feb 26 11:19:06 2020 +0800
add Hello Flink in a.txt
commit 474fe876476f90d6af9faa36717816572644a579
Author: gaozhy
Date: Wed Feb 26 11:17:33 2020 +0800
add Hello Hadoop in a.txt
commit a493b7ee776fe52fd5ba84b0ad8d39790b3bfc72 提交标识
Author: gaozhy 用户身份信息
Date: Wed Feb 26 10:43:24 2020 +0800 提交日期
first add a.txt 每一次提交的注释信息
展示提交日志简版
$ git log --pretty=oneline
# 提交标识
068e4a6d1d772bfac20c9c95e187a008aa248d28 (HEAD -> master) add Hello Flink in a.txt
474fe876476f90d6af9faa36717816572644a579 add Hello Hadoop in a.txt
a493b7ee776fe52fd5ba84b0ad8d39790b3bfc72 first add a.txt
回到过去
第三个历史版本—> 第二个历史版本
Administrator@OKDV0CSXRZHF64J MINGW64 /e/GitProject (master)
$ git log --pretty=oneline
068e4a6d1d772bfac20c9c95e187a008aa248d28 (HEAD -> master) add Hello Flink in a.txt
474fe876476f90d6af9faa36717816572644a579 add Hello Hadoop in a.txt
a493b7ee776fe52fd5ba84b0ad8d39790b3bfc72 first add a.txt
Administrator@OKDV0CSXRZHF64J MINGW64 /e/GitProject (master)
$
Administrator@OKDV0CSXRZHF64J MINGW64 /e/GitProject (master)
$ git reset --hard 474fe876476f90d6af9faa36717816572644a579
HEAD is now at 474fe87 add Hello Hadoop in a.txt
回到将来
第一个历史版本—> 第三个历史版本
Administrator@OKDV0CSXRZHF64J MINGW64 /e/GitProject (master)
$ git reset --hard 068e4a6d1d772bfac20c9c95e187a008aa248d28
HEAD is now at 068e4a6 add Hello Flink in a.txt
Administrator@OKDV0CSXRZHF64J MINGW64 /e/GitProject (master)
# 展示历史的所有提交日志
$ git reflog
a493b7e (HEAD -> master) HEAD@{0}: reset: moving to a493b7ee776fe52fd5ba84b0ad8d39790b3bfc72
068e4a6 HEAD@{1}: reset: moving to 068e4a6d1d772bfac20c9c95e187a008aa248d28
a493b7e (HEAD -> master) HEAD@{2}: reset: moving to a493b7ee776fe52fd5ba84b0ad8d39790b3bfc72
474fe87 HEAD@{3}: reset: moving to 474fe876476f90d6af9faa36717816572644a579
068e4a6 HEAD@{4}: commit: add Hello Flink in a.txt
474fe87 HEAD@{5}: commit: add Hello Hadoop in a.txt
a493b7e (HEAD -> master) HEAD@{6}: commit (initial): first add a.txt
Administrator@OKDV0CSXRZHF64J MINGW64 /e/GitProject (master)
$ git reset --hard 068e4a6
HEAD is now at 068e4a6 add Hello Flink in a.txt
add —> commit
工作区修改但没有提交到暂存区
$ git status
On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: a.txt
no changes added to commit (use "git add" and/or "git commit -a")
Administrator@OKDV0CSXRZHF64J MINGW64 /e/GitProject (master)
$ git restore a.txt
修改已添加到暂存区
Administrator@OKDV0CSXRZHF64J MINGW64 /e/GitProject (master)
$ git restore --staged a.txt
Administrator@OKDV0CSXRZHF64J MINGW64 /e/GitProject (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: a.txt
no changes added to commit (use "git add" and/or "git commit -a")
Administrator@OKDV0CSXRZHF64J MINGW64 /e/GitProject (master)
$ git restore a.txt
修改已提交到版本库
参阅:版本回退
$ git rm a.txt
rm 'a.txt'
Administrator@OKDV0CSXRZHF64J MINGW64 /e/GitProject (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged ..." to unstage)
deleted: a.txt
Administrator@OKDV0CSXRZHF64J MINGW64 /e/GitProject (master)
$ git commit -m 'deleted a.txt'
[master 163ca8b] deleted a.txt
1 file changed, 2 deletions(-)
delete mode 100644 a.txt
Administrator@OKDV0CSXRZHF64J MINGW64 /e/GitProject (master)
$ git status
On branch master
nothing to commit, working tree clean
Administrator@OKDV0CSXRZHF64J MINGW64 /e/GitProject (master)
$ git log --pretty=oneline
163ca8b73993f9b4be328fa8cd0144ebb20e90e3 (HEAD -> master) deleted a.txt
068e4a6d1d772bfac20c9c95e187a008aa248d28 add Hello Flink in a.txt
474fe876476f90d6af9faa36717816572644a579 add Hello Hadoop in a.txt
a493b7ee776fe52fd5ba84b0ad8d39790b3bfc72 first add a.txt
Administrator@OKDV0CSXRZHF64J MINGW64 /e/GitProject (master)
$ git reset --hard 068e4a6d1d772bfac20c9c95e187a008aa248d28
HEAD is now at 068e4a6 add Hello Flink in a.txt
本地版本库的一种远程备份,通过远程仓库也可以实现代码的共享和协同开发;
GitHub(Git版本库的一个托管网站),码云,GitLab(开源项目,公司内部搭建的Git版本库托管服务)等;
https://github.com/
$ ssh-keygen.exe -t rsa -C '[email protected]'
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):
Created directory '/c/Users/Administrator/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Administrator/.ssh/id_rsa
Your public key has been saved in /c/Users/Administrator/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:/NH+fkUbVAZO5gOISs6rJ+OKNizbVEgcb4Fee1bEeXE [email protected]
The key's randomart image is:
+---[RSA 3072]----+
| ... o.o.oE +.+|
| ..o... = o. * o |
| .o.o= o . = |
| ..o. *. . o.|
| . .o .S . . .o|
| . . . o ..|
|. . . . . .|
|.B + . . .|
|=.+o.+ .o. |
+----[SHA256]-----+
Administrator@OKDV0CSXRZHF64J MINGW64 ~
$ cd .ssh/
Administrator@OKDV0CSXRZHF64J MINGW64 ~/.ssh
$ ls
id_rsa id_rsa.pub
$ cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDdFiynJ2UpETRFPSGjJd/Wfud+PYmp9laCYqfRrRocTo9Ji+eWUHWAosDsP/JPA87L6DUENynzDwLVQ22P8WqG9GFUeR5qV9nLspqqby5hlYIhOJ12qg3d943I4vPL6290KxClCJsY0n4nCrnMp6gMK0oi7cdxPALbxzx0AJ7xTugruG/Ejh9AnvkOvr8h9uWy0Lj91fkcg2HbYQSBxG9J/tT0rLDYUeZJdeiJ9rtwIJc8e8X7Pcj8z/u93Jpbf4M+gUk9XQCT+L8hI64ci6ahp9UeTc4ppKKWOUJ03z5x8ndlGvUTxFqViq17uVd
建立GitHub的远程版本库
关联本地库和远程库
Administrator@OKDV0CSXRZHF64J MINGW64 /e/GitProject (master)
$ git remote add origin https://github.com/gaozhy520/170.git
将本地库push到远程库
$ git push -u origin master
Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 6 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (9/9), 673 bytes | 224.00 KiB/s, done.
Total 9 (delta 0), reused 0 (delta 0)
To https://github.com/gaozhy520/170.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
Administrator@OKDV0CSXRZHF64J MINGW64 /e
$ git clone https://github.com/gaozhy520/170.git
Cloning into '170'...
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 9 (delta 0), reused 9 (delta 0), pack-reused 0
Unpacking objects: 100% (9/9), 653 bytes | 65.00 KiB/s, done.
Administrator@OKDV0CSXRZHF64J MINGW64 /e/170 (master)
$ git pull origin master
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 670 bytes | 167.00 KiB/s, done.
From https://github.com/gaozhy520/170
* branch master -> FETCH_HEAD
068e4a6..77224fe master -> origin/master
Updating 068e4a6..77224fe
Fast-forward
README.md | 1 +
1 file changed, 1 insertion(+)
create mode 100644 README.md
Administrator@OKDV0CSXRZHF64J MINGW64 /e/170 (master)
$ git log --pretty=oneline
77224fed2bcddb6be8e609db1505f655f29abd98 (HEAD -> master, origin/master, origin/HEAD) Create README.md
068e4a6d1d772bfac20c9c95e187a008aa248d28 add Hello Flink in a.txt
474fe876476f90d6af9faa36717816572644a579 add Hello Hadoop in a.txt
a493b7ee776fe52fd5ba84b0ad8d39790b3bfc72 first add a.txt
Administrator@OKDV0CSXRZHF64J MINGW64 /e/170 (master)
$ git pull origin master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), 235 bytes | 47.00 KiB/s, done.
From https://github.com/gaozhy520/170
* branch master -> FETCH_HEAD
0ce8c08..47d4e96 master -> origin/master
Updating 0ce8c08..47d4e96
Fast-forward
b.txt | 1 +
1 file changed, 1 insertion(+)
不同人修改了相同的文件,在提交记录时产生版本冲突;
Administrator@OKDV0CSXRZHF64J MINGW64 /e/170 (master)
$ git push origin master
fatal: HttpRequestException encountered.
▒▒▒▒▒▒▒▒ʱ▒▒▒▒
To https://github.com/gaozhy520/170.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/gaozhy520/170.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Administrator@OKDV0CSXRZHF64J MINGW64 /e/170 (master)
$ git pull origin master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 652 bytes | 163.00 KiB/s, done.
From https://github.com/gaozhy520/170
* branch master -> FETCH_HEAD
47d4e96..77e9cb3 master -> origin/master
Auto-merging b.txt
CONFLICT (content): Merge conflict in b.txt
Automatic merge failed; fix conflicts and then commit the result.
Administrator@OKDV0CSXRZHF64J MINGW64 /e/170 (master|MERGING)
$ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add ..." to mark resolution)
both modified: b.txt
no changes added to commit (use "git add" and/or "git commit -a")
Administrator@OKDV0CSXRZHF64J MINGW64 /e/170 (master|MERGING)
$ git add b.txt
Administrator@OKDV0CSXRZHF64J MINGW64 /e/170 (master|MERGING)
$ git commit -m 'resovle b.txt conflict'
[master cee08ee] resovle b.txt conflict
Administrator@OKDV0CSXRZHF64J MINGW64 /e/170 (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
Administrator@OKDV0CSXRZHF64J MINGW64 /e/170 (master)
$ git push origin master
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 6 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 540 bytes | 540.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 1 local object.
To https://github.com/gaozhy520/170.git
77e9cb3..cee08ee master -> master
在版本回退里,你已经知道,每次提交,Git都把它们串成一条时间线,这条时间线就是一个分支。截止到目前,只有一条时间线,在Git里,这个分支叫主分支,即master
分支。HEAD
严格来说不是指向提交,而是指向master
,master
才是指向提交的,所以,HEAD
指向的就是当前分支。
一开始的时候,master
分支是一条线,Git用master
指向最新的提交,再用HEAD
指向master
,就能确定当前分支,以及当前分支的提交点:
每次提交,master
分支都会向前移动一步,这样,随着你不断提交,master
分支的线也越来越长。
当我们创建新的分支,例如dev
时,Git新建了一个指针叫dev
,指向master
相同的提交,再把HEAD
指向dev
,就表示当前分支在dev
上:
你看,Git创建一个分支很快,因为除了增加一个dev
指针,改改HEAD
的指向,工作区的文件都没有任何变化!
不过,从现在开始,对工作区的修改和提交就是针对dev
分支了,比如新提交一次后,dev
指针往前移动一步,而master
指针不变:
假如我们在dev
上的工作完成了,就可以把dev
合并到master
上。Git怎么合并呢?最简单的方法,就是直接把master
指向dev
的当前提交,就完成了合并:
所以Git合并分支也很快!就改改指针,工作区内容也不变!
合并完分支后,甚至可以删除dev
分支。删除dev
分支就是把dev
指针给删掉,删掉后,我们就剩下了一条master
分支:
Administrator@OKDV0CSXRZHF64J MINGW64 /e/170 (master)
$ git checkout -b dev
Switched to a new branch 'dev'
Administrator@OKDV0CSXRZHF64J MINGW64 /e/170 (dev)
Administrator@OKDV0CSXRZHF64J MINGW64 /e/170 (dev)
$ git branch
* dev
master
Administrator@OKDV0CSXRZHF64J MINGW64 /e/170 (dev)
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
Administrator@OKDV0CSXRZHF64J MINGW64 /e/170 (master)
$ git merge dev
Updating cee08ee..53483f0
Fast-forward
c.txt | 0
d.txt | 0
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 c.txt
Administrator@OKDV0CSXRZHF64J MINGW64 /e/170 (master)
$ git branch -d feture
Deleted branch feture (was 53483f0).
Administrator@OKDV0CSXRZHF64J MINGW64 /e/170 (master)
$ git branch
dev
* master
Administrator@OKDV0CSXRZHF64J MINGW64 /e/170 (master)
$ git branch -d dev
Deleted branch dev (was 53483f0).