Git 常见问题汇总

git reabse

合并多个commit

git rebase -i HEAD~{NUM}
git rebase -i start end

会进入一个指定区间的commit列表 
根据提示进行文本编辑 选择是否合并 或者放弃某个提交

    ## 选项说明
    pick:保留该commit(缩写:p)
    reword:保留该commit,但我需要修改该commit的注释(缩写:r)
    edit:保留该commit, 但我要停下来修改该提交(不仅仅修改注释)(缩写:e)
    squash:将该commit和前一个commit合并(缩写:s)
    fixup:将该commit和前一个commit合并,但我不要保留该提交的注释信息(缩写:f)
    exec:执行shell命令(缩写:x)
    drop:我要丢弃该commit(缩写:d)

复制代码
编辑完成 :wq后 会自动执行rebase过程 
    期间可能会出现代码冲突 
 -- 动解决然后 执行git rebase --continue
 -- 或者执行 git rebase --abort 放弃rebase 过程
复制代码

git rm

版本控制中移除文件或文件夹

git rm --cache [file_path|dir_path]
复制代码

git webhook

每当本地push代码,还得在服务器上git pull。这样太麻烦了。git支持hook机制,类似事件通知,比如git服务器收到push请 求,并且接受完代码提交时触发。需要在hooks目录下创建post-receive文件 服务器操作

1. 配置仓库密匙 免密码push 请阅读此篇文章

Git - 生成 SSH公钥 , Linux 下多密钥管理

2. 创建可通过web 访问的hook文件

博主这里用的是php 用其他语言也是一样的 只要能调用到git pull 就OK

<?php
$pwd = '密码xxx';

error_reporting(E_ALL);
$gitPost = json_decode(file_get_contents("php://input"), true);
// file_put_contents('log', $gitPost);

if(isset($gitPost['password']) && $gitPost['password'] == $pwd){
    // 这里只是最简单的拉取代码,如果要做更加多的操作,如验证、日志,请自己解析push内容并操作
    // 获取push数据内容的方法
    $requestBody = file_get_contents("php://input");
    // 只需这一行代码便可拉取
    // 目录换成项目的目录
    $command = "cd /data/wwwroot/wx-luckyShop " .
        "&& git checkout master" .
        "&& git branch -D master-clone && git branch master-clone " .
        "&& git fetch " .
        "&& git reset --hard origin/master > GitPull.log " .
        "&& echo `cat GitPull.log`";
    $result = shell_exec($command);
    echo($result . "<br/>
");

    echo date("Y-m-d H:i:s",time());
    // 查看是哪个用户执行该命令
    // echo system("whoami");
}else{
    echo '非法访问';
}
复制代码

3. 为项目配置hook文件

转载于:https://juejin.im/post/5c26e145f265da6172658f8a

原文地址:https://www.cnblogs.com/twodog/p/12135385.html