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 initgit

狀態:git statussql

添加:git addshell

git add -A 提交全部變化
git add -u 提交被修改(modified)和被刪除(deleted)文件,不包括新文件(new)
git add . 提交新文件(new)和被修改(modified)文件,不包括被刪除(deleted)文件vim

日誌:git log網站

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

刪除:git rm日誌

查看工做區和版本庫裏面最新版本的區別:git diff HEAD -- readme.txtcode

撤銷暫存區:git reset HEAD fileserver

撤銷修改: 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三劍客

相關文章
相關標籤/搜索