首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >预提交抱怨“隐藏的更改与钩子自动修复冲突”,尽管手动格式化文件?

预提交抱怨“隐藏的更改与钩子自动修复冲突”,尽管手动格式化文件?
EN

Stack Overflow用户
提问于 2022-10-12 18:50:56
回答 2查看 809关注 0票数 0

我在C++项目中使用这个钩子(2.20.0版本):

代码语言:javascript
运行
复制
repos:
- repo: https://github.com/pre-commit/mirrors-clang-format
  rev: v14.0.6
  hooks:
  - id: clang-format

我只是从几个不同的..cpp/.h/cmakelist文件中各放了几行。当我尝试提交这些更改时,从预提交中得到以下错误:

代码语言:javascript
运行
复制
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to C:\Users\tyler.shellberg\.cache\pre-commit\patch1665600217-21836.
clang-format.............................................................Failed
- hook id: clang-format
- files were modified by this hook
[WARNING] Stashed changes conflicted with hook auto-fixes... Rolling back fixes...
[INFO] Restored changes from C:\Users\tyler.shellberg\.cache\pre-commit\patch1665600217-21836.

我对此感到困惑。预提交不允许我部分提交文件,或者在提交时有未分阶段的更改吗?

所有的文件,无论是阶段性的还是非阶段性的,都是用clang-格式格式化的.我已经手动检查过了。

为清晰而编辑:

如果我对每个文件(已执行和未执行)运行pre-commit run --files [filename],则所有报告都会返回“传递”或“跳过”(对于非cpp文件)。如果所有文件都通过了,为什么会出现问题?

EN

回答 2

Stack Overflow用户

发布于 2022-10-12 19:00:37

输出的第一部分是:

代码语言:javascript
运行
复制
[WARNING] Unstaged files detected.

这意味着您进行了部分阶段的提交--这很好,pre-commit很好地支持这一点。

在那之后,一个钩子做了改变:

代码语言:javascript
运行
复制
- files were modified by this hook

这些更改肯定与未分阶段的部分相冲突,因为接下来会有如下消息:

代码语言:javascript
运行
复制
[WARNING] Stashed changes conflicted with hook auto-fixes... Rolling back fixes...

这意味着您需要确保未分阶段的部分被正确地格式化(pre-commit run --files whatever.cpp),也需要对它们进行分级,以便它们也被格式化。

免责声明:我创建了预提交。

票数 1
EN

Stack Overflow用户

发布于 2022-10-12 20:25:23

如果确实存在一些没有进行的局部处理,您可以尝试以下解决方法:

代码语言:javascript
运行
复制
# stash your modifications, but keep your index as is:
git stash --keep

git commit

# get back the content of your repo as it was before you ran `git stash`,
# without trying to fix any conflicts
git restore -s=stash .

#  *warning* : using `git restore` to restore the worktree is one of those few
# destructive commands, that is allowed to discard changes and delete files
# from your disk without them being stored in git (a lot like
# 'git reset --hard').
# In the current situation, there shouldn't be any uncommitted changes, though.

# you shouldn't need that stash anymore
git stash drop
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74046718

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档