Git工作模式
目錄
Git初始化
爲每臺電腦配置身份信息:
$ git config --global user.name "Your Name" $ git config --global user.email "[email protected]" 把某個目錄變成可以讓git管理到的目錄(創建版本庫) git init
Git基本命令
#把內容輸入到一個文件 echo "xxxx" > xxxx #把工作空間的某個文件添加到cache git add xxxx #把工作空間的所有內容添加到cache git add -A #把cache當中的某個文件提交到本地庫 git commit -m "xxxx" #all git commit -am "xxxx" #cache ---->work file恢復一個文件 file1 file2 恢復兩個文件 .恢復所有文件 git checkout readme.txt #git狀態查詢 git status #查看不同的地方 默認查看工作區和cache #git diff --cached 比較cache和Repository #git diff HEAD 工作區和最新的Resository #git diff commit-id 工作區和制定的repository #git diff --cached commit-id #git diff --commit-id commit-id git diff #清除工作區的指定文件的修改 git checkout -- 文件名 #清除工作區所有文件修改 git checkout . #reset 回到之前的版本 ^一個表示一個版本 顧名思義 (HEAD~100) git reset HEAD^ #回退n個版本 git reset HEAD~n #git的日誌 git log git log --pretty=oneline #oh my pretty pretty boy i love you git reflog 查看歷史命令 #git rm --cached file_path git rm git mv #遠程倉庫的克隆島本地庫 git clone #關聯遠程倉庫 git remote add github [email protected]:xxxxx git remote add centos7_vm1 [email protected]/home/git git clone [email protected]:/home/git #推送到遠程倉庫 git pusvue #如果沒有關聯遠程分支 git push --set-upstream origin xxx #拉取遠程倉庫的內容 git pull #查看當前分支 -a查看所有分支 -av 查看所有分支的信息 -avv 查看所有分支的信息和關係 #-v 查看本地分支 #遠程分支信息:./git/refs/remotes/origin/ 本地分支信息:./git/refs/heads/ git branch #創建一個分支 基於當前分支創建分支 git branch xxx #基於oldType創建分支 git branch newBranch oldType #切換分支 git checkout 分支的名字 #刪除分支 git branch -d xxx #查看文件內容 git cat-file -p commitid #查看對象類型 blob commit tree git cat-file -t commitid
Github
$ ssh-keygen -t rsa -C "email" //public key for push git remote add nickName gitUrl // conn remote git push -u remoteBranch localBranch
分支
- 查看分支
- 創建分支
基於當前分支創積分分支 基於遠程分支創建分支 基於新分支創建分支 基於提交創建分支 其實都是基於commit創建分支
- 合併分支
一定要切換到被合併的分支上去合併
比如說A要合併A1
那麼先要切換到A1,然後在A1上面執行merge
Git原理
find .get/objects -tpe f 查詢objects下面所有文件 git cat-file -t 文件名文件hash值 查詢文件類型 git cat-file -p 文件名文件hash值 反編譯文件中的數據
Git對象
在.git/objects文件介紹,Git是通過objects中的文件進行文件管控
- blob對象
存儲的是文件內容(壓縮的)。 文件名字是根據內容算出的一個hash值
- tree對象
多個blob對象的引用 子tree對象 文件名 文件類型
- commit對象
父commit 作者/提交者註釋 指向一個tree的指針
圖例:
git如何獲取當前分支?
在.git下有個HEAD文件,文件內容就是當前分支信息:ref: refs/heads/xx
在.git/refs文件介紹,heads文件進行分支管控,文件中的數據是最近一次commit的ID
上圖,相同的指針指向了不同文件,就是Git的精髓(不同分支一樣的內容都會指向同一個blob文件)。
HEAD 爲什麼要通過 refs/heads/master 中轉一下,而不是直接指向 master 分支的提交?
每個分支的頭指針都指向該分支的最新提交 但是此時出現一個 dev 分支,剛從 master 檢出,頭指針和 master 相同,HEAD直接指向 提交的話,怎麼知道,當前工作分支是 master 還是 dev 呢?
注:上面意思是,如果當前HEAD指向commitID,幾個分支都是最新提交commitID相同,此時如果提交我們HEAD應該指向哪個分支?
但是會進入一種特殊的狀態 detached HEAD。 detached HEAD,遊離的 HEAD 指針。 使用 git checkout <commit id> 成功的進入了 detached HEAD 狀態: 得出結論,當 HEAD 指針直接指向提交時,就會導致 detached HEAD 狀態。在這個狀態下, 如果創建了新提交,新提交不屬於任何分支。相對應的,現存的所有分支也不會受 detached HEAD 狀態提交的影響 example: 排查問題的時候,checkout 到懷疑的 commit 點上去做些測試,detached HEAD會保護你 的現有分支不受影響,測試完了不想保存直接 checkout 到其他地方,可以放棄修改。想保 存修改,可以創建一個 git checkout -b <new-branch-name> 新分支保存
遠程倉庫搭建
1.基於文件共享(nfs)
2.基於SSH搭建遠程倉庫
- git init --bare 初始化空的Git版本庫
- git remote add centos7_vm1 [email protected]/home/git
- git clone [email protected]:/home/git
- git push centos7_vm1 master
3.搭建Gitlab
https://about.gitlab.com/installation/#centos-7
傳上去gitlab-ce-10.8.2-ce.0.el7.x86_64.rpm
安裝 rpm -ivh /home/tools/gitlab-ce-10.8.2-ce.0.el7.x86_64.rpm
sudo gitlab-ctl reconfigure 時間很少8-10分鐘
gitlab-ctl start 啓動gitlab
訪問gitlab http://ip:port
設置密碼
QQ羣號:464512055
羣二維碼: