Git 提交 bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8482843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git commit bash script
提问by mgold
I'm writing a bash script to add, commit, push all files in a directory.
我正在编写一个 bash 脚本来添加、提交、推送目录中的所有文件。
#!/bin/bash
git add .
read -p "Commit description: " desc
git commit -m $desc
git push origin master
I'm getting the following error:
我收到以下错误:
$ ./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
I'm not sure if this is a problem with reading in the text (it echo
s fine) or passing it to git commit -m
.
我不确定这是否是阅读文本(echo
很好)或将其传递给git commit -m
.
回答by manojlds
You have to do:
你必须要做:
git commit -m "$desc"
In the current script, test
is going as commit message and commit
and script
are being treated as next arguments.
在当前的脚本,test
正在按提交信息和commit
与script
被视作下一个参数。
回答by JayCrossler
Here's a merge of the last two answers - chaining together the add -u is awesome, but the embedded read command was causing me troubles. I went with (last line used for my heroku push, change to 'git push origin head' if that's your method):
这是最后两个答案的合并 - 将 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
回答by gahooa
it is helpful to remove from the index the files that have actually been deleted. git add -u
takes care of this. Also, you may want to consider chaining these commands together like this:
从索引中删除实际已删除的文件会很有帮助。 git add -u
照顾这个。此外,您可能需要考虑将这些命令链接在一起,如下所示:
git add . && \
git add -u && \
git commit -m "$(read -p 'Commit description: ')" && \
git push origin HEAD
If any command fails, it will stop evaluating the remaining commands.
如果任何命令失败,它将停止评估剩余的命令。
Just food for thought (untested food).
只是深思熟虑(未经测试的食物)。
Thanks!
谢谢!
回答by godzilla
#!/bin/bash
git pull
git add .
git commit -m "$*"
git push
call script with comment as cmd args, less keys to push:
调用脚本,注释为 cmd args,推送的键更少:
$ ./togithub test commit script
回答by RobBenz
The following is a script that I use to mange my git repos - this will include the option to push to your origin branch, your staging site ( if setup ), and your production site ( if setup )
以下是我用来管理我的 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
I tried to include as many comments as possible to help you understand what everything does.
我尝试包含尽可能多的评论,以帮助您了解所有内容的作用。
let me know if you have any questions.
如果您有任何问题,请告诉我。
also, i have this setup like this
另外,我有这样的设置
echo "alias commit='sh /path/to/script.sh" >> ~/.bash_profile
source ~/.bash_profile
echo "alias commit='sh /path/to/script.sh" >> ~/.bash_profile
source ~/.bash_profile
maybe this can help someone looking to accelerate workflow
也许这可以帮助希望加快工作流程的人
回答by Filipe Fernandes
This is what i use most of the time to commit local branch and merge with remote branches:
这是我大部分时间用来提交本地分支并与远程分支合并的方法:
This little bash script allows you to add and commit on your local branch, checkout to another branch, merge with it and push it, and also checkout again to your local branch, so that you continue to work.
这个小 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