【git】之使用shell脚本提交代码

为减少提交步骤,防止提交错误,使用Shell脚本进行git提交不失一件好事

#!/bin/sh
# @author Hubal  
# @Email Hubal@123.com
# @createBy 2018-11-30
# Shell脚本提交git代码 简单,快速,高效
# 
author = Hubal
echo ' >>>>>> start push <<<<<< '  
echo " ====== 当前分支 ====== "  
branch= git branch
echo $branch 

# 判断参数1是否包含参数2
contains_str(){
    # echo " >>> $1 <<< "
    # echo " <<< $2"
    
    contains_result=$(echo $1 | grep "${2}")
    if [[ -n $contains_result  ]] ; then
          return 1
      else
          return 0     
    fi
    
}

git_add(){
    echo ">>>>>> 执行 git add 之前,本地文件状态如下 <<<<<<"
    git status 
    statusResult=$(git status)
    no_change="nothing to commit"

    contains_str "$statusResult" "$no_change"

    if [[ $? == 1 ]]; then
        echo "=== 当前没有新增或者修改的文件 ==="
        git_push
        exit
    fi

    read -p "是否确定add?Y|N : " add_params
    if [[ $add_params == "Y" || $add_params == "y" ]]; then 
            git add .
    else 
        exit 
    fi     
}

git_commit(){
     echo ">>>>>> 执行 git commit 之前,本地文件状态如下 <<<<<<"
     git status 
     read -p "是否确定commit?Y|N : " commit_params
     if [[ $commit_params == "Y" || $commit_params == "y" ]] ; then
             read -p "请输入commit信息: " commit_msg
             if [ -z $commit_msg  ] ; then 
                 git commit -m "git commit by $author" .
             else
                 git commit -m $commit_msg .    
             fi
     elif [[ $commit_params == "N" || $commit_params == "n" ]] ; then 
          exit 
     else 
         exit    
     fi
}

git_push(){
    echo ">>>>>> 执行 git push 之前,本地文件状态如下 <<<<<<"
    git status 
    current_branch=$(git symbolic-ref --short -q HEAD) 
    echo ">>>>>> 当前分支:$current_branch <<<<<<"
    read -p "是否确定push?Y|N : " push_confirm
    if [[ $push_confirm != "Y" &&  $push_confirm != "y" ]]; then
        echo ">>>>>> end push <<<<<<"
        exit
    fi
    read -p "请输入远程git地址别名,默认是origin: " origin_params 
    echo -e "\n"
    read -p "请输入远程分支名称,默认是当前分支: " branch_params
    push_result="";
    if [[ -z $origin_params && -z $branch_params ]]; then
        echo ">>>>>> push origin $current_branch"
        sleep 5 
        git push origin $current_branch 

    elif [[ -n $origin_params && -n $branch_params ]]; then
        echo ">>>>>> push $origin_params $branch_params"
        sleep 5 
        git push $origin_params $branch_params

    elif [[ -z $origin_params && -n $branch_params  ]]; then
        echo ">>>>>> push origin $branch_params"
        sleep 5 
        git push origin $branch_params

    elif [[ -n $origin_params && -z $branch_params  ]]; then
        echo ">>>>>> push $origin_params $current_branch"
        sleep 5 
        git push $origin_params $current_branch    
    else
        echo ">>>>>> end push <<<<<<"    
    fi
    
}



read -p "默认push当前分支,Q代表quit,其他单词代表切换分支 : " branch
if [[ $branch == "Y" || $branch == "y" || -z $branch ]] ; then 
        # echo  "你输入的是:  $branch "
        statusResult=$(git status)
        to_commit="Changes to be committed"
        contains_str "$statusResult" "$to_commit"
        if [[ $? != 1 ]]; then
            git_add;
        else 
            git add . 
            echo " ====== 本地没有需要add的文件,直接commit ====== "
        fi
        git_commit;
        git_push;
        exit;

elif [[ $branch == "Q" || $branch == "q" ]] ; then
        # echo "你输入的是: $branch ,代表退出当前操作!" 
        exit 
else  
    git checkout $branch
    echo -e "当前分支: \n $(git branch) "  
    git_add;
    git_commit;
    git_push;
    exit;
fi

 

转载于:https://www.cnblogs.com/gyjx2016/p/10058209.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Git指令的Shell脚本,能够快速便捷地管理Git库,包括添加修改、提交修改、显示库状态、推送到远程库、从远程库更新到本地、版本恢复等操作。 使用方法: 1. 在Linux系统中,将本文件放在Git库目录下,利用Shell运行本文件; 2.在windows系统中,需下载安装与操作系统相对应的Git软件,并将本文件放在Git库目录下,双击即可运行。 运行示例: Please choose the first letter of options. [Add|Commit|Diff|Fetch|Exit|Help|Log|Push|User|Reset|Status]? h A: Add all changes to repository. C: Commit all changes to repository. D: Show differences between current version with HEAD->. E: Exit shell script. F: Fetch origin/master and merge. L: Show latest two-weeks logs of repository. P: Push commissions to origin/master. U: User command mode(Press ‘Enter’ to exit). R: Reset current version according version_id. S: Show status of repository. Please choose the first letter of options. [Add|Commit|Diff|Fetch|Exit|Help|Log|Push|User|Reset|Status]? s On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: Git.sh modified: PyNote/PyNote_2.md no changes added to commit (use "git add" and/or "git commit -a") Please choose the first letter of options. [Add|Commit|Diff|Fetch|Exit|Help|Log|Push|User|Reset|Status]? a On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD ..." to unstage) modified: Git.sh modified: PyNote/PyNote_2.md
### 回答1: 可以使用以下命令在shell脚本中拉取git代码: ``` #!/bin/bash git clone https://github.com/username/repo.git ``` 其中,`username`是你的GitHub用户名,`repo`是你要拉取的仓库名称。你也可以使用SSH协议来拉取代码,只需要将`https`替换为`git`即可。 如果你需要在脚本中指定代码的存储路径,可以在`git clone`命令后面加上路径: ``` #!/bin/bash git clone https://github.com/username/repo.git /path/to/destination ``` 其中,`/path/to/destination`是你要存储代码的路径。如果该路径不存在,Git会自动创建它。 ### 回答2: Shell脚本是Linux操作系统中常用的一种脚本语言,能够执行一系列的操作任务。其中之一就是拉取Git代码的任务。在执行该任务之前,我们需要明确一些基本概念: 1. Git是一个分布式版本控制系统,用于管理代码库的版本和代码的协同开发。 2. 拉取(pull)代码是指将服务器上的代码库更新到本地。 下面我们将介绍如何用Shell脚本拉取Git代码: 1. 确认Git环境和权限 在执行拉取代码脚本命令之前,需要确认Git环境已经正确安装并配置,并且拥有权限访问Git仓库。 2. 创建Shell脚本 打开终端,输入`vi pull_git.sh`,创建一个名为`pull_git.sh`的Shell脚本文件。 3. 开始脚本编写 在`pull_git.sh`文件中,输入以下Shell脚本命令: ``` #!/bin/sh git config --global credential.helper store git reset --hard git clean -xfd git pull ``` 4. 解释脚本命令 a. `#!/bin/sh` 表示使用Shell作为解释器。 b. `git config --global credential.helper store`表示将Git登录凭证保存在本地。 c. `git reset --hard`表示强制将本地仓库回滚到和远程仓库一致的状态。 d. `git clean -xfd`表示清除本地仓库中没有被Git版本控制的文件和目录。 e. `git pull`表示拉取最新的代码。 5. 保存文件 按“Esc”键退出编辑状态,输入“:wq”,保存并退出。 6. 对脚本添加执行权限 在终端上使用chmod命令为脚本添加执行权限: ``` chmod +x pull_git.sh ``` 7. 执行脚本 在终端输入`./pull_git.sh`,执行脚本,便能将服务器上的代码更新到本地了。 Shell脚本可以帮助我们快速方便地拉取Git代码,使得我们能够更加高效地进行协同开发。反复的输入命令不但浪费时间,也容易出现错误,因此使用Shell脚本无疑是一个不错的选择。 ### 回答3: 首先,我们需要了解什么是shell脚本git代码Shell脚本是一种文本文件,它包含了一系列可执行的命令。Git是一种开源的分布式版本控制系统,它能够管理您的代码。 在编写shell脚本拉取git代码之前,我们需要确保我们已经安装了Git。另外,我们需要确定我们要拉取的Git代码存储在哪个Git库中。通常情况下,我们需要Git库的URL和分支或标签名。 下面是一个简单的Shell脚本示例,可以拉取Git代码: ```shell #!/bin/bash # Git的URL和分支或标签名 git_url="https://github.com/user/repo.git" branch_or_tag="main" # 克隆代码到本地 git clone $git_url # 切换到指定分支或标签 cd repo && git checkout $branch_or_tag ``` 上面的脚本首先定义了Git的URL和分支或标签名,并使用`git clone`命令将代码克隆到本地。接下来,使用`cd`命令进入git仓库目录,并使用`git checkout`命令切换到指定的分支或标签。如果没有指定分支或标签,则默认为`master`分支。 当我们将上面的脚本保存为`pull_git.sh`文件并运行时,它将拉取指定的Git仓库并切换到指定的分支或标签。 此外,我们还可以在脚本中添加其他操作,例如检查是否已经在本地存在该Git仓库,以及在代码拉取完成后进行编译和部署等操作。 总之,使用Shell脚本拉取Git代码是一项非常常见的任务,可以大大高我们的效率。我们只需要在脚本中定义好Git的URL和分支或标签名,然后就能够轻松地拉取代码并进行其他操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值