Git很火舔箭。原因有三:
它是大神Linus Torvalds的作品,天然地具備神二代的氣質(zhì)和品質(zhì)蚊逢;
促進了生產(chǎn)力的發(fā)展层扶,Git的分布式版本控制理念,并非首創(chuàng)烙荷,但非常適合開源社區(qū)的協(xié)作方式(不存在master-slave的關(guān)系)
GitHub
GitHub很好镜会,號稱代碼界的facebook.
facebook,twitter终抽,Microsoft戳表,vmware,redhat昼伴,LinkedIn匾旭,Yahoo等公司都在GitHub上有建立數(shù)目不等的repositories。一些知名開源項目圃郊,例如jQuery价涝, Ruby on Rails,node.js都把src code寄存于GitHub上持舆。GitHub太成功了色瘩,以至于使很多人產(chǎn)生誤解伪窖,以為git就是GitHub,使用git就必須連接GitHub居兆。事實上覆山,GitHub只是一個提供git repository hosting服務(wù)的網(wǎng)站。
本文試圖講解如何在U盤上創(chuàng)建git repository(使U盤成為你的私有代碼云)史辙;以及如何在不同客戶端進行同步作業(yè)汹买。把git repository建在USB盤上能滿足多種應(yīng)用場景佩伤,特別是:
注重私密性(GitHub上普通賬號不能創(chuàng)建私有repository)
網(wǎng)速很慢聊倔,甚至斷網(wǎng)的時候需要同步
但不適合需要強collaborate的項目。
前提條件
先把git給裝好了…然后…我們有了兩臺git ready的電腦生巡,和一個U盤耙蔑。
開始,1孤荣,初始化本地repository
假設(shè)有一個存在的項目甸陌,需要由git接管版本控制,那么來到這個%projct_home%目錄(例如我的git_sandbox)下
step 1.1
初始化
$ git init git_sandbox
step 1.2
創(chuàng)建.gitignore文件(在%project_home%下盐股,只對這個project有效)钱豁,排除路徑下不需用被提交到repository中的文件(例如.svn,.class疯汁, Thumbs.db…)
step 1.3
查看當(dāng)前文件狀態(tài)牲尺,可以看到有一堆”untracked files”
$ git status
step 1.4
把所有”untracked files”加入索引
$ git add .
step 1.5
提交到repository
$ git commit -m "initialized."
2, 搞到U盤上去
step 2.1
插上U盤幌蚊,查看U盤掛載路徑
$ mount
我的路徑是”/Volumes/KINGSTON”
step 2.2
在U盤上創(chuàng)建一個repository谤碳,
$ mkdir /Volumes/KINGSTON/workspace/usbGitSpace/gitusb_sandbox
$ cd /Volumes/KINGSTON/workspace/usbGitSpace/gitusb_sandbox
$ git init --bare
使用–bare選項創(chuàng)建的repository被稱作bare repository,它不會包含working目錄(只包含.git目錄下的內(nèi)容)溢豆,所以不適合在上面改code蜒简。bare repository主要的作用就是被push和pull。根據(jù)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
在另一臺電腦上先創(chuàng)建一個本地repository
$ cd ~/my_gitspace/sandbox_win
$ git init
step 3.2
把U盤插到這個電腦上,查看當(dāng)前掛載的路徑队他,添加U盤作為當(dāng)前repository的remote repository
$ git remote add usb /cygdrive/f/workspace/usbGitSpace/gitusb_sandbox
step 3.3
把U盤上的內(nèi)容拉下來
$ 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
發(fā)現(xiàn)兩臺電腦上提交的記錄都在log里面
好了系枪,成功。現(xiàn)在U盤成為了你的GitHub磕谅,你和你的代碼之間私爷,再沒有阻隔雾棺。
當(dāng)然,最后衬浑,需要定期給U盤做一個備份捌浩。技術(shù)發(fā)展到今天,數(shù)據(jù)安全靠天吃飯的日子已經(jīng)一去不復(fù)返了工秩,沒有什么U盤尸饺,硬盤是靠得住的。