版本管理的演變
VCS出現以前
- 用目錄拷貝區別不同版本
- 公告文件容易被覆蓋
- 成員溝通成本很高,代碼繼承效率低下。
集中式VCS
- 有集中的版本管理服務器
- 具有文件版本管理和分支管理能力
- 繼承效率有明顯提高
- 客戶端必須時刻和服務器相連
分佈式 VCS
- 服務端和客戶端都有完整的版本庫
- 脫離服務端,客戶端照樣可以管理版本
- 查看歷史和版本比較等多數操作,都不需要訪問服務器,比集中式VCS更能提高版本管理效率。
GIT
配置user信息
git config --global user.name ‘your_name’
git config --global user.email ‘[email protected]’
why do this?
項目變更信息和開發者信息捆綁在一起,當評審人員指出代碼問題之後git系統能很方便的發送郵件給對應的變更者。
config的三個作用域
缺省等同於local
- –local只對某個倉庫有效
- –global對所有倉庫有效
- –system對系統所有登陸用戶有效
顯示config的配置,加–list
- git config --list --local
- git config --list --global
- git config --list --system
修改配置:直接覆蓋。
建GIT倉庫
- git add files :將工作目錄的內容提交到暫存區
- git commit :將暫存區的內容正式提交到版本歷史
- git init
- git status
- git add 文件名1,文件名2…/-A # 全部
- git commit -m “註釋信息”
- git log # 查看歷史 包括:提交的id號,變更者信息、變更時間。
修改文件名
- 在工作區重命名
mv old_file_name new_file_name
通過git status可以看出,我們刪除了一個名爲:python.png的文件,新增了一個名爲python.jpg的文件。
根據提示
- 把變更的文件添加到暫存區
git add python.jpg
- 刪除掉原來的文件
git rm python.png
查看下git的狀態:
git status
結果:
renamed: python.png -> python.jpg
but mv 、git add、git rm 並不可取!!!
建議使用:
git mv old_FileName new_FileName
不需要暫存區直接commit
git commit -am '註釋信息'
git log
以 版本號 註釋信息 的形式顯示
- git log --oneline
指定最近的幾個commit
- git log -n3
- git log -n3 --oneline
多個分支(1.當前分支commit 2.所有分支commit 3. 圖形化展示所有分支commit)
- git log
- git log --all
- git log --all --graph
- git log --oneline --all
- git log --oneline --all -n3
- git log --oneline --all -n4 --graph
其他參數接口:git help --web log
圖形界面版本管理工具
略…
探祕 .git目錄
1.進入.git目錄
- cd .git
- ls -al
drwxr-xr-x 1 11014 197609 0 5月 14 16:46 ./
drwxr-xr-x 1 11014 197609 0 5月 14 16:21 …/
-rw-r–r-- 1 11014 197609 24 5月 14 16:22 COMMIT_EDITMSG
-rw-r–r-- 1 11014 197609 173 5月 14 16:46 config
-rw-r–r-- 1 11014 197609 73 5月 14 15:20 description
-rw-r–r-- 1 11014 197609 43 5月 14 16:43 GITGUI_MSG
-rw-r–r-- 1 11014 197609 21 5月 14 16:24 HEAD
drwxr-xr-x 1 11014 197609 0 5月 14 15:20 hooks/
-rw-r–r-- 1 11014 197609 217 5月 14 16:22 index
drwxr-xr-x 1 11014 197609 0 5月 14 15:20 info/
drwxr-xr-x 1 11014 197609 0 5月 14 15:24 logs/
drwxr-xr-x 1 11014 197609 0 5月 14 16:22 objects/
drwxr-xr-x 1 11014 197609 0 5月 14 15:20 refs/
1. HEAH
- cat HEAD 查看HEAD內容
ref: refs/heads/temp
ref引用,指向temp。說明當前所在分支爲:temp
- git checkout master
- cat HEAD
ref: refs/heads/master
說明當前分支爲:master
2. config
cat config
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
ignorecase = true
[gui]
wmstate = normal
geometry = 1640x853+800+377 360 389
有一點沒明白
- git --list
能看見user的配置信息,config文件中卻沒有。
3. refs
- heads/
- tags
存放項目開發”里程碑“(tags)and heads(存放各分支文件,而分支文件(如master)存放的是該分支指針指向的commit()
$ cat master
24dfdb836f8e85cdce5227eaafadb8200f542359
這一串是什麼玩意兒?
查看其類型:
$ git cat-file -t 24dfdb836f8e85cd
commit
4. objects
drwxr-xr-x 1 11014 197609 0 5月 14 16:22 ./
drwxr-xr-x 1 11014 197609 0 5月 14 17:13 …/
drwxr-xr-x 1 11014 197609 0 5月 14 16:22 15/
drwxr-xr-x 1 11014 197609 0 5月 14 16:22 24/
drwxr-xr-x 1 11014 197609 0 5月 14 15:24 42/
drwxr-xr-x 1 11014 197609 0 5月 14 15:22 6a/
drwxr-xr-x 1 11014 197609 0 5月 14 15:24 b1/
drwxr-xr-x 1 11014 197609 0 5月 14 15:22 cd/
drwxr-xr-x 1 11014 197609 0 5月 14 15:20 info/
drwxr-xr-x 1 11014 197609 0 5月 14 15:20 pack/
三個核心對象:
- commit
- tree
- blob
查看:
cd l5
$ ls -al
total 1
drwxr-xr-x 1 11014 197609 0 5月 14 15:24 ./
drwxr-xr-x 1 11014 197609 0 5月 14 16:22 ../
-r--r--r-- 1 11014 197609 129 5月 14 15:24 20d05524be40305a7f88c5b917b03143c55dc6
# 查看類型:-t
# 15+20d05524be40305a7f88c5b917b03143c55dc6組成一個hash.
$ git cat-file -t 15e8c980eccd586f3e6e92cd54e3d2b19e03ba8b
tree # 類型爲:tree
# 查看內容:-p
$ git cat-file -p 15e8c980eccd586f3e6e92cd54e3d2b19e03ba8b
100644 blob 6ad2a17c4e789bc75950aa499a7601077b908bbc caotu.jpg
100644 blob cdf5238ddf9754bfbaa8f13ccb6f125815b31f9a index.html
# 查看blob內容:
git cat-file -p cdf5238ddf9754bfbaa
# 內容爲index。html源碼
略...
其他文件夾查看方式一樣,類型可能會是commit。
- 一個commit ------>至少一棵樹
- 樹 ----------->blob+其他樹(子樹?隨便叫吧)
- 一個blob------>相同類型文件
# 初始化一個名爲watch_git_objects的倉庫
$ git init watch_git_objects
Initialized empty Git repository in C:/Users/11014/Desktop/watch_git_objects/.git/
# 進入倉庫
$ cd watch_git_objects/
$ cd .git
$ ls -al
total 3
drwxr-xr-x 1 11014 197609 0 5月 14 20:21 ./
drwxr-xr-x 1 11014 197609 0 5月 14 20:21 ../
-rw-r--r-- 1 11014 197609 112 5月 14 20:21 config
-rw-r--r-- 1 11014 197609 73 5月 14 20:21 description
-rw-r--r-- 1 11014 197609 23 5月 14 20:21 HEAD
drwxr-xr-x 1 11014 197609 0 5月 14 20:21 hooks/
drwxr-xr-x 1 11014 197609 0 5月 14 20:21 info/
drwxr-xr-x 1 11014 197609 0 5月 14 20:21 objects/
drwxr-xr-x 1 11014 197609 0 5月 14 20:21 refs/
$ cat HEAD
ref: refs/heads/master
# 查看objects/中類信息
$ cd objects/
$ ls -al
total 0
drwxr-xr-x 1 11014 197609 0 5月 14 20:21 ./
drwxr-xr-x 1 11014 197609 0 5月 14 20:21 ../
drwxr-xr-x 1 11014 197609 0 5月 14 20:21 info/
drwxr-xr-x 1 11014 197609 0 5月 14 20:21 pack/
# 創建文件
$ mkdir text
$ cd text/
$ echo "hello git" > 1.txt
$ cat 1.txt
hello git
# 將文件添加到暫存區
$ git add ./
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: text/1.txt
# 查看objects/中類信息,新增了一個8d
$ cd .git/objects
$ ls -al
total 0
drwxr-xr-x 1 11014 197609 0 5月 14 20:25 ./
drwxr-xr-x 1 11014 197609 0 5月 14 20:26 ../
drwxr-xr-x 1 11014 197609 0 5月 14 20:25 8d/
drwxr-xr-x 1 11014 197609 0 5月 14 20:21 info/
drwxr-xr-x 1 11014 197609 0 5月 14 20:21 pack/
# 切換到8d
$ cd 8d
$ ls -al
total 1
drwxr-xr-x 1 11014 197609 0 5月 14 20:25 ./
drwxr-xr-x 1 11014 197609 0 5月 14 20:25 ../
-r--r--r-- 1 11014 197609 26 5月 14 20:25 0e41234f24b6da002d962a26c2495ea16a425f
#
$ git cat-file -t 8d0e41234f24b6da002d962a26c2495ea16a425f
blob
#
$ git cat-file -p 8d0e41234f24b6da002d962a26c2495ea16a425f
hello git
11014@DESKTOP-O6IER3U MINGW64 ~/Desktop/watch_git_objects (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: text/1.txt
$ git commit -m "ADD file"
[master (root-commit) 6a5767b] ADD file
1 file changed, 1 insertion(+)
create mode 100644 text/1.txt
$ cd .git/objects/
$ ls -al
total 0
drwxr-xr-x 1 11014 197609 0 5月 14 20:29 ./
drwxr-xr-x 1 11014 197609 0 5月 14 20:29 ../
drwxr-xr-x 1 11014 197609 0 5月 14 20:29 21/
drwxr-xr-x 1 11014 197609 0 5月 14 20:29 6a/
drwxr-xr-x 1 11014 197609 0 5月 14 20:25 8d/
drwxr-xr-x 1 11014 197609 0 5月 14 20:29 f8/
drwxr-xr-x 1 11014 197609 0 5月 14 20:21 info/
drwxr-xr-x 1 11014 197609 0 5月 14 20:21 pack/
$ cd 21
$ ls -al
total 1
drwxr-xr-x 1 11014 197609 0 5月 14 20:29 ./
drwxr-xr-x 1 11014 197609 0 5月 14 20:29 ../
-r--r--r-- 1 11014 197609 46 5月 14 20:29 843073d2e60dcb60e98ed7dc2015f797ac44b4
# 查看類型/查看內容
$ git cat-file -t 21843073d2e60dcb60e98ed7dc2015f797ac44b4
tree
$ git cat-file -p 21843073d2e60dcb60e98ed7dc2015f797ac44b4
040000 tree f8ffdc98e18830606c869207793d6dde378d7834 text
$ cd 6a
$ ls
5767b193335e6a6569fd18d437ac25443cb748
$ git cat-file -t 6a5767b193335e6a6569fd18d437ac25443cb748
commit
$ git cat-file -p 6a5767b193335e6a6569fd18d437ac25443cb748
tree 21843073d2e60dcb60e98ed7dc2015f797ac44b4
author xyx <xieyaxi123@outlook.com> 1557836994 +0800
committer xyx <xieyaxi123@outlook.com> 1557836994 +0800
ADD file
$ cd ../f8
$ ls
ffdc98e18830606c869207793d6dde378d7834
$ git cat-file -t f8ffdc98e18830606c869207793d6dde378d7834
tree**加粗樣式**
$ git cat-file -p f8ffdc98e18830606c869207793d6dde378d7834
100644 blob 8d0e41234f24b6da002d962a26c2495ea16a425f 1.txt
小結:
- 當add到暫存區一個文件時---->blob
- commit --------->commit+tree
分離頭指針(detached HEAD)
實質:我們當前工作在一個沒有分支的環境下。
可能時不小心,切換分支的時候切換到了一個commit或者是其他不存在分支的情況下。
**缺點:**當我們在分離頭指針的情況下做分支,checkout到其他分支時,這個沒有和分支掛鉤的內容會被git當垃圾處理掉。
**優點:**當我們嘗試性做一些變更,變更之後的效果並不理想——只需要切換到其它分支,這些變更將會被git自動丟棄。
**需要:**按提示新建分支就好。
HEAD---->某個分支。
HEAD---->某個commit---->分離頭指針
- 直接創建分支
- 基於某個分支創建新分支---->從某個分支膚質除一個新分支,並切換到新分支。