常用命令:
1.拉去遠(yuǎn)程指定倉庫指定分支
gi clone -b feature/alipay https://github.com/linyuyizhipao/http.git ./
2.本地倉庫跟遠(yuǎn)程倉庫同步信息,但又不改變你本地的代碼,可以理解成本地倉庫跟遠(yuǎn)程倉庫信息同步
git fetch
3.本地分支切換,在已有分支切換
git checkout feature/weixin
4.本地新建一個功能分支
git checkout -b feature/new
5.放棄本地修改的未git add -A 的代碼,但是新建的文件不起作用( . 可以是指定文件)
git checkout .
6.常用的
git add -A
git commit -m '備注'
git commit --amend 這個命令也是提交代碼到 本地倉庫,但是是自動追加到你本地上一次的commitid里面去了
git fetch
git pull origin master
git push origin master
git push -f origin master //強制性讓遠(yuǎn)程分支跟本地保持一致,慎重操作
git status //現(xiàn)在的代碼倉庫狀態(tài),沒事多用用
git branch -a //顯示本地分支和遠(yuǎn)程的,當(dāng)本地信息沒有遠(yuǎn)程新的時候,git fetch 就可以使用了,讓他們保持一致
git branch -d 分支名稱 //刪除本地分支
git push origin :遠(yuǎn)程分支名稱 // 將本地刪除的行為同步到遠(yuǎn)程
git tag -l -n //顯示本地tag,并展示備注信息
git tag -d v1.0 //刪除
git tag -a V2.0.1 -m '新打一個標(biāo)簽,是以當(dāng)前所在commitid記錄的,當(dāng)然你可以自行定制'
git push origin --tags //本地所有tag推到遠(yuǎn)程
git push origin V2.0.0 //本地指定的V2.0.0 推到遠(yuǎn)程
git stash //本地已經(jīng)做了修改,但是線上來了一個bug,本地修改還未完成又不想提交,可暫時封存
git stash pop //將封存的拿出來
git remote -v //顯示遠(yuǎn)程倉庫
git remote add origin https://github.com/linyuyizhipao/http.git // 添加本地分支指向的遠(yuǎn)程分支地址。(origin :遠(yuǎn)程名字)
git remote set-url origin https://github.com/linyuyizhipao/http.git // 設(shè)置本地分支指向的遠(yuǎn)程分支地址
git diff //親測,不加參數(shù)表示本地工作區(qū)與本地commit最新一次版本代碼做對比,并顯示差異
git diff --cached //親測,顯示本地暫存區(qū)與本地commit最新版本做對比(- 的標(biāo)志是舊的 ,+的標(biāo)志是新的)
git reset HEAD //本地所有的暫存區(qū)的修改都放棄掉
git rm --cached '文件路徑' //刪除暫存區(qū)的指定文件
git reset --hard 'commitid' // 版本回退到指定commitid
git config --global credential.helper store //輸入此命令后,在操作git如果出現(xiàn)輸入賬戶密碼的行為后,系統(tǒng)機會記錄下來,下次就不會再讓你輸入密碼了
git revert commit_id之后并不會回滾到該id的內(nèi)容,而是將該id的內(nèi)容給逆向操作一遍,比如說,a操作添加了“haha”,commit了a,b操作添加了“xixi”,commit b?,F(xiàn)在想回滾到只添加了“haha”,需要的是刪除“xixi”,也就是逆向操作b,所以應(yīng)該git revert b的commit_id。 git revert 應(yīng)該翻譯成“反轉(zhuǎn)、逆轉(zhuǎn)”比較好理解,而不是回退。
git branch | grep -v -E "master|dany_kefu|feature/official-event|dany_ms" | xargs git branch -D //雙引號(“”)中寫你本地要保留的git分支名稱,不在此內(nèi)的本地分支將全部被刪除
git branch -a | grep -v -E "master|dany_kefu|release/v2.0" | sed 's/remotes/origin///g' | xargs git push origin --delete //雙引號(“”)中寫你要保留的遠(yuǎn)程git分支名稱,不在此內(nèi)的遠(yuǎn)程分支將全部被刪除
git rm -r --cached ./ (刪除緩存目錄) git rm --cached ./index.php(刪除緩存文件) //如果你本地的git倉庫分支已經(jīng)追蹤了./index.php,但是你現(xiàn)在不想index.php跟git跑了,那么先執(zhí)行這個命令即可,然后再修改你的忽略文件,你便會發(fā)現(xiàn)不跟git跑了
git reflog 例如執(zhí)行 git reset --hard HEAD~1,退回到上一個版本,用git log則是看不出來被刪除的commitid,用git reflog則可以看到被刪除的commitid,我們就可以買后悔藥,恢復(fù)到被刪除的那個版本。
git cherry-pick 946992 指定commit的提交合并到當(dāng)前分支。當(dāng)master分支需要feature/goods 分支的某一次提交的內(nèi)容,就可以使用這個命令了
<?php
const REMOVE_STR = 'remotes/origin/';
//待清理的路徑
$localBranchPath = [
'/var/www/yii_customer_service',
'/var/www/yii_dany_customer',
'/var/www/yii_finley_customer',
'/var/www/yii_hugo_customer',
'/var/www/yii_joe_customer',
'/var/www/yii_kita_customer',
'/var/www/yii_liver_customer',
'/var/www/yii_sizz_customer',
];
//需要保留的分支名
$validBranch = [
'master',
'dany_kefu',
'joe',
'feature/sizz_receive_sort_20191012',
'feature/sizz_receive_waiting_sort_20191010',
'sizz_dev',
'finley_PCchat',
'finley_menu',
'finley_plugin',
];
foreach ($localBranchPath as $key=>$value){
clearBranch($value,$validBranch);
}
/**
* 清除不在$validBranch 里面的所有本地加遠(yuǎn)程分支
* @param string $paths 待管理的git路徑
* @param string $validBranchs 保留的分支名稱
*/
function clearBranch($paths,$validBranchs){
$branchs = shell_exec("cd {$paths} && git branch -a");
$branchArr = explode("\n",$branchs);
foreach ($branchArr as $k=>$v){
if(strstr($v,'remotes/origin/HEAD ')){
continue;
}
$branchName = substr($v,2);
$subStr = substr($branchName,0,strlen(REMOVE_STR));
if($subStr == REMOVE_STR){
$branchName = substr($branchName,strlen(REMOVE_STR));
//遠(yuǎn)程
if(!in_array($branchName,$validBranchs)){
$gitStr = "cd {$paths} && git push origin :".$branchName;
if($branchName == 'feature/menu'){
shell_exec($gitStr);
}
}
}else{
//本地
if(!in_array($branchName,$validBranchs)){
shell_exec("cd {$paths} && git branch -D ".$branchName);
}
}
}
}
<?php
const REMOVE_STR = 'remotes/origin/';
$branchs = shell_exec("git branch -a");
$volidBranch = [
'master',
'feature/keyword_group_dany_20191204',
'feature/sizz_batch_switch_20191203',
'feature/sizz_opt_20191202',
'feature/hugo_image_20191122',
'publish/hugo_image_20191122',
'laker',
'finley_PCchat',
];
$branchArr = explode("\n",$branchs);
foreach ($branchArr as $k=>$v){
if(strstr($v,'remotes/origin/HEAD ') || empty($v)){
continue;
}
$branchName = substr($v,2);
$subStr = substr($branchName,0,strlen(REMOVE_STR));
if($subStr == REMOVE_STR){
$branchName = substr($branchName,strlen(REMOVE_STR));
//遠(yuǎn)程
if(!in_array($branchName,$volidBranch)){
$gitStr = "git push origin :".$branchName;
if(!in_array($branchName,$volidBranch)){
shell_exec($gitStr);
}
}
}else{
//本地
if(!in_array($branchName,$volidBranch)){
shell_exec("git branch -D ".$branchName);
}
}
}