git 如何自动化“提交和推送”过程?(吉特)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16709404/
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
how to automate the "commit-and-push" process? (git)
提问by IcyFlame
I have a git repo. And after each major change, that I make in the codebase, what I do is that I go to the terminal and execute a set of commands.
我有一个 git 仓库。在我在代码库中进行的每次重大更改之后,我要做的是转到终端并执行一组命令。
git add .
git commit -m 'some message'
git push origin master
These are the same each day and the process is quite boring. Can anyone suggest a way to somehow automate this process?
这些每天都一样,过程很无聊。任何人都可以提出一种以某种方式自动化此过程的方法吗?
I am running a Linux Mint 14 OS.
我正在运行 Linux Mint 14 操作系统。
回答by IcyFlame
You can very easily automate this using Bash scripting.
您可以使用 Bash 脚本轻松实现自动化。
git add .
echo 'Enter the commit message:'
read commitMessage
git commit -m "$commitMessage"
echo 'Enter the name of the branch:'
read branch
git push origin $branch
read
store the above code as a .sh
file(say gitpush.sh
)
将上面的代码存储为一个.sh
文件(比如说gitpush.sh
)
And since you have to make this sh file an executable you need to run the following command in the terminal once:
由于您必须将此 sh 文件设为可执行文件,因此您需要在终端中运行以下命令一次:
chmod +x gitpush.sh
And now run this .sh
file.
现在运行这个.sh
文件。
Each time you run it, It will ask you for the commit message and the name of the branch. In case the branch does not exist or the push destination is not defined then git will generate an error. TO read this error, I have added the last read
statement. If there are no errors, then git generates the pushed successfully
type-of message.
每次运行它时,它都会询问您提交消息和分支名称。如果分支不存在或推送目标未定义,则 git 将生成错误。为了阅读这个错误,我添加了最后一条read
语句。如果没有错误,则 git 生成pushed successfully
消息类型。
回答by Fredrik Pihl
That is not a lot of typing to do but the process can be simplified by the usage of aliases.
这不是很多打字要做,但可以通过使用别名来简化该过程。
edit $HOME/.gitconfig
to add some shortcuts, e.g. to use git p
as an alias for git push origin master
, use:
编辑$HOME/.gitconfig
以添加一些快捷方式,例如git p
用作 的别名git push origin master
,使用:
[alias]
p = push origin master
If you're on a descent shell like bash, you can use the history feature. Everything you need to know is in the man page for bash.
如果您使用像 bash 这样的下降 shell,您可以使用历史功能。您需要知道的一切都在 bash 的手册页中。
Read more about aliases here
在此处阅读有关别名的更多信息
On a side-note, git add .
is probably not the best approach since it adds every file in the folder to the staging area. Better to use git add -u
to only add those files already in the index.
附带说明一下,git add .
这可能不是最好的方法,因为它将文件夹中的每个文件都添加到暂存区。最好git add -u
只添加那些已经在索引中的文件。
回答by Abizern
Commiting your work once a day as a big chunk is not an effective use of version control. It's far better to make small, regular, atomic commits that reflect steps in the development of the code base (or whatever else is being tracked in the repository)
每天提交一次大块工作并不是版本控制的有效使用。最好进行小的、定期的、原子性的提交,以反映代码库开发中的步骤(或存储库中正在跟踪的任何其他内容)
Using a shell script is perhaps overkill - most people use aliases to automate git commands, either as shell aliases or git aliases.
使用 shell 脚本可能有点矫枉过正——大多数人使用别名来自动化 git 命令,无论是作为 shell 别名还是 git 别名。
In my case I use shell aliases - a couple of which are:
就我而言,我使用 shell 别名 - 其中有几个是:
alias gp='git push'
alias gc='git commit'
alias gca='git commit -a'
alias gcm='git commit -m'
alias gcam='git commit -am'
so for me to do what you're asking would be:
所以对我来说,你要求的是:
gcam "My commit message";gp
This isn't a lot of typing and is hardly boring, I do this and similar git operations literally dozens of times a day (I know - because I check my shell history).
这不是很多打字,也不是很无聊,我每天执行此操作和类似的 git 操作数十次(我知道 - 因为我检查了我的 shell 历史记录)。
The reason for using small aliases rather than a script is so that I have the flexibility to use different ways of committing, If I used a script I would lose that flexibility.
使用小别名而不是脚本的原因是我可以灵活地使用不同的提交方式,如果我使用脚本,我将失去这种灵活性。
回答by Alper
You could use alias and shell function together, in $HOME/.gitconfig
or .git/config
您可以同时使用 alias 和 shell 函数,在$HOME/.gitconfig
或.git/config
[alias]
newalias = "!f(){ git add . && git commit -m \"\"; git push origin master; };f"
and call it as
并将其称为
$ git newalias "message ..."
回答by E.Zolduoarrati
The best solution for windows: 1) create a text file anywhere on you system and insert the following code into it after changing the fields according to your need
windows的最佳解决方案:1)在系统上的任何位置创建一个文本文件,并根据需要更改字段后将以下代码插入其中
git add .
git commit -m "commitMessage"
git push url master
exit
2) save and exit file.
2) 保存并退出文件。
3) change the file name to push.sh
.
3) 将文件名更改为push.sh
.
4) navigate to file directory on your system using git powershell then use the following command chmod +x push.sh
to make your file runnable.
4) 使用 git powershell 导航到系统上的文件目录,然后使用以下命令chmod +x push.sh
使您的文件可运行。
5) click windows start and search for Task Scheduler
and press create a new task.
5)点击windows开始搜索Task Scheduler
,点击创建新任务。
6) click on the action and browse to your push.sh
file press okay.
6)单击操作并浏览到您的push.sh
文件,然后按确定。
7) navigate to triggers and modify scheduling time.
7) 导航到触发器并修改调度时间。
Now you have a script which is automated as you wish.
现在您有了一个可以根据需要自动执行的脚本。
回答by markfullmer
For my workflow, git "adds" and git "commits" are targeted and atomic, so rolling those into a single bulk command isn't useful. However, each day, I push to a number of different branches across a number of different repositories, and that usually follows the pattern of "git push origin {branchname}". That canbe automated in a shell script:
对于我的工作流程,git "adds" 和 git "commits" 是有针对性和原子性的,因此将它们合并到单个批量命令中是没有用的。但是,每天我都会推送到多个不同存储库中的多个不同分支,这通常遵循“git push origin {branchname}”的模式。这可以在 shell 脚本中自动化:
parse_git_branch() {
git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3
}
git push origin $(parse_git_branch)
You could then add a bash alias (e.g., "git-push") to call this script, which would effectively run "git push origin {currently checked out branch}" on whichever repository you're located in.
然后,您可以添加一个 bash 别名(例如,“git-push”)来调用此脚本,这将在您所在的任何存储库上有效地运行“git push origin {currently checked out branch}”。