git add、commit 和 push 命令合二为一?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19595067/
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 add, commit and push commands in one?
提问by Gediminas
Is there any way to use these three commands in one?
有没有办法将这三个命令合二为一?
git add .
git commit -a -m "commit" (do not need commit message either)
git push
Sometimes I'm changing only one letter, CSS padding or something. Still, I have to write all three commands to push the changes. There are many projects where I'm only one pusher, so this command would be awesome!
有时我只更改一个字母、CSS 填充或其他内容。不过,我必须编写所有三个命令来推送更改。有很多项目我只是一个推动者,所以这个命令会很棒!
回答by btse
Building off of @Gavin's answer:
基于@Gavin 的回答:
Making lazygit a function instead of an alias allows you to pass it an argument. I have added the following to my .bashrc (or .bash_profile if Mac):
使 lazygit 成为一个函数而不是别名允许您向它传递一个参数。我已将以下内容添加到我的 .bashrc(或 .bash_profile,如果是 Mac):
function lazygit() {
git add .
git commit -a -m ""
git push
}
This allows you to provide a commit message, such as
这允许您提供提交消息,例如
lazygit "My commit msg"
You could of course beef this up even more by accepting even more arguments, such as which remote place to push to, or which branch.
您当然可以通过接受更多参数来进一步加强这一点,例如推送到哪个远程位置或哪个分支。
回答by madcapacity
I ended up adding an alias to my .gitconfig
file:
我最终为我的.gitconfig
文件添加了一个别名:
[alias]
cmp = "!f() { git add -A && git commit -m \"$@\" && git push; }; f"
Usage: git cmp "Long commit message goes here"
用法: git cmp "Long commit message goes here"
Adds all files, then uses the comment for the commit message and pushes it up to origin.
添加所有文件,然后使用提交消息的注释并将其推送到原点。
I think it's a better solution because you have control over what the commit message is.
我认为这是一个更好的解决方案,因为您可以控制提交消息的内容。
The alias can be also defined from command line, this adds it to your .gitconfig
:
别名也可以从命令行定义,这会将它添加到您的.gitconfig
:
git config --global alias.cmp '!f() { git add -A && git commit -m "$@" && git push; }; f'
回答by Tilman Vogel
While I agree with Wayne Werner on his doubts, this is technically an option:
虽然我同意 Wayne Werner 的怀疑,但从技术上讲,这是一个选择:
git config alias.acp '! git commit -a -m "commit" && git push'
Which defines an alias that runs commit
and push
. Use it as git acp
. Please be aware that such "shell" aliases are always run from the root of your git repository.
它定义了一个运行commit
和的别名push
。将其用作git acp
. 请注意,此类“shell”别名始终从 git 存储库的根目录运行。
Another option might be to write a post-commit hook that does the push.
另一种选择可能是编写一个执行推送的提交后挂钩。
Oh, by the way, you indeed canpass arguments to shell aliases. If you want to pass a custom commit message, instead use:
哦,顺便说一下,您确实可以将参数传递给 shell 别名。如果要传递自定义提交消息,请改用:
git config alias.acp '! acp() { git commit -a -m "" && git push ; } ; acp'
(Of course, now, you will need to give a commit message: git acp "My message goes here!"
)
(当然,现在,你需要给一个提交信息:git acp "My message goes here!"
)
回答by Wayne Werner
I think you might misunderstand the workflow that git was designed for. (To clarify/correct what I meant in the comment, you don't need the git add .
, since commit -a
usually serves the same purpose - adding any changes that have not yet been staged, if the files have already been added)
我想你可能误解了 git 设计的工作流程。(为了澄清/更正我在评论中的意思,您不需要git add .
,因为commit -a
通常用于相同的目的 - 如果已添加文件,则添加尚未暂存的任何更改)
Typically you'll do something like this:
通常你会做这样的事情:
# make some changes
$ git commit -a -m "Changed something"
# make some more changes
$ git commit -a -m "Changed something else"
wash, rinse, repeat, until you've finished feature X, or you're at a stopping point, or you just want other people to see what you've done. Then you do
清洗、冲洗、重复,直到你完成了功能 X,或者你在一个停止点,或者你只是想让其他人看到你做了什么。然后你做
$ git push
Git is not SVN - but it appears that you're trying to use it as such. You might find some of the resources at the end of the article hereto be of some use.
Git 不是 SVN - 但看起来你正试图这样使用它。你可以在文章的末尾找到一些资源在这里是一些使用的。
回答by Rajender Joshi
I use this in my .bash_profile
我在我的 .bash_profile 中使用它
gitpush() {
git add .
git commit -m "$*"
git push
}
alias gp=gitpush
It executes like
它执行如下
gp A really long commit message
Don't forget to run source ~/.bash_profile
after saving the alias.
不要忘记source ~/.bash_profile
在保存别名后运行。
回答by vuhung3990
You can use bash script , set alias to launch any command or group of commands
您可以使用 bash 脚本,设置别名来启动任何命令或命令组
git commit -am "your message" && git push
回答by Gavin
Set as an alias in bash:
在 bash 中设置为别名:
$ alias lazygit="git add .; git commit -a -m '...'; git push;";
Call it:
称它为:
$ lazygit
(To make this alias permanent, you'd have to include it in your .bashrc or .bash_profile)
(要使这个别名永久,您必须将它包含在您的 .bashrc 或 .bash_profile 中)
回答by michal.jakubeczy
Simpliest solution would be to:
最简单的解决方案是:
git commit -a -m "commit" && git push
git add
is already contained in -a parameter of commit, but if you want you can connect them all:
git add
已经包含在提交的 -a 参数中,但是如果您愿意,可以将它们全部连接起来:
git add . && git commit -a -m "commit" && git push
回答by mcvkr
In Linux/Mac, this much practical option should also work
在 Linux/Mac 中,这个非常实用的选项也应该有效
git commit -am "IssueNumberIAmWorkingOn --hit Enter key
> A detail here --Enter
> Another detail here --Enter
> Third line here" && git push --last Enter and it will be there
If you are working on a newbranch created locally, change the git push
piece with git push -u origin branch_name
如果您正在处理本地创建的新分支,请更改git push
部分git push -u origin branch_name
If you want to edit your commit message in system editor then
如果要在系统编辑器中编辑提交消息,则
git commit -a && git push
will open the editor and once you save the message it will also push it.
将打开编辑器,一旦您保存消息,它也会推送它。
回答by Fries
You can try gitu.
你可以试试gitu。
For the first time (node js has to be installed):
第一次(需要安装node js):
npm install -g git-upload
After that:
在那之后:
gitu COMMIT_MSG
To issue those three commands at once.
一次发出这三个命令。
The good thing is that you don't have to worry when you reinstall your system or when you want to do this on different computers and No file modification is needed.This also work on different platforms(not just Linux and Mac, but also Windows under command prompt like cmd
and powershell
) just that you have to install npm
and nodejs
(git
of course).
好消息是,当您重新安装系统或想要在不同的计算机上执行此操作时,您不必担心并且无需修改文件。这也适用于不同的平台(不仅是 Linux 和 Mac,还适用于命令提示符下的 Windowscmd
和powershell
),只是您必须安装npm
和nodejs
(git
当然)。