获取特定git提交命令要提交的文件列表

3
输入:git仓库目录和提交命令,例如git commit -m "commit staged"git commit -a -m "commit all file"或其他任何git commit ... 输出:将被此命令添加到提交中的文件列表。
可能的解决方案:
  • 通过git diff --cached --name-status获取暂存的文件。

  • 检查git commit命令是否包含--all标志。

  • 如果有--all:通过git diff --name-only获取未暂存的文件,并将此列表与暂存更改列表合并。

  • 如果没有--all:仅获取暂存更改列表。

这个算法是否覆盖了所有的git提交命令和仓库状态?

不 - 你也可以明确告诉提交命令只提交特定的文件:git commit -m "Message" file1.txt file2.txt - 1615903
好的,谢谢。还有其他选项吗? - WHITECOLOR
你用这个做什么? - Schwern
在真正执行提交之前,要提交的文件尚未确定。将此任务放入“post-commit”钩子中,您可以获得即时反馈,了解哪些文件已提交。 - ElpieKay
我需要这个用于预提交检查。 - WHITECOLOR
@WHITECOLOR 哦,这种情况下可以编写一个pre-commit hook。在那时,我相信即将提交的内容将被暂存。 - Schwern
2个回答

4
您可以使用git status --porcelain命令一次性获取工作目录和暂存区的完整状态。
$ git status --porcelain
A  that
MM this
?? blah

第一列是暂存区的状态,第二列是工作目录的状态。上面的内容表示that已被添加到暂存区,this有已暂存和未暂存的修改,blah未被跟踪。

以下是同样的内容,但是更详细。

$ git status
On branch feature
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   that
    modified:   this

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   this

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    blah

但我猜想你真正想要的是在提交之前查看即将更改的内容。在这种情况下,我建议你不要这样做。相反...

  1. 不使用-m
  2. 使用-v
  3. git commit --amend来修复任何错误。

git commit -m是一个坏习惯。这意味着你的提交信息将只有一行,缺乏细节。一个好的提交信息应该是:

简要总结。

详细信息,详细信息。

git commit -m会阻碍这一点,而那些细节在以后将非常重要,你永远无法再次获得它们。值得额外几秒钟的时间。

更重要的是,常规的git commit会拉起编辑器,并显示即将提交的内容。

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch feature
# Changes to be committed:
#       new file:   that
#       modified:   this
#
# Changes not staged for commit:
#       modified:   this
#
# Untracked files:
#       blah
#

更好的是,如果您使用git commit -v,您将获得完整的差异以进行审查。我经常使用此功能,我已将其别名设置为git ci(检入)。

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch feature
# Changes to be committed:
#       new file:   that
#       modified:   this
#
# Changes not staged for commit:
#       modified:   this
#
# Untracked files:
#       blah
#
# ------------------------ >8 ------------------------
# Do not touch the line above.
# Everything below will be removed.
diff --git a/that b/that
new file mode 100644
index 0000000..e69de29
diff --git a/this b/this
index 73e4f83..f129e32 100644
--- a/this
+++ b/this
@@ -4,3 +4,5 @@ fix
 1
 2
 3
+4
+

最后,与其他版本控制系统不同,Git中的提交不会立即共享。除非你使用git push。这意味着如果你在提交时犯了一个错误,你可以快速修复它。只需进行修复并使用git commit --amend,上一个提交将被更新。

所以,与其添加许多层来防止自己犯错,不如直接修复错误。这就是版本控制的美妙之处!


值得注意的是,“提交本地化”并不是Git所特有的。Mercurial以完全相同的方式工作,现代Mercurial还增加了一些保护措施:每个提交都有一个与之关联的“阶段”(秘密、草稿或公共),当推送时,提交会自动升级到“公共”阶段。像hg amendhg histedit这样的命令知道您只能编辑未发布的提交。(尽管hg在这里显然具有更好的界面,但我仍然使用并喜欢Git :-)) - torek
感谢您的建议和意见。 - WHITECOLOR

0

如果您像上面建议的那样使用git commit -v来查看修改的文件列表,请注意在Git 2.14.x/2.15(2017年第4季度)中,该列表前面的行将发生变化。

提交日志编辑器中切割线的解释已经略有调整。

查看 提交 8c4b1a3(2017年9月13日)由Kaartic Sivaraam (sivaraam)提交。
协助者:Jeff King (peff)
(由Junio C Hamano -- gitster --合并于提交 77f4539,2017年9月25日)

commit-template: 更直观地改变信息

使用“不要触摸”这个短语来传达切线不应该被修改或删除的信息并不好,因为对于不知道“触摸”一词意味着“篡改”的人来说,可能会产生误解。
此外,在翻译成某些语言时,它可能没有预期的含义,从而使翻译变得有些困难。

因此,在句子中使用更直观的术语。

Do not touch the line above.
Everything below will be removed.

你将拥有:

Do not modify or remove the line above.
Everything below it will be ignored.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接