【问题标题】:Git commit bash scriptGit 提交 bash 脚本
【发布时间】:2012-01-18 22:48:27
【问题描述】:

我正在编写一个 bash 脚本来添加、提交、推送目录中的所有文件。

#!/bin/bash  
git add .  
read -p "Commit description: " desc  
git commit -m $desc  
git push origin master

我收到以下错误:

$ ./togithub  
Commit description:   
test commit script  
error: pathspec 'commit' did not match any file(s) known to git.  
error: pathspec 'script"' did not match any file(s) known to git.  
Everything up-to-date

我不确定这是否是阅读文本时出现的问题(echos 很好)或将其传递给 git commit -m

【问题讨论】:

    标签: git bash github


    【解决方案1】:

    这是一个脚本,它使用格式良好的提交消息提交并将您的更改推送到开发人员 提交信息格式如下:

    <Author Name> #first Line that script asks to enter from user
    - Git Commit message -- # Second Line that script asks to enter from user
    
    -List of added/Modified files
    

    https://github.com/AdityaSingh0/gitFormatedCommitScript/blob/main/CommitGit

    【讨论】:

      【解决方案2】:

      这是我大部分时间用来提交本地分支并与远程分支合并的方法:

      这个小 bash 脚本允许您在本地分支上添加和提交, 结帐到另一个分支,与它合并并推送它,然后再次结帐到 您当地的分支机构,以便您继续工作。

      default="local-dev-whatever-the-name-of-your-local-branch"
      read -p "Enter local branch [$default]: " local
      local=${local:-$default}
      echo "Local branch is $local"
      
      if [ -z "$local" ]
      then
      bin/git-merge.sh
      else
          printf "Enter remote branch: "
          read remote
      
          if [ -z "$remote" ]
          then
              printf "Cannot continue without remote branch!\n\n"
              exit
          fi
      
          git add .
          git add -u
          read -r -p 'Commit description: ' desc
      
          if [ -z "$desc" ]
          then
              printf "\nExit: commit description is empty!"
          fi
      
          git commit -m "$desc"
          git checkout $remote
          git status
          git merge $local
          git push
          git status
          git checkout $local
          git status
          printf "\nEnd local commit on $local; merge and push to branch $remote. Well done!\n"
      fi
      

      【讨论】:

        【解决方案3】:

        以下是我用来管理我的 git 存储库的脚本 - 这将包括推送到您的原始分支、暂存站点(如果设置)和生产站点(如果设置)的选项

        #!/usr/bin/env bash
        
        # die script -- just in case
        die() { echo "$@" 1>&2 ; exit 1; }
        
        # kill message when dead 
         KILL="Invalid Command"
        
        # function to see where to push what branch
        pushing() {
            git branch
            sleep 1
            tput setaf 1;echo  What Branch?;tput sgr0 
            read -r branch
            tput setaf 2;echo  Where to? You can say 'origin', 'staging', or 'production';tput sgr0 
            read -r ans
            if [ "$ans" = "origin" ] || [ "$ans" = "staging" ] || [ "$ans" = "production" ]
            then
                git push "$ans" "$branch" 
            elif [ "$ans" = "no" ]
            then
                echo "Okay" 
            else die "$KILL"
            fi
        }
        
        # function to see how many more times
        more() {
            tput setaf 2;echo More?;tput sgr0 
            read -r more
            if [ "$more" = "yes" ]
            then
                pushing
            elif [ "$more" = "no" ]
            then
                die "Goodbye" 
            else die "$KILL"
            fi
        }
        
        # get the root directory in case you run script from deeper into the repo
        gr="$(git rev-parse --show-toplevel)"
        cd "$gr" || exit
        tput setaf 5;pwd;tput sgr0 
        
        # begin commit input
        git add . -A
        read -r -p "Commit description: " desc  
        git commit -m "$desc"
        
        # find out if we're pushin somewhere
        tput setaf 2;echo  wanna do some pushin?;tput sgr0 
        read -r push 
        if [ "$push" = "yes" ]
        then 
            pushing # you know this function 
            until [ "$more" = "no" ]
            do
                more # you know this function
            done
        elif [ "$push" = "no" ]
        then
            echo "Okay" 
        else die "$KILL"
        fi
        

        我尝试包含尽可能多的 cmets 以帮助您了解所有内容的作用。

        如果您有任何问题,请告诉我。

        另外,我有这样的设置

        echo "alias commit='sh /path/to/script.sh" &gt;&gt; ~/.bash_profile source ~/.bash_profile

        也许这可以帮助希望加快工作流程的人

        【讨论】:

          【解决方案4】:
          #!/bin/bash  
          git pull
          git add .
          git commit -m "$*"
          git push
          

          调用脚本,注释为 cmd 参数,推送的键更少:

          $ ./togithub test commit script 
          

          【讨论】:

            【解决方案5】:

            这是最后两个答案的合并 - 将 add -u 链接在一起很棒,但是嵌入式读取命令给我带来了麻烦。我选择了(最后一行用于我的 heroku 推送,如果这是你的方法,请更改为 'git push origin head'):

            #!/bin/bash
            read -p "Commit description: " desc
            git add . && \
            git add -u && \
            git commit -m "$desc" && \
            git push heroku master
            

            【讨论】:

            • 只做git add -A . 而不是git add . &amp;&amp; git add -u
            【解决方案6】:

            从索引中删除实际已删除的文件很有帮助。 git add -u 负责这个。此外,您可能需要考虑将这些命令链接在一起,如下所示:

            git add . && \
            git add -u && \
            git commit -m "$(read -p 'Commit description: ')" && \
            git push origin HEAD
            

            如果任何命令失败,它将停止评估剩余的命令。

            只是思考的食物(未经测试的食物)。

            谢谢!

            【讨论】:

            • 如果任何命令失败,您也可以使用#!/bin/bash -e 使脚本退出。
            • 有趣的是,这得到了如此多的支持......你会得到空的提交消息......
            【解决方案7】:

            你必须这样做:

            git commit -m "$desc"
            

            在当前脚本中,test 将作为提交消息,commitscript 被视为下一个参数。

            【讨论】:

            • “正确引用”永远不会被夸大。网上有太多低于标准的“howtos”和半正确的建议/示例......
            • 100% - 如果您将整个命令作为参数传递,您可以反斜杠引号,即./myscript.sh git commit -m \"my commit message\"
            猜你喜欢
            • 1970-01-01
            • 2011-07-16
            • 1970-01-01
            • 2017-09-02
            • 2012-03-22
            • 1970-01-01
            • 2016-03-13
            • 2015-09-21
            • 2019-05-23
            相关资源
            最近更新 更多