美文网首页git
git使用笔记

git使用笔记

作者: Best博客 | 来源:发表于2019-06-28 15:03 被阅读0次

    常用命令:

    1.拉去远程指定仓库指定分支
    gi clone -b feature/alipay https://github.com/linyuyizhipao/http.git ./
    2.本地仓库跟远程仓库同步信息,但又不改变你本地的代码,可以理解成本地仓库跟远程仓库信息同步
    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 //强制性让远程分支跟本地保持一致,慎重操作
    git status //现在的代码仓库状态,没事多用用
    git branch -a //显示本地分支和远程的,当本地信息没有远程新的时候,git fetch 就可以使用了,让他们保持一致
    git branch -d 分支名称 //删除本地分支
    git push origin :远程分支名称 // 将本地删除的行为同步到远程
    git tag -l -n //显示本地tag,并展示备注信息
    git tag -d v1.0 //删除
    git tag -a V2.0.1 -m '新打一个标签,是以当前所在commitid记录的,当然你可以自行定制'
    git push origin --tags //本地所有tag推到远程
    git push origin V2.0.0 //本地指定的V2.0.0 推到远程
    git stash //本地已经做了修改,但是线上来了一个bug,本地修改还未完成又不想提交,可暂时封存
    git stash pop //将封存的拿出来
    git remote -v //显示远程仓库
    git remote add origin https://github.com/linyuyizhipao/http.git // 添加本地分支指向的远程分支地址。(origin :远程名字)
    git remote set-url origin https://github.com/linyuyizhipao/http.git // 设置本地分支指向的远程分支地址
    git diff //亲测,不加参数表示本地工作区与本地commit最新一次版本代码做对比,并显示差异
    git diff --cached //亲测,显示本地暂存区与本地commit最新版本做对比(- 的标志是旧的 ,+的标志是新的)
    git reset HEAD //本地所有的暂存区的修改都放弃掉
    git rm --cached '文件路径' //删除暂存区的指定文件
    git reset --hard 'commitid' // 版本回退到指定commitid
    git config --global credential.helper store //输入此命令后,在操作git如果出现输入账户密码的行为后,系统机会记录下来,下次就不会再让你输入密码了

    git revert commit_id之后并不会回滚到该id的内容,而是将该id的内容给逆向操作一遍,比如说,a操作添加了“haha”,commit了a,b操作添加了“xixi”,commit b。现在想回滚到只添加了“haha”,需要的是删除“xixi”,也就是逆向操作b,所以应该git revert b的commit_idgit revert 应该翻译成“反转、逆转”比较好理解,而不是回退。

    git branch | grep -v -E "master|dany_kefu|feature/official-event|dany_ms" | xargs git branch -D //双引号(“”)中写你本地要保留的git分支名称,不在此内的本地分支将全部被删除
    git branch -a | grep -v -E "master|dany_kefu|release/v2.0" | sed 's/remotes/origin///g' | xargs git push origin --delete //双引号(“”)中写你要保留的远程git分支名称,不在此内的远程分支将全部被删除
    git rm -r --cached ./ (删除缓存目录) git rm --cached ./index.php(删除缓存文件) //如果你本地的git仓库分支已经追踪了./index.php,但是你现在不想index.php跟git跑了,那么先执行这个命令即可,然后再修改你的忽略文件,你便会发现不跟git跑了
    git reflog 例如执行 git reset --hard HEAD~1,退回到上一个版本,用git log则是看不出来被删除的commitid,用git reflog则可以看到被删除的commitid,我们就可以买后悔药,恢复到被删除的那个版本。
    git cherry-pick 946992 指定commit的提交合并到当前分支。当master分支需要feature/goods 分支的某一次提交的内容,就可以使用这个命令了

    <?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 里面的所有本地加远程分支
    * @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));
                //远程
                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));
            //远程
            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);
            }
        }
    
    
    }
    
    
    
    
    
    

    相关文章

      网友评论

        本文标题:git使用笔记

        本文链接:https://www.haomeiwen.com/subject/xvwscctx.html