• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • 在 centOS 上关联 Git 远程仓库(本地客户端)

    在远程服务器端中,用户种类:

    • root:系统超级用户,属于 root 组。禁止 ssh 登录远程服务器 shell。
    • admin:自定义管理员用户,属于 wheel 组。允许 ssh 登录远程服务器 shell。
    • developer:自定义普通用户,属于 develop 组。允许 ssh 登录远程服务器 shell。对项目程序代码文件,是所有者。scp、sftp 下载上传文件。
    • git:自定义普通用户,属于 develop 组。禁止 ssh 登录远程服务器 shell。对共享的 Git 仓库是所有者。Git 上传下载 Git 远程仓库。

    在本机客户端中,用户种类:

    • root:系统超级用户,属于 root 组。进入本机 ceonOS 时,默认使用的用户。为了个人方面,经常使用。


    克隆远程服务器端 Git 项目仓(存在项目分支)

    在远程服务器端存在 Git 仓库,本机能使用 SSH 协议,克隆代码。

    cd   /var/web/www
    git clone git@192.168.0.50:/var/web/wwwgit/project.git
    
    # 查看源配置
    git remote -v
    


    # 需要修改源的时候,使用命令
    git remote set-url origin git@example:/var/web/wwwgit/newproject.git
    
    # 需要修改源的时候,也可以,先删除旧源,再添加新源。
    git remote remove origin
    git remote add origin git@example:/var/web/wwwgit/newproject.git
    


    本地仓库关联服务器端 Git 仓库(空仓)

    在远程服务器端存在 Git 仓库,里面没有任何内容,不存在项目,是个空仓,而项目代码在本机电脑上。

    第一步:初始化项目

    # 进入项目根目录
    cd /var/web/www/my_project
    
    # # 修改所有者、所属组
    sudo chown -R developer:develop /var/web/www/my_project
    


    编辑本地.gitignore文件,设置需要过滤的目录以及文件。根据自己的需要,自己修改:

    /public/.htaccess
    /public/baidumap.xml
    /public/sitemap.xml
    /public/upload/robotLog.txt
    /public/upload/gather/
    /public/build
    /public/hot
    /public/storage
    /bootstrap/cache
    /database/migrations
    /storage/
    /node_modules/
    /.fleet
    /.idea
    /.vscode
    .env
    .env.backup
    .env.production
    .phpunit.result.cache
    *.psd
    *.md
    Homestead.json
    Homestead.yaml
    auth.json
    npm-debug.log
    yarn-error.log
    

    编辑本地.gitattributes文件,设置需要过滤的目录以及文件。根据自己的需要,自己修改:

    * text=auto
    *.png -text
    *.jpg -text
    *.jpeg -text
    *.gif -text
    *.svg -text
    *.ico binary
    
    *.blade.php diff=html
    *.css diff=css
    *.html diff=html
    *.md diff=markdown
    *.php diff=php
    
    /.github export-ignore
    CHANGELOG.md export-ignore
    


    # 本地项目初始化
    git init
    
    # 把现有项目文件添加到缓存区:
    git add .
    

    若提交的时候报错:fatal: CRLF would be replaced by LF in xxx

    # 使用工具处理单个文件
    dos2unix xxx.file
    
    # 进入项目根目录,递归地把当前目录下子目录 app 下的所有文件变成 unix 格式
    find ./app -type f -exec dos2unix {} \;
    
    # 执行完毕,格式转化后,再次提交:
    git add .
    


    # 把现有项目文件提交到本地仓库:
    git commit -m 'initial'
    
    # 查看分支:
    git branch
    
    # 显示结果,只有一个 master
    * master
    


    第二步:关联远程 Git 仓库

    # 关联远程服务端仓库
    git remote add origin git@192.168.0.1:/var/web/wwwgit/project.git
    
    # 显示所有远程仓库
    git remote -v
    
    • git是登录服务端用户。
    • 192.168.0.1服务器端 IP 地址,根据自己的实际情况修改。
    • /var/web/wwwgit/project.git,服务器端 Git 项目仓库。
    # 查看本地分支:
    git branch
    
    # 查看远程分支:
    git branch -r
    
    # 查看全部分支:
    git branch -a
    


    第三步:首次上传代码

    # 首次上传本地 Git 代码。参数 -u 跟踪上游分支
    git push -u origin master
    
    # 显示如下:
    git@192.168.0.1's password: 输入 git 登录密码
    Counting objects: 10619, done.
    Delta compression using up to 6 threads.
    Compressing objects: 100% (10344/10344), done.
    Writing objects:  21% (2230/10619), 46.87 MiB | 1.94 MiB/s
    


    第四步:以后先下拉代码,再上传代码

    # 把 Git 仓库 master 分支 拖拉到本地
    git pull origin master
    
    # 上传本地 Git 代码,不需要参数 -u
    git push origin master
    


    本地仓库关联服务器端 Git 仓库(存在项目)

    在远程服务器端存在 Git 仓库,里面存在项目,而本机电脑上,也存在项目代码在。

    其使用的办法跟上面的,【本地仓库关联服务器端 Git 仓库(空仓)】,基本相同。第一步:初始化项目。第二步:关联远程 Git 仓库(与上面相同)

    第三步:合并远程分支

    # 下载远程分支,进入本地远程仓库
    git fetch origin
    
    # 查看全部分支:
    git branch -a
    
    # 显示分支情况
    * master
      remotes/origin/master
    
    # 把远程仓分支 master 合并到本地仓 master 上
    git merge origin/master --allow-unrelated-histories
    
    Merge branch 'master' of www.xxx.cn:/var/web/wwwgit/xxxx
    # Please enter a commit message to explain why this merge is necessary,
    # especially if it merges an updated upstream into a topic branch.
    #
    # Lines starting with '#' will be ignored, and an empty message aborts
    # the commit.
    

    上面弹出的提示信息。保存退出即可。


    第四步:重新提交本地代码

    # 执行完毕,格式转化后,再次提交:
    git add .
    
    # 把现有项目文件提交到本地仓库:
    git commit -m 'OK'
    
    # 把 Git 仓库 master 分支 拖拉到本地
    git pull origin master
    
    # 上传本地 Git 代码,不需要参数 -u
    git push origin master
    



    常见错误

    合并策略:

    • git pull origin master:先尝试快进合并,如果不行再进行正常合并生成一个新的提交。
    • git pull --rebase origin master:先尝试快进合并,如果不行再进行变基合并。
    • git pull --ff-only origin master:只尝试快进合并,如果不行则终止当前合并操作。
    • git pull --no-ff origin master:禁止快进合并,即不管能不能快进合并,最后都会进行正常合并生成一个新的提交。

    强制合并:

    • git pull --allow-unrelated-histories origin master:允许不相关历史提交,强制下拽合并。
    • git push --force origin master:允许不相关历史提交,强制上推合并并。

    本地分支与远程分支建立关联:

    • git branch --set-upstream-to=origin/mastermaster:关联目的是在执行git pull,git push操作时就不需要指定对应的远程分支,你只要没有显示指定,git pull的时候,就会提示失败。



    fatal: Need to specify how to reconcile divergent branches

    这是由于你拉取 pull 分支前,进行过 merge 合并更新分支操作,而其他人在你之前已经 push 过一个版本,导致版本不一致。

    • 执行git config pull.rebase false
    • 默认将 pull 下来的代码与现有改动的代码进行合并
    • 但是可能会造成代码冲突,需要处理下这个问题,代码冲突如果 2 个人都改了同一个文件,需要联系之前 push 的同学,看看这块代码怎么保存
    git config pull.rebase false
    git pull origin master 
    


    fatal: detected dubious ownership in repository

    在 centOS 中,当前用户使用 git 命令,报错:

    fatal: detected dubious ownership in repository at '/var/web/www/my-project'
    To add an exception for this directory, call:
            git config --global --add safe.directory '/var/web/www/my-project'
    

    这是由于当前用户,与本地项目 git 仓库的所有者不同,导致的。切换到项目的所有者即可。若项目所有者的用户,已经被删除掉,那么更改项目以及本地项目 Git 仓的所有者,为当前用户,即可。

    # 把项目以及仓库的所有者改为 root
    chown -R root:root /var/web/www/my-project
    


    fatal: refusing to merge unrelated histories

    新建了一个仓库之后,把本地仓库进行关联提交、拉取的时候,出现了如下错误:

    git pull origin master
    fatal: refusing to merge unrelated histories
    
    gitt pull origin master --allow-unrelated-histories
    
    # 显示如下:
     * branch            master     -> FETCH_HEAD
    Auto-merging lanmper.sql
    CONFLICT (add/add): Merge conflict in public/upload/202212/20221230125702.png
    Automatic merge failed; fix conflicts and then commit the result.
    

    我这里由于使用了官方的.gitignore 自动合并失败,需要手动合并之后再进行 add、commit 即可

    git add .
    git commit -m 'initial'
    git merge master --allow-unrelated-histories