安装 lnmp 环境后,下面介绍如何安装 git 环境。
1、准备工作:
useradd命令用于Linux中创建的新的系统用户
-m 自动建立用户的登入目录
useradd -m www
参数P代表parents,表示递归创建目录。
mkdir -p /data/www/yidian
更改当前目录的owner(就是所有者)为www,-R意思是循环遍历
chown -R www.www /data/www
su - www
su命令和su -命令最大的本质区别就是:前者只是切换了root身份,但Shell环境仍然是普通用户的Shell
注:如果报错 “This account is currently not available”。
执行“usermod -s /bin/bash www”
ssh-keygen 用来生成ssh登录的key,可以使我们以后登录远程主机时无需再输入远程主机的key
ssh-keygen -t rsa #一路回车,不需要设置密码
复制公钥文件
cp /home/www/.ssh/id_rsa.pub /tmp/
将file1中的内容追加到file2中,即将file1内容合并到file2中,并不覆盖file2原来已有内容。
cat /tmp/id_rsa.pub >>/home/www/.ssh/authorized_keys
复制自己公钥到 authorized_keys 中
cat /tmp/chengcheng.pub >>/home/www/.ssh/authorized_keys
设置600权限(必须)
chmod 600 /home/www/.ssh/authorized_keys
跳转到yidian目录
cd /data/www/yidian
2、开始配置
初始化空的 Git 版本库于 /data/www/yidian/.git/
git init
touch readme
添加当前工作目录文件到index
git add .
配置用户名
git config --global user.name "chengcheng"
配置用户邮箱
git config --global user.email chengcheng@yidian-mall.com
查看origin仓库
git config branch.master.remote origin
合并到远程仓库
git config branch.master.merge refs/heads/master
git commit -a -m "init yidian-git"
git remote add origin www@192.168.47.131:/data/www/yidian/.git
git push origin master
git config receive.denyCurrentBranch ignore
开始测试 pull 和 push 代码:
www@192.168.47.131:/data/www/yidian/.git
然后去 /data/www/yidian 目录下看是否存在 push 上去的文件。
注:如果push过后发现服务器不存在文件
下载地址:https://pan.baidu.com/s/1he1_Td1_fjoBtWM1TIewrg 密码:3qaq
需要在“/data/www/yidian/.git/hooks”目录下,添加“post-receive”文件,并且设置775权限。
复制文件上去后,执行
chmod 775 post-receive
su - root
cd /data/www/yidian/.git/hooks
chown -R www.www .
3、问题解决:
1、git push origin master出现如下错误:
Counting objects: 3, done.
Writing objects: 100% (3/3), 226 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@192.168.45.42:teamwork.git
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'git@192.168.45.42:teamwork.git'
执行命令解决问题
2、git pull出现如下错误:
You asked me to pull without telling me which branch you
want to merge with, and 'branch.master.merge' in
your configuration file does not tell me, either. Please
specify which branch you want to use on the command line and
try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) for details.
If you often merge with the same branch, you may want to
use something like the following in your configuration file:
[branch "master"]
remote = <nickname>
merge = <remote-ref>
[remote "<nickname>"]
url = <url>
fetch = <refspec>
See git-config(1) for details.
执行命令:
$ git config branch.master.remote origin
$ git config branch.master.merge refs/heads/master
这是由于git默认拒绝了push操作,需要进行设置,修改.git/config添加如下代码:
[receive]
denyCurrentBranch = ignore
其他:
在初始化远程仓库时最好使用 git --bare init 而不要使用:git init
如果使用了git init初始化,则远程仓库的目录下,也包含work tree,
当本地仓库向远程仓库push时,如果远程仓库正在push的分支上(如果当时不在push的分支,就没有问题),
那么push后的结果不会反应在work tree上,也即在远程仓库的目录下对应的文件还是之前的内容,
必须得使用git reset --hard才能看到push后的内容.
成功后截图:
通过以上方法可以完美安装和配置 git 环境。