git - 1.基础

配置

C:\Users\Administrator
λ git config --global user.name 'tangsansan'

C:\Users\Administrator
λ git config --global user.email 'tangge@vip.qq.com'

config 三个作用域

建设仓库

$ git init aurelia_learning

$ cd aurelia_learning

$ dir -all
total 8
drwxr-xr-x 1 Administrator 197121 0 四月   26 22:23 .
drwxr-xr-x 1 Administrator 197121 0 四月   26 22:23 ..
drwxr-xr-x 1 Administrator 197121 0 四月   26 22:23 .git   ## 这里是.git文件夹

## 为了区别local和global,在这里设置
$ git config --local  user.name 'tangge'

$ git config --local --list
user.name=tangge
...

$ git config --global --list
user.name='tangsansan'
user.email='tangge@vip.qq.com'
...

命令

创建仓库:git init

状态:git status

添加:git add

git add -A 提交所有变化
git add -u 提交被修改(modified)和被删除(deleted)文件,不包括新文件(new)
git add . 提交新文件(new)和被修改(modified)文件,不包括被删除(deleted)文件

日志:git log

提交: git commit -m'Add aurelia files'

删除:git rm

查看工作区和版本库里面最新版本的区别:git diff HEAD -- readme.txt

撤销暂存区:git reset HEAD file

撤销修改: git checkout -- file ,这里区别于git checkout 是切换分支
查看内容:cat <file>

重命名

$ mv readme.txt readme.MD


这里需要

Administrator@Y430P-TG MINGW64 /e/10.【Git】/git_learning (master)
$ git add readme.MD

Administrator@Y430P-TG MINGW64 /e/10.【Git】/git_learning (master)
$ git rm readme.txt
rm 'readme.txt'

这时候可以commit提交了

日志

当前所有详细的log

$ git log
commit 9ef0016f28654fb5bea7165b013e439c7be7d2c0 (HEAD -> master)
Author: tangsansan <tangge@vip.qq.com>
Date:   Sat Apr 27 11:32:41 2019 +0800

    rename

commit 0c80384267000ea9d0b4f89c3f5ba933a25f8ba0
Author: tangsansan <tangge@vip.qq.com>
Date:   Sat Apr 27 10:53:49 2019 +0800

    modified readme.txt

commit 3624b3a1574ee44cf529511287a61f990f5ae86e
Author: tangsansan <tangge@vip.qq.com>
Date:   Sat Apr 27 10:32:53 2019 +0800

    Add readme.txt

只看日志名

$ git log --oneline
9ef0016 (HEAD -> master) rename
0c80384 modified readme.txt
3624b3a Add readme.txt

查看最近的2个

$ git log -n2
commit 9ef0016f28654fb5bea7165b013e439c7be7d2c0 (HEAD -> master)
Author: tangsansan <tangge@vip.qq.com>
Date:   Sat Apr 27 11:32:41 2019 +0800

    rename

commit 0c80384267000ea9d0b4f89c3f5ba933a25f8ba0
Author: tangsansan <tangge@vip.qq.com>
Date:   Sat Apr 27 10:53:49 2019 +0800

    modified readme.txt

所有分支日志,图形化

$ git log --all --graph

Commit、tree和blob三个对象之间的关系

# 1.查看日志
$ git log    
...
# 省略
...

commit 3624b3a1574ee44cf529511287a61f990f5ae86e
Author: tangsansan <tangge@vip.qq.com>
Date:   Sat Apr 27 10:32:53 2019 +0800

    Add readme.txt

# 2.查看commit
$ git cat-file -p 3624b3a1574e
tree 10024464a9c1e1ddf17b6c144d1d99a62042d1fd
author tangsansan <tangge@vip.qq.com> 1556332373 +0800
committer tangsansan <tangge@vip.qq.com> 1556332373 +0800

Add readme.txt

# 3.查看 tree下的blob 10024464a9c1e1ddf17b6c144d1d99a62042d1fd
$ git cat-file -p  10024464a9c1e1ddf
100644 blob 013b5bc866a4c08e10047dbd1e83c2a3a83e0f40    readme.txt

# 4.再看blob内容
$ git cat-file -p  013b5bc866a4c08e1004
Git is a distributed version control system.
Git is free software.

管理修改

第一步,对readme.md做一个修改,比如加一行内容:

$ cat readme.MD
Git is a distributed version control system.
Git is free software.
Add the text
Git tracks changes.

添加

$ git add readme.MD
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   readme.MD

然后,再修改readme.md:

Git is a distributed version control system.
Git is free software.
Add the text
Git tracks changes of files.

提交

$ git commit -m "git tracks changes"
[master bd38a44] git tracks changes
 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 checkout -- <file>..." to discard changes in working directory)

        modified:   readme.MD

no changes added to commit (use "git add" and/or "git commit -a")

还是报错!!!

第一次修改 -> git add -> 第二次修改 -> git commit

你看,我们前面讲了,Git管理的是修改,当你用git add命令后,在工作区的第一次修改被放入暂存区,准备提交,但是,在工作区的第二次修改并没有放入暂存区,所以,git commit只负责把暂存区的修改提交了,也就是第一次的修改被提交了,第二次的修改不会被提交。
提交后,用git diff HEAD -- readme.MD命令可以查看工作区和版本库里面最新版本的区别:

$ git diff HEAD -- readme.MD
warning: LF will be replaced by CRLF in readme.MD.
The file will have its original line endings in your working directory
diff --git a/readme.MD b/readme.MD
index f0abcb0..0157864 100644
--- a/readme.MD
+++ b/readme.MD
@@ -1,4 +1,4 @@
 Git is a distributed version control system.
 Git is free software.
 Add the text
-Git tracks changes.
+Git tracks changes of files.


那怎么提交第二次修改呢?你可以继续git add再git commit,也可以别着急提交第一次修改,先git add第二次修改,再git commit,就相当于把两次修改合并后一块提交了:

第一次修改 -> git add -> 第二次修改 -> git add -> git commit

好,现在,把第二次修改提交了,然后开始小结。

撤销修改

修改在工作区未commit

你添加了1行

$ cat readme.md
Git is a distributed version control system.
Git is free software.
Add the text
Git tracks changes of files.
My stupid boss still prefers SVN.

在你准备提交前,一杯咖啡起了作用,你猛然发现了“stupid boss”可能会让你丢掉这个月的奖金!
你可以发现,Git会告诉你,git checkout -- file可以丢弃工作区的修改:

$ git checkout -- readme.MD
$ git status
On branch master
nothing to commit, working tree clean

把readme.MD文件在工作区的修改全部撤销,这里有两种情况:
一种是readme.MD自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;
一种是readme.MD已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。
总之,就是让这个文件回到最近一次git commitgit add时的状态。
git checkout -- file命令中的--很重要,没有--,就变成了“切换到另一个分支”的命令,我们在后面的分支管理中会再次遇到git checkout命令。

修改在暂存区,未commit

下面看第2种情况,已经提交到暂存区。

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   readme.MD

Git同样告诉我们,用命令git reset HEAD file可以把暂存区的修改撤销掉(unstage),重新放回工作区:

$ git reset HEAD reademe.MD
Unstaged changes after reset:
M       readme.MD


$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   readme.MD

no changes added to commit (use "git add" and/or "git commit -a")

现在已经到工作区了,还记得如何丢弃工作区的修改吗?

$ git checkout -- readme.MD

$ git status
On branch master
nothing to commit, working tree clean

OK了。

分离头指针 detached HEAD

变更没有基于 branch。

参考:

Git教程 - 廖雪峰的官方网站
极客时间:玩转Git三剑客

posted @ 2019-04-26 21:47  【唐】三三  阅读(315)  评论(0编辑  收藏  举报