1. 安裝 openssh-server ,用於創建SSH服務。
sudo apt-get install openssl-server
使用命令ps -e|grep ssh,查看ssh服務是否啟動。
如果正常啟動,則會顯示類似信息:1966 ? 00:00:00 ssh-agent
2. 創建用戶名為git的用戶,用來管理和運行git服務。
sudo user del -r git // 刪除已經存在的叫git的用戶; sudo adducer git // 添加用戶名叫git的用戶;
進入git用戶目錄下,創建目錄.ssh,然後在.ssh目錄下創建名為authorized_keys的文件;
cd /home/git sudo mkdir .ssh cd .ssh touch authorized_keys
將客戶端的公鑰(生成方法見後文)拷貝到authorized_keys文件中;
cat path/id_rsa.pub >> /home/git/.ssh/authorized_keys
安裝 git-core
sudo apt-get install git-core
3. 初始化服務端倉庫
git —bare init /home/git/test1.git
注:該命令會生成一個空倉庫,此時test1.git對應的地址則為git@hostIp:/home/git/test1.git
或者:
mkdir test1.git cd test1.git git —bare init
4. 客戶端運行ssh-keygen -t rsa生成密鑰;
生成密鑰後,在.ssh目錄下,會有兩個文件id_rsa和id_rsa.pub文件,id_rsa.pub為公鑰,通過命令scp /home/git/.ssh/id_rsa.pub gitServer:/home/git將client上生成的公鑰拷貝到gitServer上;
服務端把公鑰添加到authorized_keys文件中後,客戶端就能通過路徑訪問到git倉庫了;
生成密鑰時,會要求你確認保存公鑰的位置(默認為~/.ssh/id_rsa),然後會讓重復一個密碼兩次,如果不想在使用公鑰的時候輸入密碼,則留空;
假定hostIp為192.168.1.100
git clone [email protected]:/home/git/test1.git
5. 常用git命令
>1. git clone
語法:git clone 版本庫網址 本地庫名稱(可省略)
>2. git remote
此命令用於管理遠程主機名,在沒有參數的情況下可以列出所有主機名;
git remote origin
顯示origin是在使用clone命令,克隆遠程版本庫時git自動為遠程主機命令;
通過git remote -v 查看版本庫的地址;
>3. git fetch
語法:git fetch origin(git fetch origin master)
默認情況下,git fetch origin將會更新遠程主機origin上所有分支,如果只想更新某個分支則在主機名後加分支名
>4. git push
語法:git push 遠程主機名 本地分支名:遠程分支名
如果省略遠程分支名,則表示將本地分支推送與存在最終關系的遠程分支,如果遠程分支不存在,則會被創建;
git push origin master,表示將本地master分支推送到origin主機的master分支;
如果省略本地分支名,則表示要刪除遠程主機中的分支,如git push origin : master,則表示刪除origin主機中的master分支;
>5. git pull
語法:git pull 遠程主機 遠程分支:本地分支 如:git pull origin master:master,表示將遠程主機origin中的master分支更新到本地分支master;
6. 每次添加一個新項目都需要通過shell登入主機並創建一個純倉庫,我們不妨以gitServer作為git用戶和倉庫所在的主機名,如果你在網絡內部運行該主機,並且在DNS中設定gitServer指向該主機,則以下這些命令都是可用的:
cd myproject git init git add . git commit -m “initial commit” git remote add origin git@gitServer:test1.git git push origin master
創建一個版本庫倉庫
這樣,其它人的克隆和推送也一樣
git clone git@gitServer:/test1.git vim README git commit -am ‘fix for the README file’ git push origin master
7. 作為一個額外的防護措施,可以用git自帶的git-shell工具來把git用戶的活動限制在僅與git相關。把它設為git用戶登入的shell,那麼該用戶就不能擁有主機正常的shell訪問權,為了實現這一點,需要指明用戶的登入shell為git-shell,而不是 bash 或者 csh,可以通過編輯/etc/passwd文件
sudo vim /etc/passwd
在文件末尾,找到類似此行
git:x:1000:1000::/home/git:/bin/sh
將bin/sh改為/usr/bin/git-shell
git:x:1000:1000::/home/git:/usr/bin/git-shell
現在git用戶只能用ssh連接來推送和獲取git倉庫,而不能直接使用主機shell。
8. Q&A
(1)
Q:
xiongmc@xiongmc-desktop:~/myproject2$ git push origin master
Agent admitted failure to sign using the key.
git@localhost's password:
error: src refspec master does not match any.
error: failed to push some refs to 'git@localhost:/opt/git/project.git/'
A:如果初始的代碼倉庫為空,git push origin master提交代碼的時候會出現以上異常
http://www.linuxidc.com/Linux/2013-03/81022.htm
(2)
Q:
xiongmc@xiongmc-desktop:~/myproject2$ git push origin master
Agent admitted failure to sign using the key.
git@localhost's password:
Permission denied, please try again.
git@localhost's password:
Counting objects: 3, done.
Writing objects: 100% (3/3), 213 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
error: insufficient permission for adding an object to repository database ./objects
fatal: failed to write object
error: unpack failed: unpack-objects abnormal exit
To git@localhost:/opt/git/project.git/
! [remote rejected] master -> master (n/a (unpacker error))
error: failed to push some refs to 'git@localhost:/opt/git/project.git/'
A: 服務器無權限。
http://blog.sina.com.cn/s/blog_53e449530101349s.html
http://stackoverflow.com/questions/1918524/error-pushing-to-github-insufficient-permission-for-adding-an-object-to-reposi
sudo chown -R git:gitgroup path/to/repo.git/ // or sudo chown -R git:git path/to/repo.git/
(3)
http://www.linuxidc.com/Linux/2013-03/81022.htm
Q:
xiongmc@xiongmc-desktop:~/myproject2$ git push origin master
Agent admitted failure to sign using the key.
git@localhost's password:
Counting objects: 3, done.
Writing objects: 100% (3/3), 213 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error:
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error:
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To git@localhost:/opt/git/project.git/
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'git@localhost:/opt/git/project.git/'
A:
$cd .git
$vim config
該配置文件的原始內容為:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
在該配置文件中加入以下內容:
[receive]
denyCurrentBranch = ignore
(4)
Linux服務器:Ubuntu配置git服務 - 逸雲沙鷗
http://www.xue5.com/Server/Linux/667461.html
(5)為了集成到SCM,我們在Linxu上安裝GIT
http://www.examw.com/linux/all/182529/index-2.html
在LINUX上創建GIT服務器
http://lionest.iteye.com/blog/1447310
http://blog.csdn.net/andy_android/article/details/6996134
Receiving objects: 26% (5668/21560), 8.06 MiB | 183 KiB/s 21560)
(6)
Q:
xiongmc@xiongmc-desktop:~/myproject.git$ git push origin master ssh: connect to host xiongmc-desktop port 22: Connection refused
fatal: The remote end hung up unexpectedly
xiongmc@xiongmc-desktop:~/myproject.git$ git push origin master
ssh: connect to host xiongmc-desktop port 22: Connection refused
fatal: The remote end hung up unexpectedly
A:
http://blog.csdn.net/zlm_250/article/details/7979221
sudo apt-get install openssh-server
sudo net start sshd
sudo ufw disable
ssh localhost
(7)
Q:
ubuntu系統下“關於'xx'用戶不在 sudoers文件中,此事將被報告。”的解決方法
A:
http://blog.sina.com.cn/s/blog_bede36550101b0av.html
git ALL=(ALL:ALL) ALL
(8)
Q:
xiongmc@xiongmc-desktop:~/myproject.git$ git push origin master
git@xiongmc-desktop's password:
fatal: '/opt/git/project.git' does not appear to be a git repository
fatal: The remote end hung up unexpectedly
A:
http://www.dotkam.com/2010/08/22/gitolite-does-not-appear-to-be-a-git-repository/