1. 安裝 openssh-server ,用於建立SSH服務。html
sudo apt-get install openssl-server
使用命令ps -e|grep ssh,查看ssh服務是否啓動。linux
若是正常啓動,則會顯示相似信息:1966 ? 00:00:00 ssh-agent android
2. 建立用戶名爲git的用戶,用來管理和運行git服務。git
sudo user del -r git // 刪除已經存在的叫git的用戶;
sudo adducer git // 添加用戶名叫git的用戶;
進入git用戶目錄下,建立目錄.ssh,而後在.ssh目錄下建立名爲authorized_keys的文件;github
cd /home/git
sudo mkdir .ssh
cd .ssh
touch authorized_keys
將客戶端的公鑰(生成方法見後文)拷貝到authorized_keys文件中;shell
cat path/id_rsa.pub >> /home/git/.ssh/authorized_keys
安裝 git-coreubuntu
sudo apt-get install git-core
3. 初始化服務端倉庫vim
git —bare init /home/git/test1.git
注:該命令會生成一個空倉庫,此時test1.git對應的地址則爲git@hostIp:/home/git/test1.gitbash
或者:服務器
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 git@192.168.1.100:/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/