美文网首页
【转】把Git Repository建到U盘上去

【转】把Git Repository建到U盘上去

作者: 小白文_Vincent | 来源:发表于2017-02-16 17:11 被阅读0次
    • Git很火。原因:

      • 1.它是大神Linus Torvalds的作品,天然地具备神二代的气质和品质;
      • 2.促进了生产力的发展,Git的分布式版本控制理念,并非首创,但非常适合开源社区的协作方式(不存在master-slave的关系)
    • GitHub很好,号称代码界的facebook.

    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盘上能满足多种应用场景,特别是:

      • 1.注重私密性(GitHub上普通账号不能创建私有repository)
      • 2.网速很慢,甚至断网的时候需要同步
    • 但不适合需要强collaborate的项目。

    <h3>前提条件</h3>

    • 先把git给装好了…然后…我们有了两台git ready的电脑,和一个U盘。

    <h3>开始,1,初始化本地repository</h3>

    • 假设有一个存在的项目,需要由git接管版本控制,那么来到这个%projct_home%目录(例如我的git_sandbox)下
    • <h5>step 1.1</h5>
      • 初始化
      • $ git init git_sandbox
    • <h5>step 1.2</h5>
      • 创建.gitignore文件(在%project_home%下,只对这个project有效),排除路径下不需用被提交到repository中的文件(例如.svn,.class, Thumbs.db…)
    • <h5>step 1.3</h5>
      • 查看当前文件状态,可以看到有一堆”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盘上创建一个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.

    • <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, 同步到另一台电脑</h3>

    • <h5>step 3.1</h5>

      • 在另一台电脑上先创建一个本地repository
      • $ cd ~/my_gitspace/sandbox_win
      • $ git init
    • <h5>step 3.2</h5>

      • 把U盘插到这个电脑上,查看当前挂载的路径,添加U盘作为当前repository的remote repository
      • $ git remote add usb /cygdrive/f/workspace/usbGitSpace/gitusb_sandbox
    • <h5>step 3.3</h5>

      • 把U盘上的内容拉下来
      • $ git pull usb master
        好了,代码同步到另一台机器上了
    • <h3>4, 测试一下</h5>

    • <h5>step 4.1</h5>

      • 改动一下文件,比如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
    • 发现两台电脑上提交的记录都在log里面

    • 好了,成功。现在U盘成为了你的GitHub,你和你的代码之间,再没有阻隔。

    • 当然,最后,需要定期给U盘做一个备份。技术发展到今天,数据安全靠天吃饭的日子已经一去不复返了,没有什么U盘,硬盘是靠得住的。

    相关文章

      网友评论

          本文标题:【转】把Git Repository建到U盘上去

          本文链接:https://www.haomeiwen.com/subject/agjhwttx.html