准备工作:
先弄来一个服务器
可以先自行租赁一个服务器例如华为云、阿里云、腾讯云等等,这些服务器都可以哪个便宜就选择哪个吧。然后安装一个ubuntu的系统,这边我自用的是ubuntu系统,其他的系统没用搭建过,如果是Linux的系统大致都一样。
我这里选择的是一个轻量级服务器,其他的类型服务器只要能安装Linux系统也是可以,按照自己的需求选择。
连接服务器
然后这一步之后,用一些支持ssh协议的软件连接这个服务器,这里我自己用的是XShell
XSHELL 的官网 这个下载申请一个学习使用版就行其他的什么功能用不到。
第一次连接的是要输入的勾上记住账户名、密码后面就不需要再继续输入了
到这里所有的准备工作已经做完下面开始安装Git
开搞
安装Git
在服务器的ubuntu命令行输入命令安装git
sudo apt-get install git
安装完Git之后可以在ubuntu中创建一个用户作为Git仓库的管理账户,也可以直接使用ubuntu这个用户作为Git仓库的管理账户。这里我是直接用ubuntu直接做为管理用户。
创建一个文件夹放自己的仓库
ubuntu@VM-4-5-ubuntu:~$ mkdir git_warehouse #文件夹名字可以自己起
ubuntu@VM-4-5-ubuntu:~$ cd git_warehouse/
ubuntu@VM-4-5-ubuntu:~/git_warehouse$ mkdir test.git #创建仓库文件夹
ubuntu@VM-4-5-ubuntu:~/git_warehouse$ cd test.git/
ubuntu@VM-4-5-ubuntu:~/git_warehouse/test.git$ git --bare init #使用Git命令初始化仓库
到这里git的远程仓库就创建完成了
下面看是测试
客户端连接
在Windows上随便打开一个文件夹然后右键使用Git bash Here打开
在Windows客户端安装git,安装教程在这里就略过了,基本上是一直无脑下一步就行
Git 官网
git remote -v #查看有没有连接到远端仓库,第一次打开应该是不会有连接到的
Windows安装完git之后第一次打开先设置一下邮箱和名字,不然上传会失败
git config --global user.name "名字"
git config --global user.email "邮箱"
然后是获取远端仓库
$ git clone ubuntu@自己的ip:自己的仓库路径
然后切换到获取到的文件夹里面然后再查看连接情况,可以看到已经有连接到远端
连接完成现在开始上传文件
在本地端创建一些文件然后添加到本地的仓库里面
$ git add .#把当前目录下的文件都添加到本地的仓库
$ git commit -m "根据实际情况添加介绍"
开始上传
$ git push origin
测试一下
可以打开ubuntu查看上传log
git log
打开一个新的文件获取一下测试
会遇到的问题
问题1
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@自己的ip:/home/ubuntu/git_warehouse/test.git
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'git@自己的ip:/home/ubuntu/git_warehouse/test.git
解决办法:
#这一段在服务器端的仓库里面的config文件中添加
[receive]
denyCurrentBranch = ignore
然后再运行一个命令就ok了
git config --bool core.bare true
问题2:
这个问题会在第一次获取文件的时候出现,如果有出现,就把known_hosts文件中的内容清除再重新获取一次
到此,教程已经全部结束。如果是不用ubuntu这个用户作为git仓库的管理账户而创建新的用户做为管理用户的话,那新创建的用户要给他一些必要的权限,其他是操作都是一样的。