新建分支和建立映射的方式:
一:
二:
三:
分支的修改提交:
分支的推送:
合併分支:
解決合併多個分支時的衝突:
-
主分支中執行:
-
獲取當前分支狀態:
-
選擇解決衝突原始工具:
-
衝突解決完成後,提交文件:
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