git 如何使用单个命令暂存和提交所有文件,包括新添加的文件?

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

How can I stage and commit all files, including newly added files, using a single command?

git

提问by Anantha Kumaran

How can I stage and commit all files, including newly added files, using a single command?

如何使用单个命令暂存和提交所有文件,包括新添加的文件?

回答by Ian Clelland

Does

git add -A && git commit -m "Your Message"

count as a "single command"?

算作“单一命令”吗?

Edit based on @thefinnomenon's answer below:

根据以下@thefinnomenon 的回答进行编辑

To have it as a git alias, use:

要将其作为git alias,请使用:

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

and commit all files, including new files, with a message with:

并提交所有文件,包括新文件,并带有以下消息:

git coa "A bunch of horrible changes"


Explanation(from git adddocumentation):

解释(来自git add文档):

-A, --all, --no-ignore-removal

Update the index not only where the working tree has a file matching but also where the index already has an entry. This adds, modifies, and removes index entries to match the working tree.

If no <pathspec>is given when -A option is used, all files in the entire working tree are updated (old versions of Git used to limit the update to the current directory and its subdirectories).

-A, --all, --no-ignore-removal

不仅在工作树有文件匹配的地方更新索引,而且在索引已经有条目的地方更新索引。这会添加、修改和删除索引条目以匹配工作树。

如果<pathspec>在使用 -A 选项时给出no ,则更新整个工作树中的所有文件(旧版本的 Git 用于将更新限制为当前目录及其子目录)。

回答by Jai Keerthi

This command will add and commit all the modified files, but not newly created files.

此命令将添加并提交所有修改过的文件,但不会添加并提交新创建的文件

git commit -am  "<commit message>"

From man git-commit:

来自 man git-commit:

   -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.

回答by phlppn

I use this function:

我使用这个功能:

gcaa() { git add --all && git commit -m "$*" }

In my zsh config file, so i can just do:

在我的 zsh 配置文件中,我可以这样做:

> gcaa This is the commit message

To automatically stage and commit all files.

自动暂存和提交所有文件。

回答by Yarin

One-liner to stage ALL files (modified, deleted, and new) and commit with comment:

单线暂存所有文件(已修改已删除新文件)并提交注释:

git add --all && git commit -m "comment"

http://git-scm.com/docs/git-add
http://git-scm.com/docs/git-commit

http://git-scm.com/docs/git-add
http://git-scm.com/docs/git-commit

回答by thefinnomenon

Not sure why these answers all dance around what I believe to be the right solution but for what it's worth here is what I use:

不知道为什么这些答案都围绕着我认为是正确的解决方案跳舞,但对于这里的价值,我使用的是:

Create an alias:

创建别名:

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

Add all files & commit with message:

添加所有文件并提交消息:

git coa "A bunch of horrible changes"

NOTE: coais short for commit all and can be replaced with anything your heart desires

注意:coa是 commit all 的缩写,可以用你想要的任何东西替换

回答by King Linux

Committing in git can be a multiple step process or one step depending on the situation.

根据情况,在 git 中提交可以是一个多步骤过程或一个步骤。

  1. This situation is where you have multiple file updated and wants to commit:

    You have to add all the modified files before you commit anything.

    git add -A
    

    or

    git add --all
    
  2. After that you can use commit all the added files

    git commit
    

    with this you have to add the message for this commit.

  1. 这种情况是您更新了多个文件并希望提交:

    在提交任何内容之前,您必须添加所有修改过的文件。

    git add -A
    

    或者

    git add --all
    
  2. 之后,您可以使用提交所有添加的文件

    git commit
    

    有了这个,您必须为此提交添加消息。

回答by Ajedi32

If you just want a "quick and dirty" way to stash changes on the current branch, you can use the following alias:

如果您只是想要一种“快速而肮脏”的方式来存储当前分支上的更改,您可以使用以下别名:

git config --global alias.temp '!git add -A && git commit -m "Temp"'  

After running that command, you can just type git tempto have git automatically commit all your changes to the current branch as a commit named "Temp". Then, you can use git reset HEAD~later to "uncommit" the changes so you can continue working on them, or git commit --amendto add more changes to the commit and/or give it a proper name.

运行该命令后,您只需键入git temp让 git 自动将所有更改提交到当前分支,作为名为“Temp”的提交。然后,您可以git reset HEAD~稍后使用“取消提交”更改,以便您可以继续处理它们,或者git commit --amend向提交添加更多更改和/或为其指定适当的名称。

回答by SystematicFrank

I have in my config two aliases:

我的配置中有两个别名:

alias.foo=commit -a -m 'none'
alias.coa=commit -a -m

if I am too lazy I just commit all changes with

如果我太懒了,我只会提交所有更改

git foo

and just to do a quick commit

只是为了快速提交

git coa "my changes are..."

coa stands for "commit all"

coa 代表“全部提交”

回答by VeRo

Great answers, but if you look for a singe line do all, you can concatenate, alias and enjoy the convenience:

很好的答案,但如果你寻找一条单行做所有事情,你可以连接、别名并享受便利:

git add * && git commit -am "<commit message>"

git add * && git commit -am "<commit message>"

It is a single line but two commands, and as mentioned you can alias these commands:

它是一行但有两个命令,如前所述,您可以为这些命令设置别名:

alias git-aac="git add * && git commit -am "(the space at the end is important) because you are going to parameterize the new short hand command.

alias git-aac="git add * && git commit -am "(末尾的空格很重要)因为您要参数化新的速记命令。

From this moment on, you will be using this alias:

从现在开始,您将使用这个别名:

git-acc "<commit message>"

git-acc "<commit message>"

You basically say:

你基本上说:

git, add for me all untracked files and commit them with this given commit message.

git,为我添加所有未跟踪的文件并使用此给定的提交消息提交它们。

Hope you use Linux, hope this helps.

希望你使用 Linux,希望这会有所帮助。

回答by Kiran Maniya

Run the given command

运行给定的命令

git add . && git commit -m "Changes Committed"

However, even if it seems a single command, It's two separate command runs one by one. Here we just used &&to combine them. It's not much different than running git add .and git commit -m "Changes Committed"separately. You can run multiple commands together but sequence matters here. How if you want to push the changes to remote server along with staging and commit you can do it as given,

然而,即使它看起来是一个单一的命令,它也是两个独立的命令一个接一个地运行。这里我们只是用来&&组合它们。这git add .git commit -m "Changes Committed"单独运行和单独运行没有太大区别 。您可以同时运行多个命令,但这里的顺序很重要。如果您想将更改连同暂存和提交一起推送到远程服务器,您可以按照给定的方式进行,

git add . && git commit -m "Changes Committed" && git push origin master

Instead, if you change the sequence and put the pushto first, It will be executed first and does not give desired push after staging and commit just because it already ran first.

相反,如果您更改序列并将pushto 放在第一位,它将首先执行并且在暂存和提交后不会提供所需的推送,因为它已经首先运行。

&&runs the second command on the line when the first command comes back successfully, or with an error level of 0. The opposite of &&is ||, which runs the second command when the first command is unsuccessful, or with an error level of 1.

&&当第一命令回来成功,或具有0的相对误差级上运行就行了第二命令&&就是||,其中,当所述第一命令不成功,或为1的误差水平上运行第二个命令。

Alternatively, you can create aliseas git config --global alias.addcommit '!git add -a && git commit -m'and use it as git addcommit -m "Added and commited new files"

或者,您可以创建aliseasgit config --global alias.addcommit '!git add -a && git commit -m'并将其用作git addcommit -m "Added and commited new files"