-
Git很火。原因:
- 1.它是大神Linus Torvalds的作品啸驯,天然地具備神二代的氣質(zhì)和品質(zhì)客扎;
- 2.促進(jìn)了生產(chǎn)力的發(fā)展,Git的分布式版本控制理念罚斗,并非首創(chuàng)徙鱼,但非常適合開源社區(qū)的協(xié)作方式(不存在master-slave的關(guān)系)
GitHub很好,號(hào)稱代碼界的facebook.
facebook针姿,twitter袱吆,Microsoft,vmware距淫,redhat绞绒,LinkedIn,Yahoo等公司都在GitHub上有建立數(shù)目不等的repositories榕暇。一些知名開源項(xiàng)目处铛,例如jQuery饲趋, Ruby on Rails,node.js都把src code寄存于GitHub上撤蟆。GitHub太成功了奕塑,以至于使很多人產(chǎn)生誤解,以為git就是GitHub家肯,使用git就必須連接GitHub龄砰。事實(shí)上,GitHub只是一個(gè)提供git repository hosting服務(wù)的網(wǎng)站讨衣。
-
本文試圖講解如何在U盤上創(chuàng)建git repository(使U盤成為你的私有代碼云)换棚;以及如何在不同客戶端進(jìn)行同步作業(yè)。把git repository建在USB盤上能滿足多種應(yīng)用場(chǎng)景反镇,特別是:
- 1.注重私密性(GitHub上普通賬號(hào)不能創(chuàng)建私有repository)
- 2.網(wǎng)速很慢固蚤,甚至斷網(wǎng)的時(shí)候需要同步
但不適合需要強(qiáng)collaborate的項(xiàng)目。
<h3>前提條件</h3>
- 先把git給裝好了…然后…我們有了兩臺(tái)git ready的電腦歹茶,和一個(gè)U盤夕玩。
<h3>開始,1惊豺,初始化本地repository</h3>
- 假設(shè)有一個(gè)存在的項(xiàng)目燎孟,需要由git接管版本控制,那么來到這個(gè)%projct_home%目錄(例如我的git_sandbox)下
- <h5>step 1.1</h5>
- 初始化
$ git init git_sandbox
- <h5>step 1.2</h5>
- 創(chuàng)建.gitignore文件(在%project_home%下尸昧,只對(duì)這個(gè)project有效)揩页,排除路徑下不需用被提交到repository中的文件(例如.svn,.class烹俗, Thumbs.db…)
- <h5>step 1.3</h5>
- 查看當(dāng)前文件狀態(tài)爆侣,可以看到有一堆”untracked files”
$ git status
- <h5>step 1.4</h5>
- 把所有”untracked files”加入索引
$ git add .
- <h5>step 1.5</h5>
- 提交到repository
$ git commit -m "initialized."
<h3>2, 搞到U盤上去</h3>
-
<h5>step 2.1</h5>
插上U盤幢妄,查看U盤掛載路徑
$ mount
我的路徑是”/Volumes/KINGSTON”
-
<h5>step 2.2</h5>
- 在U盤上創(chuàng)建一個(gè)repository兔仰,
$ mkdir /Volumes/KINGSTON/workspace/usbGitSpace/gitusb_sandbox
$ cd /Volumes/KINGSTON/workspace/usbGitSpace/gitusb_sandbox
$ git init --bare
- 使用–bare選項(xiàng)創(chuàng)建的repository被稱作bare repository,它不會(huì)包含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.
-
<h5>step 2.3</h5>
- 回到本地%project_home%置吓,把初始化后的usb repository添加為remote repository
$ git remote add usb /Volumes/KINGSTON/workspace/usbGitSpace/gitusb_sandbox
- 將本地的repository push到usb上
$ git push usb master
<h3>3, 同步到另一臺(tái)電腦</h3>
-
<h5>step 3.1</h5>
- 在另一臺(tái)電腦上先創(chuàng)建一個(gè)本地repository
$ cd ~/my_gitspace/sandbox_win
$ git init
-
<h5>step 3.2</h5>
- 把U盤插到這個(gè)電腦上缔赠,查看當(dāng)前掛載的路徑衍锚,添加U盤作為當(dāng)前repository的remote repository
$ git remote add usb /cygdrive/f/workspace/usbGitSpace/gitusb_sandbox
-
<h5>step 3.3</h5>
- 把U盤上的內(nèi)容拉下來
-
$ git pull usb master
好了,代碼同步到另一臺(tái)機(jī)器上了
<h3>4嗤堰, 測(cè)試一下</h5>
-
<h5>step 4.1</h5>
- 改動(dòng)一下文件戴质,比如README.txt
-
<h5>step 4.2</h5>
$ git add README.txt
$ git commit -m "update from another laptop"
$ git push usb master
-
<h5>step 4.3</h5>
- 插回原來的laptop
$ git pull usb master
-
<h5>step 4.4</h5>
- 查看提交歷史
$ git log
發(fā)現(xiàn)兩臺(tái)電腦上提交的記錄都在log里面
好了度宦,成功。現(xiàn)在U盤成為了你的GitHub告匠,你和你的代碼之間戈抄,再?zèng)]有阻隔。
當(dāng)然后专,最后划鸽,需要定期給U盤做一個(gè)備份。技術(shù)發(fā)展到今天戚哎,數(shù)據(jù)安全靠天吃飯的日子已經(jīng)一去不復(fù)返了裸诽,沒有什么U盤,硬盤是靠得住的型凳。