开发者

git: Can I stash an untracked file without adding it to the index?

A related question How do you stash an untracked file? was answered with "track the file." This doesn't work for my particular needs, however.

I'm trying to stash everything that isn't in the index with git stash save --keep-index so that I can validate the index in my pre-commit hook. The idea is from the "Testing partial commits" example of the git-stash man page. I want to make sure that what I'm actually committing passes the tests a开发者_如何学运维nd not just whats in the working directory. Here's what I have so far:

echo "Running tests on the staging area."
git stash save --keep-index
# configure, build, run tests, clean
git stash pop; true

This seems to work until I have untracked files in my working directory which don't get stashed. Some searching resulted in a feature request from two years ago: Option to save untracked and/or ignored files in stash, but nothing else.

Should I be using stash at all? Perhaps theres a better way involving temporary branches or something.


As I answered on the related question, the answer is now YES:

As of git 1.7.7, git stash accepts the --include-untracked option to include untracked files in your stash.

git stash --include-untracked

Or you can use the short -u option.

git stash -u


No, git stash will only "record the current state of the working directory and the index". So stashing won't pickup your untracked files.

Switching to a temporary branch and tracking the untracked files there, as you suggest, seems like a reasonable approach.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜