Git 阶段并使用一个命令提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14816570/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:28:43  来源:igfitidea点击:

Git stage and commit with one command

git

提问by Dmitry

Is there a way in Git to stage and commit files in one command? For example in my local repository I created files index.html, styles.css located in css folder and script.js located in js folder. Now I want to run one command to stage and commit all this files. I tried code below but it didn't work

Git 有没有办法在一个命令中暂存和提交文件?例如,在我的本地存储库中,我创建了位于 css 文件夹中的文件 index.html、styles.css 和位于 js 文件夹中的 script.js。现在我想运行一个命令来暂存并提交所有这些文件。我试过下面的代码,但没有用

git commit -a -m "my commit message"

回答by David Hunsicker

What you want to do is:

你想要做的是:

git commit -am "Message for my commit"

This will automatically add all tracked files and you can type your message in one command.

这将自动添加所有跟踪的文件,您可以在一个命令中输入您的消息。

-a --all
Tell the command to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected.

-m <msg> --message=<msg>
Use the given as the commit message. If multiple -m options are given, their values are concatenated as separate paragraphs.

https://git-scm.com/docs/git-commit

-a --all
告诉命令自动暂存已修改和删除的文件,但您没有告诉 Git 的新文件不受影响。

-m <msg> --message=<msg>
使用给定的作为提交消息。如果给出了多个 -m 选项,它们的值将连接为单独的段落。

https://git-scm.com/docs/git-commit

If you want to stage and commit untracked files, you can:

如果要暂存和提交未跟踪的文件,您可以:

git add -A & git commit -am 'message'

回答by David Culp

git commit -a ...will automatically add and commit files that have already been commited previously and are modified or deleted now. As you found out it does not affect new files.

git commit -a ...将自动添加和提交之前已经提交并且现在被修改或删除的文件。正如您发现的那样,它不会影响新文件。

You could use an alias to combine the git add ...and git commit ...into one command line. But if you do, take the time to script it to not need to use git add .or git add -Aas that will inevitably lead to commiting files you really don't want to.

您可以使用别名将git add ...和组合git commit ...到一个命令行中。但是,如果您这样做了,请花时间将其编写为不需要使用的脚本,git add .否则git add -A将不可避免地导致提交您真正不想要的文件。

回答by bpoiss

You can do this by using an alias.

您可以通过使用别名来做到这一点。

Define an alias like this:

像这样定义别名:

git config --global alias.your-alias '!git add -A && git commit'

git config --global alias.your-alias '!git add -A && git commit'

Then you can use it like a normal git command: git your-alias -m 'commit message'

然后你可以像普通的 git 命令一样使用它: git your-alias -m 'commit message'