【闲暇日记01】记一次使用 Git 的奇葩经历

发布于 2021-04-01  89 次阅读


导读

我不要写 PowerShell 啦!!!


1. 天坑

事情是这样的……

昨晚(今早)在敲毕设代码的时候遇到了个问题:

学校的毕设要求用学校提供的 Git 平台,也就是需要在指定的平台上完成代码迭代,但我个人是习惯将代码备份保存在 GitHub 上的。

问题来了,学校提供的 Git 平台和我 GitHub 的邮箱不一样,这直接导致了我每一次 push 到远程仓库的时候,必然有一个平台无法识别到提交该次代码的账号:

请输入图片描述

在翻遍了 Git 说明和网上的奇技淫巧后,我仍然没有解决方案。

最后我只能将目光看向 PowerShell 脚本。

2. 异想天开

最开始想的很简单,我能否在push之前先切换一次用户名和邮箱,来达到对不同仓库的不同账号信息提交呢?
于是就有了如下 PowerShell 代码:

function gitpush($where) {
    switch ($where) {
        '1'{
            git config user.name "name 1";
            git config user.email "email 1";
            git push -u github master
            break
        }
        '2'{
            git config user.name "name2";
            git config user.email "email 2";
            git push -u school master
            break
        }
        default{
            Write-Host " "
            Write-Host "Where?" -ForegroundColor "Yellow"
            Write-Host "Correct format:"
            Write-Host "    gitpush [AddressNo]"
            Write-Host " "
        }
    }
}

原理是,当我输入 gitpush 1 的时候,会自动切换到第一个账号信息并提交到GitHub上,gitpush 2 亦然、

但事实证明我想多了,账号信息是绑定在 commit 上的,而不是在 push 上,也就是说,我需要连 push行为也要分开处理。

这就代表着,我无论如何都不能够实现在一个文件夹内以不同身份推送代码到不同的远程仓库了。

3. 奇技淫巧

在与群友讨论后,我将目光锁定在“复制文件夹”,也就是,当我需要提交的时候,自动 copy 代码到另一个文件夹,然后在这两个文件夹之间分别提交到不同的仓库。

代码如下:

function pgit($title, $description) {
    Write-Host "Synchronize two folders..." -ForegroundColor "Yellow"
    robocopy "源文件" "目标文件" /E /mir /XD "源文件\node_modules" "源文件\.git";
    Write-Host "Synchronized completion";
    Write-Host " ";
    Write-Host " ";
    Write-Host "Pushing to school git..." -ForegroundColor "Yellow"
    Write-Host " ";
    cd "源文件夹";
    git add .
    If([String]::IsNullOrEmpty($description)) {
        git commit -m "$title";
    } Else {
        git commit -m "$title" -m "$description";
    }
    git push -u origin master;
    Write-Host " ";
    Write-Host " ";
    Write-Host "Pushing to github..." -ForegroundColor "Yellow"
    Write-Host " ";
    cd "目标文件夹";
    git add .
    If([String]::IsNullOrEmpty($description)) {
        git commit -m "$title";
    } Else {
        git commit -m "$title" -m "$description";
}
    git push -u origin master;
    cd "源文件夹"
    Write-Host " ";
    Write-Host " ";
    Write-Host "Finished!" -ForegroundColor "Yellow"
}

原理是,先通过系统自带的 robocopy 将除了 node_modules.git 两个文件夹排除后,剩余的文件复制到另一个文件夹。

然后 cd 到源文件夹,并根据传入的 message 提交更改(用if语句判断是否有第二个传参,如果有则提交为多行 message,否则为单行。

最后推送到远程仓库,并 cd 到另一个文件夹重复上述操作推送到另一个远程仓库。
请输入图片描述
大功告成!

04. 番外

在 PC 上配置完成后,还需要在笔记本上在配置一遍。
但很残念,没怎么接触过 zsh,且只会一点点 bash 语法,照猫画虎写了 bash 版,但也着实耗费了不少时间。

echo ""
echo ""
echo "\033[36m 请输入提交信息: \033[0m"
read -p "Title【必填】: " Title
if [ ! "$Title" ]; then
    echo ""
    echo ""
    echo -e "\033[31m Title 为必填项。 \033[0m"
    echo ""
    echo ""
    exit
fi
read -p "Discription【选填】: " Discription
echo ""
echo ""
echo "\033[36m 正在复制文件... \033[0m"
echo ""
echo ""
rsync -av --exclude ".git/" --exclude ".gitignore" --exclude "pgit.sh" --exclude "Front-end/node_modules" "/Users/yuuki/Programs/源目录/" "/Users/yuuki/Programs/目标目录/"
echo ""
echo ""
echo "\033[36m 复制完成。 \033[0m"
echo ""
echo ""
echo "\033[36m 推送到 School Git... \033[0m"
echo ""
echo ""
cd /Users/yuuki/Programs/源目录/
git add .
if [ ! "$Discription" ]; then
    git commit -m "$Title"
else
    git commit -m "$Title" -m "$Discription"
fi
git push -u origin master
echo ""
echo ""
echo "\033[36m 推送到 GitHub... \033[0m"
echo ""
echo ""
cd /Users/yuuki/Programs/目标目录/
git add .
if [ ! "$Discription" ]; then
    git commit -m "$Title"
else
    git commit -m "$Title" -m "$Discription"
fi
git push -u origin master
cd /Users/yuuki/Programs/源目录/
echo ""
echo ""
echo "\033[36m 完成 \033[0m"
echo ""
echo ""



版权声明:本文为原创文章,版权归 夕綺Yuuki 所有。

本文链接https://kira.cool/daily/370-Stupid-experience-with-Git

本文章采用 CC BY-NC-SA 4.0 International 协议进行许可。这意味着您可以自由的复制、转载和修改,惟须注明文章来源且禁止用于商业目的。

This article is licensed under the CC BY-NC-SA 4.0 International License, which means you are free to copy, distribute, and modify it, but must attribute the source and prohibit commercial use.