Git 遠程倉庫搭建


大名鼎鼎的git就不多做介紹了,總之。我們使用git來作為項目的一個版本控制工具,多人開發的項目的時候會輕松很多。

 

安裝git

whthomas@whthomas:~/workplace/gitOne$sudo apt-get install git

Windows下,可以在http://msysgit.github.io/上下載安裝包。

同時Windows上也可以執行shell喲!

話不多說,下面我們來配置git的遠程倉庫 首先下我們需要配置git上我們的用戶信息,包括這個用戶名稱和電子郵件地址,用戶名可隨意修改,它們用於記錄是誰提交了更新,以及更新大家的聯系方式。

whthomas@whthomas:~/workplace/gitOne$ git config --global user.name  "whthomas"
whthomas@whthomas:~/workplace/gitOne$ git config --global user.email whthomas93@gmial.com

這里再做一個配置

whthomas@whthomas:~/workplace/gitOne$ git config receive.denyCurrentBranch ignore

 

好了我們開始架設服務器了 首先為了避免我們的項目和其他的文件相沖突,我們新建一個git用戶。

root@whthomas:/home/whthomas# adduser git
root@whthomas:/home/whthomas# su git

我們新建一個文件夾 .ssh,並在其目錄下新建一個文件authorized_keys,它被用於存放其他用戶的公鑰(所有人的公鑰都要放在這個文件里面,我們可以使用 >> 的方式,把大家的公鑰追加進來。)

git@whthomas:~$ mkdir .ssh
git@whthomas:~$ cd .ssh
git@whthomas:~$ touch authorized_keys

放好大家的公鑰之后,我們開始使用git用戶新建一個倉庫了。

git@whthomas:~$ mkdir res.git
git@whthomas:~$ cd res.git/
git@whthomas:~/res.git$ git --bare init

在另一端用戶就可以使用自己新建的倉庫加入這個遠程倉庫中去了

whthomas@whthomas:~/workplace/gitu$ git init
初始化空的 Git 版本庫於 /home/whthomas/workplace/gitu/.git/
whthomas@whthomas:~/workplace/gitu$ touch README
whthomas@whthomas:~/workplace/gitu$ echo hello >> README 
whthomas@whthomas:~/workplace/gitu$ cat README 
hello
whthomas@whthomas:~/workplace/gitu$ git add .
whthomas@whthomas:~/workplace/gitu$ git commit -m "add a README"
[master (根提交) 59d4695] add a README
 1 file changed, 1 insertion(+)
 create mode 100644 README
whthomas@whthomas:~/workplace/gitu$ git remote add origin git@127.0.0.1:/home/git/res.git
whthomas@whthomas:~/workplace/gitu$ git push origin master 
Counting objects: 3, done.
Writing objects: 100% (3/3), 216 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@127.0.0.1:/home/git/res.git
 * [new branch]      master -> master

如果push的時候出現錯誤:

ssh: connect to host 127.0.0.1 port 22: Connection refused
fatal: The remote end hung up unexpectedly

這是由於openssl服務器沒有安裝的問題,使用如下命令安裝之后就OK了。

whthomas@whthomas:$sudo apt-get install openssh-server 

安裝完成之后,我們再次push代碼那么一切都可以ok了。

 

我們可以去另外一個目錄下clone項目,檢查push是不是成功。

whthomas@whthomas:~/workplace/gitu$ cd ..
whthomas@whthomas:~/workplace$ git clone git@127.0.0.1:/home/git/res.git
正克隆到 'res'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
接收對象中: 100% (3/3), done.
Checking connectivity... done
whthomas@whthomas:~/workplace$ ls
gitu     res     

Ok!  遠程倉庫搭建成功!這種git服務器搭建方式,非常適合幾個人的小型團隊協作工作。

 

附上我們怎么生成ssh密鑰。

whthomas@whthomas:~$ ssh-keygen

一路回車鍵~(當然你要輸入密碼的話,就不要一路回車了。。。。)

生成的密鑰文件會放在用戶目錄的.ssh目錄下。

whthomas@whthomas:~/.ssh$ ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts

id_rsa是我們的私鑰, id_rsa.pub是我們的公鑰。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM