天天看點

git入門三(遠端、标簽)

  分布式版本控制管理系統本地倉庫和中心伺服器倉庫資料是本地的鏡像倉庫,中心伺服器資料倉庫的是為了多使用者資料合并和擷取同步的中心,多人協作需要管理這些遠端倉庫,以便推送和拉去資料,彙總各自項目的進度和工作成果。管理遠端倉庫的工作添加遠端庫,廢棄遠端庫,管理遠端分支管理等等。每次使用者從中心伺服器拉去檔案不僅僅是最新版本的檔案資料,同僚還包含了所有曆史資料,現在我們來看看遠端伺服器資料倉庫的使用。我們已github 測試項目作為遠端伺服器資料倉庫作為操作環境。

1、克隆一個倉庫

$ git clone [email protected]:andy/test.git

Cloning into 'test'...

remote: Reusing existing pack: 4, done.

remote: Total 4 (delta 0), reused 0 (delta 0)

Receiving objects: 100% (4/4), done.

$ git remote

origin

$ git remote -v

gshell  https://gitshell.com/andy/test.git (fetch)

gshell  https://gitshell.com/andy/test.git (push)

origin  [email protected]:andy/test.git (fetch)

origin  [email protected]:andy/test.git (push)

$ cat .git/config

[remote "origin"]

        fetch = +refs/heads/*:refs/remotes/origin/*

        url = [email protected]:andy/test.git

[branch "master"]

        remote = origin

        merge = refs/heads/master

[remote "gshell"]

        url = https://gitshell.com/andy/test.git

        fetch = +refs/heads/*:refs/remotes/gshell/*

2、添加遠端倉庫

git remote 可以查詢遠端倉庫簡短名稱,在克隆某個項目後,至少可以看到可以名為origin的遠端庫,git用這個預設辨別你克隆的原始倉庫。我們現在也可以添加一個新的倉庫。從遠端倉庫擷取資料git fetch remote-name,會從遠端資料倉庫拉去你本地沒有的資料,拉去完成後,可以和你本地的一個分支做合并,開始我麼克隆一個倉庫,系統會預設把倉庫歸于origin名下,git fetch origin 會抓取你從上次更新以來有别人更新到伺服器的新内容,fetch隻是從遠端的資料拉取到本地倉庫,并不合并到目前工作分支。隻有使用 git pull origin 指令會自動抓取資料下來,然後将遠端分支自動合并到本地倉庫中的目前分支,日常工作中都這樣使用,又快有好。

$ git remote add gitcsdn  [email protected]:andy_yyf/test.git

gitcsdn [email protected]:andy_yyf/test.git (fetch)

gitcsdn [email protected]:andy_yyf/test.git (push)

gshell  [email protected]:andy/test.git (fetch)

gshell  [email protected]:andy/test.git (push)

$ git fetch gshell

From gitshell.com:andy/test

 * [new branch]      master     -> gshell/master

3、推送資料到遠端倉庫

 在工作中項目開發完成部分,需要推送到遠端伺服器和大家合并共享工作内容。需要将本地倉庫資料推送到遠端倉庫,實作這個指令和簡單,

git pull remote-name branch-name ,如果被本地分支master 分支推送到origin 伺服器上。隻要在中心伺服器有權限,或者同一時候沒有其他人也在推送資料,或者從最近推送或擷取後有其他人以後更新,可以直接推送到遠端倉庫,如果别人已有更新,需求把遠端更新的資料抓取到本地合并後再推送。

$ git push

Counting objects: 4, done.

Delta compression using up to 4 threads.

Compressing objects: 100% (2/2), done.

Writing objects: 100% (3/3), 330 bytes, done.

Total 3 (delta 0), reused 0 (delta 0)

To [email protected]:andy/test.git

   7b26911..894ed8b  master -> master

$ git push gshell

To [email protected]:andy/test.git

$ git push gitcsdn

To [email protected]:andy_yyf/test.git

4、檢視遠端倉庫資訊

檢視遠端倉庫資訊 git remote show remote-name ,檢視某個遠端倉庫的詳細資訊,

    $ git remote show origin

* remote origin

  Fetch URL: [email protected]:andy/test.git

  Push  URL: [email protected]:andy/test.git

  HEAD branch: master

  Remote branch:

    master tracked

  Local branch configured for 'git pull':

    master merges with remote master

  Local ref configured for 'git push':

    master pushes to master (up to date)

5、遠端倉庫本地配置别别名修改

遠端倉庫本地.git/config 配置檔案中本地短名稱修改 git remote rename oldname newname.遠端倉庫有變化,如遠端的鏡像不再使用,或者原來的克隆不再使用,需要異常遠端倉庫,可以git remote rm 指令。  通過本地修改本地配置檔案,需求修改檔案内容存放在.git/config 的檔案中。

$ git remote rename gitcsdn gcsdn

        url = [email protected]:andy/test.git

[remote "gcsdn"]

        url = [email protected]:andy_yyf/test.git

        fetch = +refs/heads/*:refs/remotes/gcsdn/*

$ git remote rm gshell

[core]

        repositoryformatversion = 0

        filemode = true

        bare = false

        logallrefupdates = true

        ignorecase = true

6、标簽

版本打标簽是在某一個版本釋出或者對應的送出上打标簽,git tag  tagName 指令是打标簽的指令,帶有備注資訊的标簽需要 git  tag -a v1.0 -m "xxxx" 的标簽,标簽使用有兩種類型,前一種是輕量級(lightweight) 和含付注釋的(annotated),輕量級标簽就像是個不會變化的分支,實際上它就是個指向特定送出對象的引用。而含付注釋的标簽,實際上是在存儲倉庫中的一個獨立對象,對象存放在.git/objects/中,它有自身的校驗和資訊,包含着标簽的名字,電子郵件位址和日期,以及标簽說明,标簽本身也允許GNU privacy Guard(GPG)來簽署或驗證。一般我們都使用含有付注釋型的标簽,以便保留相關資訊,當然如果隻是臨時性的添加标簽,或者不需要添加額外資訊,就可以用輕量級的标簽。用git show 可以檢視送出對象資訊上,列出了标簽的送出者和送出時間,以及标簽的說明。

$ git tag -a v1.0 -m"it the tag v1.0"

$ git tag

v1.0

$ git show v1.0

commit 894ed8b9883fac03c386d2d26317375797853586

Author: andy <[email protected]>

Date:   Mon Mar 10 16:46:50 2014 +0800

    local 2st commit“

diff --git a/readme.txt b/readme.txt

new file mode 100644

index 0000000..c261c57

--- /dev/null

+++ b/readme.txt

@@ -0,0 +1 @@

+git introduction

版本提已送出到倉庫,或者某個曆史送出記錄上打标簽,也可以打标簽,隻需要在标簽後加上對應的校驗和,如下後補上上标簽:

$ git tag v1.1.1 2e9cc

$ git show v1.1.1

commit 2e9cc04f50e000d5280979348e7084afcb9d35f2

Author: andy <[email protected]>

Date:   Mon Mar 10 22:19:03 2014 +0800

     3rd commit

index c261c57..2b92ac4 100644

--- a/readme.txt

@@ -1 +1,2 @@

 git introduction

+the current release version 1.8.0

标簽隻會儲存在本地,暫存區對象也是隻儲存在本地,在推送倉庫的時候不會推送到遠端伺服器上,标簽可以推送到遠端伺服器,需要通過顯式指令才能将标簽簽到遠端倉庫,其指令方式猶如推送分支,運作git push origin tagname,推送本地所有标簽 --tags,其它人在拉取資料的時候也會看到對推送到伺服器的标簽。

$ git push origin v1.1.1

Counting objects: 5, done.

Compressing objects: 100% (3/3), done.

Writing objects: 100% (3/3), 287 bytes, done.

Total 3 (delta 1), reused 0 (delta 0)

 * [new tag]         v1.1.1 -> v1.1.1

$ git push origin --tags

Counting objects: 1, done.

Writing objects: 100% (1/1), 148 bytes, done.

Total 1 (delta 0), reused 0 (delta 0)

 * [new tag]         v1.0 -> v1.0

 * [new tag]         v1.1 -> v1.1

git 基本操作,建立和克隆倉庫,更新,暫存和克隆倉庫,暫存并送出更新等,以及檢視曆史更新記錄等。

标簽: git, github, 源碼管理, git tag, git remote