在Linux服务器上搭建Git远程仓库并Push本地库
服务器
安装Git
添加git用户
adduser git -m
- 1
- 2
设置密码
passwd git
- 1
- 2
切换到git用户,新建repos目录,在该目录下新建库hello.git目录,
cd到hello.git目录,初始化该库:
git --bare init hello.git
- 1
- 2
这样就新建了一个hello的远程库,这个库是空的。
本地
安装git
配置用户名和邮箱
git config --global user.name "YOUR NAME"
git config --global user.email "YOUR EMAIL ADDRESS"
- 1
- 2
- 3
- 4
假设本地已有一个和远程库同名的hello项目,进入到该项目下,初始化该库
git init
- 1
- 2
将所有文件添加到本地库索引
git add -A
- 1
- 2
提交工作目录树中的所有文件到本地库
git commit -a -m "init commit"
- 1
- 2
这时通过以下命令可查看本地分支:
git branch -a
- 1
- 2
列出的分支中,带有*的为本地分支,其他为远程分支。
要查看远程分支信息,执行命令:
git remote -v
- 1
- 2
如果输出信息为空,表示还没有添加远程主机,通过以下命令添加:
git remote add <主机名> <网址>
- 1
- 2
例如:
git remote add origin [email protected]:repos/hello.git
- 1
- 2
最后,将本地库push到上面的远程库:
git push origin master
- 1
- 2
origin 是远程库主机名, master是本地分支名,如果该远程分支不存在,则会被新建。
输入git用户的密码完成push。
如果远程仓库不为空的话,需要先执行pull,取回远程主机某个分支的更新,再与本地的指定分支合并.
git pull origin master
- 1
- 2
如果操作过程提示错误:
fatal: Unable to create temporary file: Permission denied
表示服务器git库目录权限问题,修改成git用户及git用户组就可以了。
参考:
http://www.ruanyifeng.com/blog/2014/06/git_remote.html