Git是目前世界上最先进的分布式版本控制系统。
工作原理 / 流程:

Workspace:工作区
Index / Stage:暂存区
Repository:仓库区(或本地仓库)
Remote:远程仓库
SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以首先要从中央服务器哪里得到最新的版本,然后干活,干完后,需要把自己做完的活推送到中央服务器。集中式版本控制系统是必须联网才能工作,如果在局域网还可以,带宽够大,速度够快,如果在互联网下,如果网速慢的话,就纳闷了。
Git是分布式版本控制系统,那么它就没有中央服务器的,每个人的电脑就是一个完整的版本库,这样,工作的时候就不需要联网了,因为版本都是在自己的电脑上。既然每个人的电脑都有一个完整的版本库,那多个人如何协作呢?比如说自己在电脑上改了文件A,其他人也在电脑上改了文件A,这时,你们两之间只需把各自的修改推送给对方,就可以互相看到对方的修改了。
官方地址:**https://git-scm.com/

选择下载64-bit Git for Windows Setup
Git Bash:Unix与Linux风格的命令行,使用最多,推荐最多
安装后配置用户名邮箱(必要)
> git config --global user.name "yxm" #配置用户名
> git config --global user.email "yxm@163.com" #配置邮箱
查询用户名邮箱
> git config --system --list #查询全局配置信息
> git config --global user.name #查看用户名
> git config --global user.email #查看邮箱
已提交(committed)、已修改(modified) 和 已暂存(staged)。
工作区域:就是你平时存放项目代码的地方
暂存区:保存了下次将要提交的文件列表信息,一般在 Git 仓库目录中。
仓库区(或本地仓库):就是安全存放数据的位置,这里面有你提交到所有版本的数据。
创建本地仓库的方法有两种:一种是创建全新的仓库,另一种是克隆远程仓库。
创建全新仓库 需要在管理的项目根目录执行,执行成功后会出现.git目录
git init
克隆远程仓库
git clone [url]
例如:
git clone https://gitee.com/yangxumin/js.git
Untracked: 未跟踪, 此文件在文件夹中, 但并没有加入到git库, 不参与版本控制. 通过git add 状态变为Staged.
Unmodify: 文件已经入库, 未修改, 即版本库中的文件快照内容与文件夹中完全一致. 这种类型的文件有两种去处, 如果它被修改, 而变为Modified. 如果使用git rm移出版本库, 则成为Untracked文件
Modified: 文件已修改, 仅仅是修改, 并没有进行其他的操作. 这个文件也有两个去处, 通过git add可进入暂存staged状态, 使用git checkout 则丢弃修改过, 返回到unmodify状态, 这个git checkout即从库中取出文件, 覆盖当前修改
Staged: 暂存状态. 执行git commit则将修改同步到库中, 这时库中的文件和本地文件又变为一致, 文件为Unmodify状态. 执行git reset HEAD filename取消暂存, 文件状态为Modified
git status [filename]
创建一个文件并查看状态
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ touch readme.txt
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ echo hello yxm > readme.txt
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ cat readme.txt
hello yxm
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ git status readme.txt
On branch master
No commits yet
Untracked files:
(use "git add ..." to include in what will be committed)
readme.txt
nothing added to commit but untracked files present (use "git add" to track)
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
文件状态Untracked files:未跟踪,说明文件没有参与版本控制,只是存放在文件夹中
git add 文件名 #添加单个文件
git add . #添加所有文件
git add readme.txt
git rm --cached 文件名 #直接从暂存区删除文件,工作区则不做出改变
git rm -f 文件名 #不但从暂存中删除,同时删除物理文件
rm 文件名 #删除文件
git diff 文件名
工作区创建b.txt文件,提交到暂存区,修改工作区b.txt文件的内容,查询文件修改后的差异
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ touch b.txt
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ echo hello 123 >b.txt
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ git add b.txt
warning: in the working copy of 'b.txt', LF will be replaced by CRLF the next time Git touches it
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ echo hello 456 >b.txt
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ git diff b.txt
warning: in the working copy of 'b.txt', LF will be replaced by CRLF the next time Git touches it
diff --git a/b.txt b/b.txt
index 257299b..3a3c3e2 100644
--- a/b.txt
+++ b/b.txt
@@ -1 +1 @@
-hello 123
+hello 456
如果仓库中已经存在文件b.txt,在工作区中对b.txt修改了,如果想撤销可以使用checkout,签出覆盖
签出命令git checkout是git最常用的命令之一,同时也是一个很危险的命令,因为这条命令会重写工作区
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ git checkout b.txt
Updated 1 path from the index
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ cat b.txt
hello 123
通过add只是将文件或目录添加到了index暂存区,使用commit可以实现将暂存区的文件提交到本地仓库。
git commit 文件名 -m "注释信息" #提交单个文件
git commit . -m "注释信息" #提交所有文件
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ git commit b.txt -m "first commit"
[master (root-commit) 0cf58fb] first commit
1 file changed, 1 insertion(+)
create mode 100644 b.txt
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ git status b.txt
On branch master
nothing to commit, working tree clean
、、、
git log
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ git log
commit 7142a00af8d147747f7031a3712fc51b1ecac688 (HEAD -> master)
Author: yxm <yxm3568@163.com>
Date: Thu Sep 8 15:23:00 2022 +0800
yxmcommit
commit 0cf58fb6dead7aec39f732df3810a94a8dcb5676
Author: yxm <yxm3568@163.com>
Date: Thu Sep 8 15:20:29 2022 +0800
first commit
git reset --hard HEAD~1

创建r.txt,文件,写入内容111第一次提交,修改r.txt文件,写入内容222添加222后提交,修改r.txt文件。写入333后提交,查看提示,回退。
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ touch r.txt
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ echo 111 >r.txt
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ git add r.txt
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ git commit r.txt -m 'r.txt submit'
[master a762292] r.txt submit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 r.txt
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ git add r.txt
warning: in the working copy of 'r.txt', LF will be replaced by CRLF the next time Git touches it
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ git commit r.txt -m "增加222提交"
warning: in the working copy of 'r.txt', LF will be replaced by CRLF the next time Git touches it
[master c31134c] 增加222提交
1 file changed, 2 insertions(+), 1 deletion(-)
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ echo 333 >> r.txt
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ git add r.txt
warning: in the working copy of 'r.txt', LF will be replaced by CRLF the next time Git touches it
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ git commit r.txt -m "增加333提交"
warning: in the working copy of 'r.txt', LF will be replaced by CRLF the next time Git touches it
[master 371c99a] 增加333提交
1 file changed, 1 insertion(+)
使用git log查看提交日志

r.txt文件内容为

回退到上一个版本222添加
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ git reset --hard HEAD^
HEAD is now at c31134c 增加222提交
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ cat r.txt
111
222
再恢复回退到最新的版本到添加333
使用命令方法如下:
git reset --hard 版本号
查看版本号
git reflog
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ git reflog
c31134c (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
371c99a HEAD@{1}: commit: 增加333提交
c31134c (HEAD -> master) HEAD@{2}: commit: 增加222提交
b6f59bf HEAD@{3}: commit: r.txt submit
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ git reset --hard 371c99a
HEAD is now at 371c99a 增加333提交
再次查看r.txt内容
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ cat r.txt
111
222
333
git branch #列出所有分支
git checkout -b [分支名字] #创建一个分支并切换到该分支
git merge [分支名字] # 合并指定分支到当前分支
git branch -d [分支名字] # 删除分支
创建dev分支,然后切换到dev分支上
git checkout -b dev
在dev分支操作r.txt,合并到master主分支
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (dev)
$ cat r.txt
111
222
333
444
555
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (dev)
$ git add r.txt
warning: in the working copy of 'r.txt', LF will be replaced by CRLF the next time Git touches it
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (dev)
$ git commit -m "dev branch"
[dev 4f3b785] dev branch
1 file changed, 2 insertions(+)
切换主分支并合并
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (dev)
$ git checkout master
Switched to branch 'master'
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ cat r.txt
111
222
333
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ git merge dev
Updating cf7e35d..4f3b785
Fast-forward
r.txt | 2 ++
1 file changed, 2 insertions(+)
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ cat r.txt
111
222
333
444
555
删除dev分支
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ git branch -d dev
Deleted branch dev (was 4f3b785).
yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
$ git branch
* master
Git代码托管平台,首先推荐的是GitHub,好多好的开源项目都来自GitHub,但是GitHub只能新建公开的Git仓库,私有仓库要收费,有时候访问比较卡,如果你做的是一个开源项目,可以首选GitHub。下面推荐几个比较好的Git代码托管平台:

已存在仓库
git remote add origin https://gitee.com/yangxumin/test.git
git push -u origin "master"
git pull 命令用于从远程获取代码并合并本地的版本
yxm@USER-20200712WE MINGW64 /d/yxm/testgit2
$ git init
Initialized empty Git repository in D:/yxm/testgit2/.git/
yxm@USER-20200712WE MINGW64 /d/yxm/testgit2 (master)
$ git pull https://gitee.com/yangxumin/test.git
