Git 远程仓库搭建

时间:2024-04-04 20:08:09

大名鼎鼎的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
file changed, insertion(+)
create mode 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: , done.
Writing objects: % (/), bytes | bytes/s, done.
Total (delta ), reused (delta )
To git@127.0.0.1:/home/git/res.git
* [new branch] master -> master

如果push的时候出现错误:

ssh: connect to host 127.0.0.1 port : 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: , done.
remote: Total (delta ), reused (delta )
接收对象中: % (/), 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是我们的公钥。