GIT使用—建立並使用遠程版本庫

遠程版本庫

(1)建立一個裸版本庫

[root@localhost tmp]# git init fluff2
Initialized empty Git repository in /tmp/fluff2/.git/
[root@localhost tmp]# ls
fluff2
[root@localhost tmp]# git init --bare fluff
Initialized empty Git repository in /tmp/fluff/
[root@localhost tmp]# ls
fluff  fluff2

(2)遠程版本庫

Git使用遠程版本庫和遠程追蹤分支來引用另外一個版本庫,並有助於與該版本庫創建鏈接。html

常見命令:git

  • git fetch 從遠程版本庫抓取對象及其相關的元數據
  • git pull 跟fetch相似,但合併修改到相應本地分支
  • git push 轉移對象及其相關的元數據到遠程版本庫
  • git ls-remote 顯示一個給定的遠程版本庫的引用列表

分支類別:vim

  • 遠程追蹤分支與遠程版本庫相關聯,專門用來追蹤遠程版本庫中每一個分支的變化。
  • 本地追蹤分支與遠程追蹤分支相配對。它是一種集成分支,用於收集本地開發和遠程追蹤分支中的變動。
  • 任何本地的非追蹤分支一般稱爲特性或開發分支。
  • 爲了完成命名空間,遠程分支是一個設在非本地的遠程版本庫的分支。

示例:
建立權威版本庫public_html.git
用一個初始版本庫填充Depotfetch

[root@localhost tmp]# cd Depot/
[root@localhost Depot]# git clone --bare /root/public_html public_html.git
Initialized empty Git repository in /tmp/Depot/public_html.git/
[root@localhost Depot]# ls
public_html.git

[root@localhost Depot]# cd /root/public_html/
[root@localhost public_html]# ls   #有工做目錄
foo.html  index.html  yes.html
[root@localhost public_html]# ls -aF
./  ../  foo.html  .git/  index.html  yes.html
[root@localhost public_html]# ls -aF .git
./   BISECT_ANCESTORS_OK  BISECT_NAMES  branches/       config       HEAD    index  logs/     ORIG_HEAD
../  BISECT_LOG           BISECT_START  COMMIT_EDITMSG  description  hooks/  info/  objects/  refs/
[root@localhost public_html]# cd /tmp/Depot/
[root@localhost Depot]# ls -aF public_html.git/   #沒有工做目錄
./  ../  branches/  config  description  HEAD  hooks/  info/  objects/  packed-refs  refs/

[root@localhost Depot]# cd public_html.git/
[root@localhost public_html.git]# cat config 
[core]
    repositoryformatversion = 0
    filemode = true
    bare = true

由於在克隆過程當中使用了--bare選項,因此Git沒有引入通常默認的origin遠程版本庫。url

製做本身的origin遠程版本庫3d

[root@localhost public_html.git]# cd /root/public_html/
[root@localhost public_html]# cat .git/config 
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[root@localhost public_html]# git remote add origin /tmp/Depot/public_html
[root@localhost public_html]# cat .git/config
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = /tmp/Depot/public_html
    fetch = +refs/heads/*:refs/remotes/origin/*

在遠程版本庫中創建新的遠程追蹤分支,表明來自遠程版本庫的分支,以完成創建origin遠程版本庫進程code

[root@localhost public_html]# git branch -a
* master
  newtest
  testing
[root@localhost public_html]# git remote update
Fetching origin
From /tmp/Depot/public_html
 * [new branch]      master     -> origin/master
 * [new branch]      newtest    -> origin/newtest
 * [new branch]      testing    -> origin/testing
[root@localhost public_html]# git branch -a
* master
  newtest
  testing
  remotes/origin/master  #遠程追蹤分支:掌握和跟蹤遠程版本苦苦master分支中的提交
  remotes/origin/newtest
  remotes/origin/testing

在版本庫中進行開發orm

[root@localhost public_html]# git show-branch -a
* [master] add test.txt
 ! [newtest] newtest yes
  ! [testing] newtest yes
   ! [origin/master] add test.txt
    ! [origin/newtest] newtest yes
     ! [origin/testing] newtest yes
------
 ++ ++ [newtest] newtest yes
 ++ ++ [newtest^] removed test.txt
*+++++ [master] add test.txt
[root@localhost public_html]# vim fuzzy.txt
[root@localhost public_html]# cat fuzzy.txt 
Fuzzy Wuzzy was a bear
Fuzzy Wuzzy had no hair
Fuzzy Wuzzy wasn't very fuzzy,
Was he?
[root@localhost public_html]# git add fuzzy.txt 
[root@localhost public_html]# git commit -m "add fuzzy"
[master 5571b42] add fuzzy
 1 files changed, 4 insertions(+), 0 deletions(-)
 create mode 100644 fuzzy.txt
[root@localhost public_html]# git show-branch -a
* [master] add fuzzy
 ! [newtest] newtest yes
  ! [testing] newtest yes
   ! [origin/master] add test.txt
    ! [origin/newtest] newtest yes
     ! [origin/testing] newtest yes
------
*      [master] add fuzzy
 ++ ++ [newtest] newtest yes
 ++ ++ [newtest^] removed test.txt
*+++++ [origin/master] add test.txt

推送變動htm

[root@localhost public_html]# git push origin master
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 362 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /tmp/Depot/public_html
   b0b257c..5571b42  master -> master

Git提取master分支的變動,將它們捆綁在一塊兒,發送到名爲origin的遠程版本庫中。對象

探測遠程版本庫並驗證是否更新

在本地
[root@localhost public_html]# cd /tmp/Depot/public_html.git/
[root@localhost public_html.git]# git show-branch
! [master] add fuzzy
 * [newtest] newtest yes
  ! [testing] newtest yes
---
+   [master] add fuzzy
 *+ [newtest] newtest yes
 *+ [newtest^] removed test.txt
+*+ [master^] add test.txt

在不一樣物理機
[root@localhost public_html.git]# git ls-remote origin
而後用git rev-parse HEAD或git show ID來展現那些與當前本地分支匹配的提交ID

添加新的開發人員

[root@localhost tmp]# mkdir bobo
[root@localhost tmp]# ls
bobo  Depot  fluff  fluff2
[root@localhost tmp]# cd bobo/
[root@localhost bobo]# ls
[root@localhost bobo]# git clone /tmp/Depot/public_html.git
Initialized empty Git repository in /tmp/bobo/public_html/.git/
[root@localhost bobo]# ls
public_html
[root@localhost bobo]# cd public_html/
[root@localhost public_html]# ls   #這裏由於這前創建origin時原版本庫在newtest分支上
foo.html  index.html  yes.html
[root@localhost public_html]# git branch
* newtest
[root@localhost public_html]# ls -aF
./  ../  foo.html  .git/  index.html  yes.html
[root@localhost public_html]# cd .git/
[root@localhost .git]# ls
branches  config  description  HEAD  hooks  index  info  logs  objects  packed-refs  refs
[root@localhost .git]# cat config 
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = /tmp/Depot/public_html.git
[branch "newtest"]   #此時有一個默認的遠程版本庫
    remote = origin
    merge = refs/heads/newtest
[root@localhost .git]# git remote show origin
* remote origin
  Fetch URL: /tmp/Depot/public_html.git
  Push  URL: /tmp/Depot/public_html.git
  HEAD branch (remote HEAD is ambiguous, may be one of the following):
    newtest
    testing
  Remote branches:
    master  tracked
    newtest tracked
    testing tracked
  Local branch configured for 'git pull':
    newtest merges with remote newtest
  Local ref configured for 'git push':
    newtest pushes to newtest (up to date)

[root@localhost public_html]# git branch -a
* newtest
  remotes/origin/HEAD -> origin/newtest  #遠程版本庫認爲的活動分支
  remotes/origin/master
  remotes/origin/newtest
  remotes/origin/testing

修改提交,推送到倉庫中的主版本庫

[root@localhost public_html]# cat yes.html 
AAAAAA
[root@localhost public_html]# vim yes.html 
[root@localhost public_html]# cat yes.html 
BBBBBBB

[root@localhost public_html]# git diff
diff --git a/yes.html b/yes.html
index b068058..6a4ca1b 100644
--- a/yes.html
+++ b/yes.html
@@ -1 +1 @@
-AAAAAA
+BBBBBBB
[root@localhost public_html]# git commit yes.html 
[newtest c24a693] change yes.html
 1 files changed, 1 insertions(+), 1 deletions(-)

[root@localhost public_html]# git push
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 253 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /tmp/Depot/public_html.git
   b488b19..c24a693  newtest -> newtest

獲取版本庫更新
(當其餘人更新了文件,這時你須要刷新克隆版本庫)

[root@localhost public_html]# git pull

pull意味着先執行fetch,而後執行merge或rebase。
push和fetch都負責在版本庫之間傳輸數據,但方向相反。

(3)圖解遠程版本庫開發週期
最初的開發

克隆產生兩個單獨的版本庫

交替的歷史記錄

獲取交替記錄

合併歷史記錄

推送合併後的歷史記錄

相關文章
相關標籤/搜索