搭建自己的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 git@192.168.1.105:/home/git/project.git

PS:git remote rm origin 可以删除已经存在的远端地址。

 

并push到远端

$ git push origin master
git@192.168.1.105'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 git@192.168.1.105:/home/git/project.git
 * [new branch]      master -> master

可以看到我们已经提交成功。

$ git remote -v
origin  git@192.168.1.105:/home/git/project.git (fetch)
origin  git@192.168.1.105:/home/git/project.git (push)

在另外一个目录中做一次拉取测试

sc@sc-PC MINGW64 /e/source_code/one
$  git clone git@192.168.1.105:/home/git/project.git
Cloning into 'project'...
git@192.168.1.105'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用户管理,团队合作等用法,后面有时间再更新。

 

 

 

 

 

 

 

 

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值