如何在预提交挂钩中重新暂存已暂存的文件

如何在预提交挂钩中重新暂存已暂存的文件

本文介绍了Git:如何在预提交挂钩中重新暂存已暂存的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个git pre-commit钩子.
该脚本可以重新格式化某些代码,因此可以修改暂存的文件.

I'm writting a git pre-commit hook.
The script could reformat some code, so it could modify the staged files.

如何重新暂存已暂存的所有文件?

How can I re-stage all files that are already staged ?

推荐答案

在没有pre-commit hook上下文的情况下,您可以:

Without the pre-commit hook context, you can get a list of the staged files with the following command:

git diff --name-only --cached

因此,如果要重新索引已暂存的文件,可以使用:

So if you want to re-index the staged files, you can use:

git diff --name-only --cached | xargs -l git add


pre-commit hook上下文中,您应该遵循大卫·温特伯顿(David Winterbottom)的建议,并在未进行任何其他更改之前隐瞒了未进行的更改.


In the pre-commit hook context, you should follow the advices of David Winterbottom and stash unstaged changes before anything else.

此技术使您不必担心索引或更改未上演的更改.因此,您不必暂存所有已暂存的文件,而不必暂存所有已更新的文件:

This technique allows you not to be worry about indexing, or alterate, a change that was not staged.So you don't have to stage all the staged files, but all the updated files:

# Stash unstaged changes
git stash -q --keep-index

# Edit your project files here
...

# Stage updated files
git add -u

# Re-apply original unstaged changes
git stash pop -q

这篇关于Git:如何在预提交挂钩中重新暂存已暂存的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 05:45