把Git Repository建到U盤上去

Git很火。緣由有三: node

  1. 它是大神Linus Torvalds的做品,自然地具有神二代的氣質和品質;
  2. 促進了生產力的發展,Git的分佈式版本控制理念,並不是獨創,但很是適合開源社區的協做方式(不存在master-slave的關係)
  3. GitHub

GitHub很好,號稱代碼界的facebook. git

facebook,twitter,Microsoft,vmware,redhat,LinkedIn,Yahoo等公司都在GitHub上有創建數目不等的repositories。一些知名開源項目,例如jQuery, Ruby on Rails,node.js都把src code寄存於GitHub上。GitHub太成功了,以致於使不少人產生誤解,覺得git就是GitHub,使用git就必須鏈接GitHub。事實上,GitHub只是一個提供git repository hosting服務的網站。 安全

本文試圖講解如何在U盤上建立git repository(使U盤成爲你的私有代碼雲);以及如何在不一樣客戶端進行同步做業。把git repository建在USB盤上能知足多種應用場景,特別是: bash

  1. 注重私密性(GitHub上普通帳號不能建立私有repository)
  2. 網速很慢,甚至斷網的時候須要同步

但不適合須要強collaborate的項目。 分佈式

前提條件

先把git給裝好了…而後…咱們有了兩臺git ready的電腦,和一個U盤。 svn

開始,1,初始化本地repository

假設有一個存在的項目,須要由git接管版本控制,那麼來到這個%projct_home%目錄(例如個人git_sandbox)下
step 1.1
初始化 測試

$ git init git_sandbox

step 1.2
建立.gitignore文件(在%project_home%下,只對這個project有效),排除路徑下不需用被提交到repository中的文件(例如.svn,.class, Thumbs.db…)
step 1.3
查看當前文件狀態,能夠看到有一堆」untracked files」 網站

$ git status

step 1.4
把全部」untracked files」加入索引 ui

$ git add .

step 1.5
提交到repository spa

$ git commit -m "initialized."

2, 搞到U盤上去

step 2.1
插上U盤,查看U盤掛載路徑

$ mount


個人路徑是」/Volumes/KINGSTON」

step 2.2
在U盤上建立一個repository,

$ mkdir /Volumes/KINGSTON/workspace/usbGitSpace/gitusb_sandbox
$ cd /Volumes/KINGSTON/workspace/usbGitSpace/gitusb_sandbox
$ git init --bare

使用–bare選項建立的repository被稱做bare repository,它不會包含working目錄(只包含.git目錄下的內容),因此不適合在上面改code。bare repository主要的做用就是被push和pull。根據GitFaq的說法:

A quick rule of thumb is to never push into a repository that has a work tree attached to it, until you know what you are doing.

step 2.3
回到本地%project_home%,把初始化後的usb repository添加爲remote repository

$ git remote add usb /Volumes/KINGSTON/workspace/usbGitSpace/gitusb_sandbox

將本地的repository push到usb上

$ git push usb master

3, 同步到另外一臺電腦

step 3.1
在另外一臺電腦上先建立一個本地repository

$ cd ~/my_gitspace/sandbox_win
$ git init

step 3.2
把U盤插到這個電腦上,查看當前掛載的路徑,添加U盤做爲當前repository的remote repository

$ git remote add usb /cygdrive/f/workspace/usbGitSpace/gitusb_sandbox

step 3.3
把U盤上的內容拉下來

$ git pull usb master

好了,代碼同步到另外一臺機器上了

4, 測試一下

step 4.1
改動一下文件,好比README.txt
step 4.2

$ git add README.txt
$ git commit -m "update from another laptop"
$ git push usb master

step 4.3
插回原來的laptop

$ git pull usb master

step 4.4
查看提交歷史

$ git log

發現兩臺電腦上提交的記錄都在log裏面

好了,成功。如今U盤成爲了你的GitHub,你和你的代碼之間,再沒有阻隔。

固然,最後,須要按期給U盤作一個備份。技術發展到今天,數據安全靠天吃飯的日子已經一去不復返了,沒有什麼U盤,硬盤是靠得住的。

原文連接: http://wuminqi.com/blog/2012/01/08/%E6%8A%8Agit-repository%E5%BB%BA%E5%88%B0u%E7%9B%98%E4%B8%8A%E5%8E%BB/

相關文章
相關標籤/搜索