gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 03:09:10 25 4
gpt4 key购买 nike

输入:git repo 目录和提交命令:如git commit -m "commit staged"git commit -a -m "comit all file" 或任何其他 git commit ...

输出:将通过此命令添加到提交中的文件列表。

可能的解决方案:

  • 通过以下方式获取暂存文件:git diff --cached --name-status

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

  • if --all:通过 git diff --name-only 获取未暂存文件并获取此列表 + 暂存更改列表

  • 如果没有--all:只获取暂存变更列表

这个算法是否覆盖了所有git commit命令和repo状态的情况?

最佳答案

您可以使用 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 是一个不好养成的习惯。这意味着您的提交消息将是一行并且缺乏细节。一个好的提交信息是这样的:

Brief summary.

Details, details, details.

git commit -m 不鼓励这样做,这些细节在以后很重要,而且您永远无法取回它们。多花几秒钟是值得的。

更重要的是, pull 出编辑器的常规 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( checkin )。

# 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 - 获取要由特定 git commit 命令提交的文件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43602193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com