搭建自己的git代碼服務器

一直以來想在家搭建一個自己的git代碼服務器,國慶假期終於有時間搞一搞了。這樣自己在筆記本上辛辛苦苦寫的代碼,就可以備份到linux主機上,再也不擔心某天筆記本電腦硬盤掛了,或者電腦掛了,而丟失代碼。

資源:

  1. 一臺 linux主機(作爲git服務器來託管)
  2. 一臺win7筆記本電腦(使用win7筆記本作爲日常開發電腦)

準備工作:

  1.       在linux主機上安裝git:sudo apt-get install git
  2.       在筆記本電腦上安裝windows版本的git,非常簡單,附上連接https://git-scm.com/downloads

假設以上工作已經完成,接着就開始搭建git服務器。

windowns端:

  1. 啓動git bash,這裏模擬了一個linux環境,裏面有git的命令,及vi命令,非常的方便,當然有些用戶可能習慣使用windows GUI工具也是可以的,我們暫且不說。
  2. 在e盤下新建source_code目錄,並切換到此目錄
  3. 生成公鑰 
    ssh-keygen

    連續按幾次enter就生成了在C:\Users\sc\.ssh下有個id_rsa.pub文件就是公鑰,我們需要把此文件中的內容copy到linux主機中對應的位置,下文中會有詳細說明。

Linux端:

  1.  需要增加一個名爲git的用戶,並在git用戶的目錄下新建一個.ssh文件夾,並在.ssh文件夾中創建authorized_keys文件 

     

sudo adduser git

su git

cd

mkdir .ssh
cd .ssh

touch authorized_keys

創建authorized_keys文件是目的是爲了把各個用戶的公鑰存放在這個文件中,只有把這些公鑰存放在這裏,對應的用戶纔有權限訪問我們的代碼服務器。把上文中在windows中創建的id_rsa.pub文件中的內容拷貝到authorized_keys中並保存。

2.創建代碼目錄,假設我們創建爲project.git

mkdir project.git

cd project.git

3.將project.git初始化爲空倉庫,使用--bare選項

git@sc-System-Product-Name:~/project.git$ git --bare init
Initialized empty Git repository in /home/git/project.git/

現在我們已經在服務器端初始化完成了一個空倉庫。

windows端:

sc@sc-PC MINGW64 /e/source_code
$ mkdir code_test

sc@sc-PC MINGW64 /e/source_code
$ cd code_test/

sc@sc-PC MINGW64 /e/source_code/code_test


在code_test裏面新建文件

sc@sc-PC MINGW64 /e/source_code/code_test
$ ls
test.c

使用命令行對code_test文件夾做初始化,並做一次git 初始化提交

sc@sc-PC MINGW64 /e/source_code/code_test
$ git init
Initialized empty Git repository in E:/source_code/code_test/.git/

sc@sc-PC MINGW64 /e/source_code/code_test (master)
$ git add .
warning: LF will be replaced by CRLF in test.c.
The file will have its original line endings in your working directory.

sc@sc-PC MINGW64 /e/source_code/code_test (master)
$ git commit -m "initial commit"
[master (root-commit) 6b7e181] initial commit
warning: LF will be replaced by CRLF in test.c.
The file will have its original line endings in your working directory.
 1 file changed, 1 insertion(+)
 create mode 100644 test.c

將本地倉庫的遠端添加爲linux主機中的空倉庫

git remote add origin [email protected]:/home/git/project.git

PS:git remote rm origin 可以刪除已經存在的遠端地址。

 

並push到遠端

$ git push origin master
[email protected]'s password:
Counting objects: 3, done.
Writing objects: 100% (3/3), 228 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:/home/git/project.git
 * [new branch]      master -> master

可以看到我們已經提交成功。

$ git remote -v
origin  [email protected]:/home/git/project.git (fetch)
origin  [email protected]:/home/git/project.git (push)

在另外一個目錄中做一次拉取測試

sc@sc-PC MINGW64 /e/source_code/one
$  git clone [email protected]:/home/git/project.git
Cloning into 'project'...
[email protected]'s password:
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
Checking connectivity... done.

sc@sc-PC MINGW64 /e/source_code/one
$ ls
project/

sc@sc-PC MINGW64 /e/source_code/one
$ cd project/

sc@sc-PC MINGW64 /e/source_code/one/project (master)
$ ls
test.c

好了,現在已經完全ok了,可以愉快的提交代碼了。後面更復雜的git用戶管理,團隊合作等用法,後面有時間再更新。

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章