Debian Linux 架設使用 SSH 存取 的 Git Server

Git 是分散式的版本控制系統, 但是在此 分散式管理 之後再談, 先把 集中管理的部份 完成(試著把 SVN 做的事情取代掉).

Git 相關介紹

Git 集中管理有 git://, http[s]://, ssh://, rsync:// 等方式, 在此就先從 ssh:// 開始.

Git 安裝

  • apt-get install git-core # in Debian / Ubuntu Linux

前置作業

因為此篇是使用 SSH, 所以下述開始前, 需要先將 SSH key 產生好, 並於 Server / Client 放置完成. 可參考:

相關資料準備

  • Server: example.com
  • Linux 帳號的群組: www-data (確認 /etc/group 的 www-data 有你的帳號, ex: www-data:your_account)
  • Project name: project_name
  • Git Repository: /var/cache/git

有下述兩種架設方法:

  1. 從 "建立 Git Repository" 開始
  2. 從 "單機架設 Git Server" 開始

此兩種架設方法都可行, 只是方式不同而已.

方法1

建立 Git Repository

  1. mkdir -p /var/cache/git/project_name.git
  2. cd /var/cache/git/project_name.git
  3. git --bare init
  4. chown root:www-data -R .
  5. chmod g+rwx -R .

由 Git Repository(Git Server)取得資料 到Client(您工作的電腦)

  1. git clone ssh://example.com/var/cache/git/project_name.git
  2. cd project_name
  3. touch index.html
  4. git add index.html
  5. git commit -m 'init'
  6. git push origin master # local 預設 clone 是 master, push 到 origin(remote server)
  7.  Counting objects: 3, done.
     Writing objects: 100% (3/3), 210 bytes, done.
     Total 3 (delta 0), reused 0 (delta 0)
     To ssh://example.com/var/cache/git/project_name.git
     * [new branch]      master -> master

  8. touch test.html
  9. git add test.html
  10. git commit -m 'add test.html'
  11. git push # 第二次後, 只需要 push 即可, 不需加 origin master.

測試

  1. mkdir /tmp/a /tmp/b
  2. cd /tmp/a
  3. git clone ssh://example.com/var/cache/git/project_name.git
  4. cd /tmp/b
  5. git clone ssh://example.com/var/cache/git/project_name.git
  6. echo "hello" > hello.html # 於 b 新增一個檔案, 下述新增並 push
  7. git add hello.html
  8. git commit -m 'add hello.html' # local commit.
  9. git push # 推到 Server 上.
  10. cd /tmp/a # 於 a pull 拉下來後, 應該會看到 hello.html
  11. git pull # 會看到 hello.html

方法2

單機架設 Git Server

上述需要 REMOTE_SERVER(GIT_SERVER) 和 一台 Client 機器, 如果一台 REMOTE_SERVER 要直接架設, 可以用下述步驟:(取自: Setting Up a Git Server)

此做法是 先在 REMOTE SERVER 建立 Git repository, 再於 Client 建立 Local 的 Git repository, 最後再將 Local 的 Git repository 指到 REMOTE SERVER 去.

REMOTE SERVER
  1. ssh user@REMOTE_SERVER
  2. mkdir project_name.git
  3. cd project_name.git
  4. git --bare init
於 REMOTE SERVER 或 任何一個地方
  1. mkdir project
  2. cd project
  3. git init
  4. touch README
  5. git add README
  6. git commit -m 'first commit'
  7. git remote add origin user@REMOTE_SERVER:project_name.git # commit 完成 push 前, 將 REMOTE_SERVER 位置指過去.
  8. git push origin master

相關網頁

作者: Tsung

對新奇的事物都很有興趣, 喜歡簡單的東西, 過簡單的生活.

在〈Debian Linux 架設使用 SSH 存取 的 Git Server〉中有 7 則留言

  1. 单用户这样简单使用是没问题的,
    多用户还要修改仓库的文件属性,不然无法push的。
    如果想要更丰富的权限控制,用gitosis。

  2. 多用戶修改文件屬性? 只要用戶是在 Group 裡面, 就都可以 push 囉~ 🙂
    不過 gitosis 似乎管理的方式會更好, 可以做權限管理.
    感恩~ 我再研究看看 gitosis. Orz.

  3. 我是 Git 新手。
    非常感謝您寫這篇文章。受教!
    可是有的地方我覺得補齊一下,會比較好。
    例如:
    「由 Git Repository 取得資料」可以寫得完整一點
    「由 Git Repository (Git Server)取得資料到 Client(Your work PC)」
    或是註明一下前面幾個步驟是在 Git Server 實做
    後面幾個步驟是在 Client (Your Work PC) 實做
    可能比較進階的玩家根本不用這些廢話,可是生手讀的時候,比較愚笨的(例如我)可能會轉不過來。

  4. 還有直接在 Client git clone 一個空的 project 的話,會碰到:
    fatal: no matching remote head
    好像可以補充一下,我 google 到這篇文章有提到:
    >> git clone is the command you use to clone and start working with a remote repository.
    >> Only problem is, this command does not work if the remote repository is empty.
    reference:http://rwmj.wordpress.com/2009/03/06/git-fatal-no-matching-remote-head/
    anyway, thanks a lot!

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料