新建分支和建立映射的方式:
一:
二:
三:
分支的修改提交:
分支的推送:
合并分支:
解决合并多个分支时的冲突:
-
主分支中执行:
-
获取当前分支状态:
-
选择解决冲突原始工具:
-
冲突解决完成后,提交文件:
IDE解决冲突,phpstorm为例:
分支完成开发使命后的删除:
分支代码回滚:
-
本地代码库分支回滚:
-
远程代码库回滚:
-
-
这个是重点要说的内容,过程比本地回滚要复杂
-
应用场景:自动部署系统发布后发现问题,须要回滚到某一个commit,再从新发布
-
原理:先将本地分支退回到某个commit,删除远程分支,再从新push本地分支
-
操做步骤:
-
git checkout the_branch
-
git pull
-
git branch the_branch_backup //备份一下这个分支当前的状况
-
git reset --hard the_commit_id //把the_branch本地回滚到the_commit_id
-
git push origin :the_branch //删除远程 the_branch
-
git push origin the_branch //用回滚后的本地分支从新创建远程分支
-
git push origin :the_branch_backup //若是前面都成功了,删除这个备份分支
-
若是使用了gerrit作远程代码中心库和code review平台,须要确保操做git的用户具有分支的push权限,而且选择了 Force Push选项(在push权限设置里有这个选项)
服务器上部署Git 并建立 webhook:
-
使用php执行webhook,注意事项:
-
linux下php的执行用户,权限;
-
切记hook操做的目标文件,不要用高权限用户修改或者pull等操做(例root),不然会形成hook失效,后面再思考是否能够避免这样的状况。
-
作钩子大可能是走 ssh 协议, 须要coding 里配置部署公钥
Setting->network->SSH
C:\Program Files\TortoiseGit\bin\TortoiseGitPlink.exe
替换为本地Git执行文件路径
X:\Program Files\Git\usr\bin\ssh.exe
<?php
error_reporting(1);
$output = shell_exec('id -a');//判断是不是www:www在执行
$action_user = "<pre>$output</pre>";
file_put_contents('./log/user_action.log',"USER_ACTION::".$action_user.$output. PHP_EOL , FILE_APPEND);
$target = '/usr/local/nginx/html/git_work/forecast_admin/forecast'; // 测试环境web目录
$token = 'forecast'; //token校验暂不启用
$wwwUser = 'www';
$wwwGroup = 'www';
$json = json_decode(file_get_contents('php://input'), true);
file_put_contents('./log/user_action.log',"HOOK_DATA::".file_get_contents('php://input'). PHP_EOL , FILE_APPEND);
$cmds = array(
"cd $target && git pull origin develop",
"chown -R {$wwwUser}:{$wwwGroup} $target/",
);
foreach ($cmds as $cmd) {
$shellExec = shell_exec($cmd);
if($shellExec == NULL) {
file_put_contents('./log/test_try2.log', 'shell success'.$cmd, FILE_APPEND);
} else {
file_put_contents('./log/test_try_error.log', 'shell false'.$cmd, FILE_APPEND);
}
}
此外,关于git webhook和php发布 待了解的两个公共包:
1.composer cpliakas/git-wrapperphp
2.composer deploy