git安装
git安装
git官网进不去怎么办
http://www.nndssk.com/xtjc/297012suWk23.html
windows安装git
https://git-scm.com/downloads 客服端下载
http://www.cnblogs.com/shenyf/p/8379318.html
git config --global user.name "xxxxx"
git config --global user.email "xxxxx@qq.com"
# 设置git超时时长
git config --global http.lowSpeedLimit 1000
git config --global http.lowSpeedTime 600
# 以上配置后,只有十分钟(600秒)传输速率都低于 1KB/s 的话才会 timeout
cd ~
ssh-keygen -t rsa -C "xxxxx@qq.com"
将id_rsa.pub 密匙放入GitHub SSH配置
ssh -T git@github.com 检测是否成功
linux安装git
yum install -y git
git --version
本地免密登陆
服务器中 git 主目录为 /home/git 如果目录中没有 .ssh 文件夹 则创建
cd /home/git
mkdir .ssh
cd .ssh
touch authorized_keys
修改 .ssh 文件夹 和 authorized_keys 的权限
chmod 700 .ssh
chmod 600 authorized_keys
检查 配置文件
vim /etc/ssh/sshd_config
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
ssh-keygen -t rsa //本地和服务器执行该命令一直回车生成公匙
#在本地追加到 服务器的 authorized_keys 文件中
ssh git@xx.xx.xx.xx 'cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub
git推送到服务器自动同步到站点目录
参考 https://blog.csdn.net/qq_34284638/article/details/70154557
使用第三方github,gutee跳过此步
useradd git #给Linux系统添加git用户
passwd git #设置git用户密码
#需要设置两个仓库 一个上传仓库 一个本地仓库
mkdir -p /home/git/test.git #本地仓库
git init --bare /home/git/test.git
cd /home/git/test.git/hooks
touch post-receive #创建自动执行文件
vi post-receive
#!/bin/sh
unset GIT_DIR
#进入仓库B目录
cd /home/git/test
#执行git pull命令
git pull /home/git/test.git master
#返回仓库A并退出
cd /home/git/test.git
exit 0
#post-receive 给个权限
chmod +x /home/git/test.git/hooks/post-receive
#创建上传仓库 并拉取代码
mkdir -p /home/git/test
cd /home/git/test
git clone /home/git/test.git
#授权
sudo chown -R git:git /home/git/test.git
sudo chown -R git:git /home/git/test
#本地拉取代码
git clone git@xx.xx.xx.xx:/home/git/test.git
使用webhooks自动部署Github、GitLab、Gitee代码
ssh 拉取代码
步骤
生成密匙
sudo -u www ssh-keygen -t rsa -C "xxxx@qq.com"
将 /home/www/.ssh/id_rsa.pub 放到gitee ssh配置里面
chmod 700 /home/www/.ssh
chmod 600 /home/www/.ssh/*
vim /etc/passwd
设置 bin/bash
www:x:1000:1000:,,,:/home/www:/bin/bash
git:x:1001:1001:,,,:/home/git:/bin/bash
执行命令 visudo (ctrl+o保存 ctrl+x退出)
添加
git ALL=(ALL) NOPASSWD: ALL
www ALL=(ALL) NOPASSWD: ALL
设置 /www/wwwroot 用户组 www
sudo chown -R www:www /www/wwwroot
使用www登录
su www -s /bin/bash
拉取代码
sudo -u www -H git clone git@gitee.com:oorzc/xxx.git
赋予权限
sudo chown -R www:www /www/wwwroot/xxx
或者 http拉取
git clone http://xxx/xxx/xxx.git
sudo chown -R www:www /www/wwwroot/xxx
修改 .git config 文件
url = http://用户名:密码@xxx/xxx/xxx.git
http 拉取代码
拉取项目
git clone http://xxx:123456@xxx.xx.xxx.xx/oorzc/xxx.git
设置
/www/wwwroot/xxx/.git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = http://xxx:123456@xxx.xx.xxx.xx/oorzc/xxx.git #配置此处 拉取代码账号密码
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
sudo chown -R 777 /www/wwwroot/xxx/.git
<?php
/*
* 这里有几点需要注意:
*
* 1.确保PHP正常执行系统命令。写一个PHP文件,内容:
*php.ini 删除 shell_exec 禁用函数
*
* `<?php shell_exec('ls -la')`
* 在通过浏览器访问这个文件,能够输出目录结构说明PHP可以运行系统命令。
*
* 2、PHP一般使用www-data或者nginx用户运行,PHP通过脚本执行系统命令也是用这个用户,
* 所以必须确保在该用户家目录(一般是/home/www-data或/home/nginx)下有.ssh目录和
* 一些授权文件,以及git配置文件,如下:
* ```
* + .ssh
* - authorized_keys
* - config
* - id_rsa
* - id_rsa.pub
* - known_hosts
* - .gitconfig
* ```
*
* 3.在执行的命令后面加上2>&1可以输出详细信息,确定错误位置
* 1.创建秘钥(注:使用www用户创建 ,防止执行的时候没有权限)sudo -u www ssh-keygen -t rsa //根据提示进行对应的ssh配置
*
* 4.git目录权限问题。比如:
* `fatal: Unable to create '/web/public/xxx/.git/index.lock': Permission denied`
* 那就是PHP用户没有写权限,需要给目录授予权限:
* ``
* useradd git #给Linux系统添加git用户
* passwd git #设置git用户密码
*
* useradd www
* sudo chown -R www:www /web/public/xxx
* ```
*
*
*
* 5.SSH认证问题。如果是通过SSH认证,有可能提示错误:
* mkdir /home/www/.ssh
*
* `Could not create directory '/.ssh'.`
* 或者
* `Host key verification failed.`
*
*/
echo error_reporting(E_ALL);
// 本地仓库路径
$local = '/web/public/xxx';
// 安全验证字符串,为空则不验证
$token = '123456';
// 如果启用验证,并且验证失败,返回错误
$httpToken = isset($_SERVER['HTTP_X_GITEE_TOKEN']) ? $_SERVER['HTTP_X_GITEE_TOKEN'] : '';
if ($token && $httpToken != $token) {
header('HTTP/1.1 403 Permission Denied');
die('Permission denied.');
}
// 如果仓库目录不存在,返回错误
if (!is_dir($local)) {
header('HTTP/1.1 500 Internal Server Error');
die('Local directory is missing');
}
//如果请求体内容为空,返回错误
$payload = file_get_contents('php://input');
if (!$payload) {
header('HTTP/1.1 400 Bad Request');
die('HTTP HEADER or POST is missing.');
}
$content = json_decode($payload, true);
//若是主分支且提交数大于0
if ($content['total_commits_count']>0) {
$res = shell_exec("cd {$local} && git pull 2>&1");//以nginx用户运行
$res_log = '-------------------------'.PHP_EOL;
$res_log .= $content['user_name'] . ' 在' . date('Y-m-d H:i:s') . '向' . $content['repository']['name'] . '项目的' . $content['ref'] . '分支push了' . $content['total_commits_count'] . '个commit:' . PHP_EOL;
$res_log .= $res.PHP_EOL;
file_put_contents("git-webhook.txt", $res_log, FILE_APPEND);//追加写入
}
提交到多个仓库
git remote add github http://xxxxxxxx.git (如果是clone下来的可以省略)
git remote add gitlab http://xxxxxxxx.git;
git push github master:master;
简写 git push github
git push gitlab master:cafe-pages;
简写 git push gitlab
修改提交地址 /xxx/.git/config
提交到多个分支
git branch #查看分支
git branch online #创建分支
git checkout online #切换分支
git merge master #将master分支合并到当前分支
git push origin online (online为要提交代码的分支名称) #提交代码到指定的分支
git clone -b online xxx #git拉取指定分支的代码 xxx为仓库地址
#参考
#git clone -b online http://x'x.xx.xx.xx:xxx/xxx/xx.git xx
#sudo chown -R www:www /www/wwwroot/xx/xx
git 权限控制
使用教程
https://juejin.im/entry/5bab71655188255c3272c2fc
https://github.com/xirong/my-git/blob/master/git-workflow-tutorial.md 冲突解决
Git 版本控制器
配置
git config --list #查看配置的信息
git config --global user.name Nick #设置用户名
git config --global user.email nick_php@qq.com #设置邮箱
git help config #获取帮助信息
普通操作
git status #查看已添加到暂存区文件状态
git diff #查看未添加暂存区文件状态
git add file #添加文件
git add . #添加当前目录下所有文件到版本库
git add -A #添加所有文件到版本库
git commit -m '注释' #提交
git push #推送到仓库
git pull #拉取当前分支仓库最新代码
git pull dev #拉取指定分支的代码与本地分支合并
git reset --hard #回滚到上版本
git reset --hard af442cb672b02cdfca1fcb #回滚到指定的版本
git checkout . #恢复暂存区的所有文件到工作区
git checkout file #恢复暂存区的指定文件到工作区
git checkout af442cb672b02cdfca1fcb index.php 恢复某个 commit 的指定文件到暂存区和工作区
新建仓库
mkdir www && cd www
git init #初始化
git status #查看文件状态
git add file #.或*代表全部添加
git commit -m "message"#此处注意乱码
git remote add origin git@github.com:username/project.git #关联远程仓库
git push -u origin master #第一次推送文件到远程仓库
克隆仓库
git clone https://github.com/xxx/gitbook.git #克隆远程仓库
git clone https://github.com/xxx/gitbook.git linux #克隆并指定目录名称
日志
git log #查看提交日志
git reflog #查看提交日志 git reset 回滚版本依然有日志,可以撤销回滚
git log --pretty=oneline #单行显示提交日志
git log --author=nick #查看指定用户的日志
git log -p filename #查看文件每次提交的修改部分
分支
git branch #查看本地分支
git branch -r #查看远端分支
git branch -a #查看所有分支
git branch test #新建 test 分支
git checkout -b dev ##新建 dev 分支并切换
git checkout - #切换到上一个分支
git checkout -b test dev #基于 dev 新建 test 分支,并切换
git merge test #将test分支合并到当前分支
git branch -m old new #重命名分支
git branch -M old new #强制重命名分支
git push origin branch #推送分支到远程
git branch -d branch #删除本地分支
git branch -D branch #强制删除本地分支(当分支内容有修改并且已经 commit 时,分支没有合并,需要强制删除)
git push origin :branch #删除远程分支
标签
git tag #列出现有标签
git tag v0.1#新建标签
git tag -a v0.1 -m 'my version 1.4'#新建带注释标签
git checkout tagname #切换到标签
git push origin v1.5 #推送分支到源上
git push origin --tags #一次性推送所有分支
git tag -d tag #删除本地 tag
git push origin :tag #删除远程 tag
关联远程仓库
git remote -v #查看全部远程仓库
git remote add origin https://github.com/xxx/gitbook.git #添加本地仓库与远程仓库关联
git remote rename origin github #重命名
git remote remove origin #取消与远程仓库关联
git忽略文件
例如.DS_Store
查找所有
find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch
因为您可能永远不想包含.DS_Store文件,所以制定一个全局规则。 首先,在某处创建一个全局.gitignore文件,例如
echo .DS_Store >> ~/.gitignore_global
node_modules
.idea
.vscode
.gradle
.DS_Store
*.DS_Store
现在告诉 git 将它用于所有存储库:
git config --global core.excludesfile ~/.gitignore_global
git忽略已提交文件和文件夹
git rm -r --cached www
修改历史提交用户和用户邮箱
#拉取代码
git clone --mirror https://your-repo-url.git
cd your-repo-name.git
git filter-branch --env-filter '
if [ "$GIT_COMMITTER_EMAIL" = "old@example.com" ]
then
GIT_COMMITTER_NAME="New Name"
GIT_COMMITTER_EMAIL="new@example.com"
fi
if [ "$GIT_AUTHOR_EMAIL" = "old@example.com" ]
then
GIT_AUTHOR_NAME="New Name"
GIT_AUTHOR_EMAIL="new@example.com"
fi
' --tag-name-filter cat -- --branches --tags
打开git受保护分支,运行强制提交
git push --mirror --force