git 提交并自动添加所有未跟踪的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16128018/
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
Commit and automatically add all untracked files
提问by Louis Rhys
I often forgot that I have some new files and directly do,
我经常忘记我有一些新文件,直接做,
git commit -a -m "My commit message"
This only commits changed files, and I have to do add the remaining files in a new commit. This means that there are two separate commits although logically they are part of the same task.
这只会提交更改的文件,我必须在新提交中添加剩余的文件。这意味着有两个单独的提交,尽管在逻辑上它们是同一任务的一部分。
The easiest way I know to include untracked files in the commit via two consecutive commands:
我所知道的通过两个连续命令在提交中包含未跟踪文件的最简单方法:
git add -A
git commit -a -m "My commit message"
Is it possible to have the same effect like the above in one command?
是否有可能在一个命令中具有与上述相同的效果?
回答by d_ethier
Create a file in your execution path called (no extension): git-add-commit-untracked
在您的执行路径中创建一个名为(无扩展名)的文件: git-add-commit-untracked
Put this in it:
把这个放进去:
#!/bin/bash
message=[alias]
ca = !sh -c 'git add -A && git commit -m \"\"' -
git add -A
git commit -am "$message"
Then:
git-add-commit-untracked "Commit message"
然后:
git-add-commit-untracked "Commit message"
You can use a shorter name for the file though. I left it lengthy for illustrative purposes.
不过,您可以为文件使用较短的名称。为了说明的目的,我把它留得很长。
回答by kowak
You can define an alias to add all before commit. Just put this lines into your ~/.gitconfig file :
您可以定义别名以在提交前添加所有内容。只需将此行放入您的 ~/.gitconfig 文件中:
$ git ca 'Your commit message'
Then use your alias like this :
然后像这样使用你的别名:
[alias]
untracked = ls-files --other --exclude-standard
add-untracked = !git add $(git untracked)
回答by Richard
Here are a few options:
这里有几个选项:
- Use
git commit --amend
so that you end up with only one commit after adding the untracked files (but not if you've already pushed the previous commit); - Use
git commit --interactive
; Create an alias or script that automatically adds new files (examples in other answers). Here are two aliases that I use for exactly this purpose:
[alias] untracked = ls-files --other --exclude-standard add-untracked = !git add $(git untracked)
Squash your two commits using
git rebase -i
,git reset <commit prior to first commit>
andgit commit
, orgit merge --squash
onto another branch.
- 使用,
git commit --amend
以便在添加未跟踪的文件后最终只有一个提交(但如果您已经推送了上一个提交,则不会); - 使用
git commit --interactive
; 创建自动添加新文件的别名或脚本(其他答案中的示例)。以下是我用于此目的的两个别名:
##代码##使用
git rebase -i
,git reset <commit prior to first commit>
和git commit
, 或git merge --squash
将您的两次提交压缩到另一个分支上。
However, you cannot overridea builtin command such as commit
with an alias, so it is still up to you to remember to add the files that you want to the index before committing.
但是,您不能使用别名覆盖内置命令commit
,因此您仍然需要记住在提交之前将您想要的文件添加到索引中。
Unfortunately as well, you cannot specify paths along with git commit -a
; I just tried git commit -a $(git untracked)
and it told me fatal: Paths with -a does not make sense.
So to answer your basic question, I think a git script would be the only non-interactive way.
不幸的是,您不能与git commit -a
;一起指定路径。我刚刚尝试过git commit -a $(git untracked)
,它告诉我fatal: Paths with -a does not make sense.
所以要回答您的基本问题,我认为 git 脚本将是唯一的非交互式方式。
回答by duykhoa
Yeah, I had the same problem like you in the past. I preferred to use git command, but I got a big troubles because the guard in rails changed many files without alert :)
是的,我以前也遇到过和你一样的问题。我更喜欢使用 git 命令,但我遇到了很大的麻烦,因为 rails 中的守卫在没有警报的情况下更改了许多文件:)
So now, whenever i commit new code, I try to use the UI to do this (git gui) and keep in mind never use git commit -a
because you don't sure which files were modified!
After rebase new code, please you gitk
to view the git tree, so you can know the lasted commitment, committers, commit files and so on.
所以现在,每当我提交新代码时,我都会尝试使用 UI 来执行此操作 (git gui) 并记住永远不要使用,git commit -a
因为您不确定哪些文件被修改了!rebase 新代码后,请您gitk
查看git 树,以便您了解上次提交、提交者、提交文件等。
回答by slezica
It's not possible using the default git
interface. However, git
supports aliases.
使用默认git
界面是不可能的。但是,git
支持aliases。
Using an alias, it's possible to invoke a different git command, or even run arbitrary shell scripts. Create a script containing the two commands, and alias it.
使用别名,可以调用不同的 git 命令,甚至可以运行任意 shell 脚本。创建一个包含这两个命令的脚本,并为其设置别名。
See the wikifor more information on aliases, and this questionfor this specific scenario.
有关别名的更多信息,请参阅wiki,以及针对此特定场景的此问题。
Edit: note that this is not such a good idea. The chances are you'll end up adding unnecessary files by mistake, or having a useless alias due to non-versioned files in the tree.
编辑:请注意,这不是一个好主意。您最终可能会错误地添加不必要的文件,或者由于树中的非版本化文件而具有无用的别名。