git 你如何隐藏一个未跟踪的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/835501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:26:23  来源:igfitidea点击:

How do you stash an untracked file?

gitgit-stash

提问by skiphoppy

I have changes to a file, plus a new file, and would like to use git stash to put them away while I switch to another task. But git stash by itself stashes only the changes to the existing file; the new file remains in my working tree, cluttering up my future work. How do I stash this untracked file?

我对文件进行了更改,并添加了一个新文件,并且想在切换到另一个任务时使用 git stash 将它们收起来。但是 git stash 本身只存储对现有文件的更改;新文件保留在我的工作树中,打乱了我以后的工作。我如何隐藏这个未跟踪的文件?

回答by sykora

To stash your working directory including untracked files (especially those that are in the .gitignore) then you probably want to use this cmd:

要隐藏您的工作目录,包括未跟踪的文件(尤其是 .gitignore 中的文件),那么您可能想要使用此 cmd:

git stash --include-untracked

git stash --include-untracked

More details:

更多细节:

Update 17 May 2018:

2018 年 5 月 17 日更新:

New versions of git now have git stash --allwhich stashes all files, including untracked and ignored files.
git stash --include-untrackedno longer touches ignored files (tested on git 2.16.2).

新版本的 git 现在可以存储git stash --all所有文件,包括未跟踪和忽略的文件。
git stash --include-untracked不再涉及被忽略的文件(在 git 2.16.2 上测试)。

Original answer below:

原答案如下:

Warning, doing this will permanently delete your files if you have any directory/* entries in your gitignore file.

警告,如果您的 gitignore 文件中有任何 directory/* 条目,则这样做将永久删除您的文件。

As of version 1.7.7 you can use git stash --include-untrackedor git stash save -uto stash untracked files without staging them.

从 1.7.7 版开始,您可以使用git stash --include-untrackedgit stash save -u来隐藏未跟踪的文件而无需暂存它们。

Add (git add) the file and start tracking it. Then stash. Since the entire contents of the file are new, they will be stashed, and you can manipulate it as necessary.

添加 ( git add) 文件并开始跟踪它。然后藏起来。由于文件的全部内容都是新的,它们将被隐藏起来,您可以根据需要对其进行操作。

回答by John Kary

As of git 1.7.7, git stashaccepts the --include-untrackedoption (or short-hand -u). To include untracked files in your stash, use either of the following commands:

从 git 1.7.7 开始,git stash接受--include-untracked选项(或简写-u)。要将未跟踪的文件包含在您的存储中,请使用以下任一命令:

git stash --include-untracked
git stash -u

Warning, doing this will permanently delete your files if you have any directory/* entries in your gitignore file.

警告,如果您的 gitignore 文件中有任何 directory/* 条目,则这样做将永久删除您的文件。

回答by skiphoppy

Add the file to the index:

将文件添加到索引:

git add path/to/untracked-file
git stash

The entire contents of the index, plus any unstaged changes to existing files, will all make it into the stash.

索引的全部内容,加上对现有文件的任何未暂存更改,都将进入存储。

回答by DolphinJava

In git bash, stashing of untracked files is achieved by using the command

在 git bash 中,通过使用命令来实现未跟踪文件的隐藏

git stash --include-untracked

or

或者

git stash -u

http://git-scm.com/docs/git-stash

http://git-scm.com/docs/git-stash

git stash removes any untracked or uncommited files from your workspace. And you can revert git stash by using following commands

git stash 从您的工作区中删除任何未跟踪或未提交的文件。您可以使用以下命令恢复 git stash

git stash pop

This will place the file back in your local workspace.

这会将文件放回本地工作区。

My experience

我的经验

I had to perform a modification to my gitIgnore file to avoid movement of .classpath and .project files into remote repo. I am not allowed to move this modified .gitIgnore in remote repo as of now.

我不得不对我的 gitIgnore 文件进行修改,以避免将 .classpath 和 .project 文件移动到远程仓库中。到目前为止,我不允许在远程仓库中移动这个修改过的 .gitIgnore。

.classpath and .project files are important for eclipse - which is my java editor.

.classpath 和 .project 文件对 eclipse 很重要——这是我的 java 编辑器。

I first of all selectively added my rest of the files and committed for staging. However, final push cannot be performed unless the modified .gitIgnore fiels and the untracked files viz. .project and .classpath are not stashed.

我首先有选择地添加了我的其余文件并提交进行暂存。但是,除非修改了 .gitIgnore 字段和未跟踪的文件,否则无法执行最终推送。.project 和 .classpath 没有被隐藏。

I used

我用了

 git stash 

for stashing the modified .gitIgnore file.

用于隐藏修改后的 .gitIgnore 文件。

For stashing .classpath and .project file, I used

为了隐藏 .classpath 和 .project 文件,我使用了

git stash --include-untracked

and it removed the files from my workspace. Absence of these files takes away my capability of working on my work location in eclipse. I proceeded on with completing the procedure for pushing the committed files to remote. Once this was done successfully, I used

它从我的工作区中删除了文件。缺少这些文件会削弱我在 eclipse 中处理工作位置的能力。我继续完成将提交的文件推送到远程的过程。成功完成后,我使用了

git stash pop

This pasted the same files back in my workspace. This gave back to me my ability to work on the same project in eclipse. Hope this brushes aside misconceptions.

这将相同的文件粘贴回我的工作区。这让我有能力在 Eclipse 中处理同一个项目。希望这能消除误解。

回答by user1012513

On git version 2.8.1: following works for me.

在 git 2.8.1 版上:以下对我有用。

To save modified and untracked files in stash without a name

将修改和未跟踪的文件保存在没有名称的存储中

git stash save -u

To save modified and untracked files in stash with a name

使用名称将已修改和未跟踪的文件保存在 stash 中

git stash save -u <name_of_stash>

You can use either popor applylater as follows.

您可以使用pop或稍后应用,如下所示。

git stash pop

git stash apply stash@{0}

回答by lindes

As has been said elsewhere, the answer is to git addthe file. e.g.:

正如其他地方所说,答案是git add文件。例如:

git add path/to/untracked-file
git stash

However, the question is also raised in another answer: What if you don't really want to add the file? Well, as far as I can tell, you have to. And the following will NOTwork:

但是,另一个答案中也提出了这个问题:如果您真的不想添加文件怎么办?好吧,据我所知,你必须这样做。而下面将工作:

git add -N path/to/untracked/file     # note: -N is short for --intent-to-add
git stash

this will fail, as follows:

这将失败,如下所示:

path/to/untracked-file: not added yet
fatal: git-write-tree: error building trees
Cannot save the current index state

So, what can you do? Well, you have to truly add the file, however, you can effectively un-add it later, with git rm --cached:

所以,你可以做什么?好吧,您必须真正添加文件,但是,您可以稍后有效地取消添加它git rm --cached

git add path/to/untracked-file
git stash save "don't forget to un-add path/to/untracked-file" # stash w/reminder
# do some other work
git stash list
# shows:
# stash@{0}: On master: don't forget to un-add path/to/untracked-file
git stash pop   # or apply instead of pop, to keep the stash available
git rm --cached path/to/untracked-file

And then you can continue working, in the same state as you were in before the git add(namely with an untracked file called path/to/untracked-file; plus any other changes you might have had to tracked files).

然后您可以继续工作,与之前的状态相同git add(即使用名为path/to/untracked-file;的未跟踪文件以及您可能必须跟踪文件的任何其他更改)。

Another possibility for a workflow on this would be something like:

对此工作流程的另一种可能性是:

git ls-files -o > files-to-untrack
git add `cat files-to-untrack` # note: files-to-untrack will be listed, itself!
git stash
# do some work
git stash pop
git rm --cached `cat files-to-untrack`
rm files-to-untrack

[Note: As mentioned in a comment from @mancocapac, you may wish to add --exclude-standardto the git ls-filescommand (so, git ls-files -o --exclude-standard).]

[注意:正如@mancocapac 的评论中提到的,您可能希望添加--exclude-standardgit ls-files命令中 (so, git ls-files -o --exclude-standard)。]

... which could also be easily scripted -- even aliases would do (presented in zsh syntax; adjust as needed) [also, I shortened the filename so it all fits on the screen without scrolling in this answer; feel free to substitute an alternate filename of your choosing]:

...这也可以很容易地编写脚本 - 甚至别名也可以(以 zsh 语法呈现;根据需要进行调整)[此外,我缩短了文件名,使其全部适合屏幕,而无需在此答案中滚动;随意替换您选择的备用文件名]:

alias stashall='git ls-files -o > .gftu; git add `cat .gftu`; git stash'
alias unstashall='git stash pop; git rm --cached `cat .gftu`; rm .gftu'

Note that the latter might be better as a shell script or function, to allow parameters to be supplied to git stash, in case you don't want popbut apply, and/or want to be able to specify a specific stash, rather than just taking the top one. Perhaps this (instead of the second alias, above) [whitespace stripped to fit without scrolling; re-add for increased legibility]:

请注意,后者可能更适合作为 shell 脚本或函数,以允许将参数提供给git stash,以防您不想要popbutapply和/或希望能够指定特定的存储,而不是仅仅取顶部一。也许这个(而不是上面的第二个别名)[空白被剥离以适应而不滚动;重新添加以提高易读性]:

function unstashall(){git stash "${@:-pop}";git rm --cached `cat .gftu`;rm .gftu}

Note: In this form, you need to supply an action argument as well as the identifier if you're going to supply a stash identifier, e.g. unstashall apply stash@{1}or unstashall pop stash@{1}

注意:在这种形式中,如果您要提供存储标识符,则需要提供操作参数和标识符,例如unstashall apply stash@{1}unstashall pop stash@{1}

Which of course you'd put in your .zshrcor equivalent to make exist long-term.

当然,你会投入你的.zshrc或等效的东西来长期存在。

Hopefully this answer is helpful to someone, putting everything together all in one answer.

希望这个答案对某人有帮助,将所有内容放在一个答案中。

回答by Oded

I was able to stash justthe untracked files by doing:

我能藏匿只是做未跟踪文件:

git stash save "tracked files I'm working on"
git stash save -u "untracked files I'm trying to stash"
git stash pop stash@{1}

The last one pops the stash of the tracked files, thus leaving only the untracked files stashed.

最后一个弹出跟踪文件的存储,从而只留下未跟踪的文件。

回答by Tdy

If you want to stash untracked files, but keep indexed files (the ones you're about to commit for example), just add -k(keep index) option to the -u

如果您想隐藏未跟踪的文件,但保留索引文件(例如您将要提交的文件),只需将-k(keep index) 选项添加到-u

git stash -u -k

回答by shubham mishra

You can simply do it with below command

您可以简单地使用以下命令来完成

git stash save --include-untracked

or

或者

git stash save -u

For more about git stash Visit this post (Click Here)

有关 git stash 的更多信息,请访问此帖子(单击此处)

回答by martline1

let's suppose the new and untracked file is called: "views.json". if you want to change branch by stashing the state of your app, I generally type:

让我们假设新的和未跟踪的文件被称为:“views.json”。如果您想通过隐藏应用程序的状态来更改分支,我通常会输入:

git add views.json

Then:

然后:

git stash

And it would be stashed. Then I can just change branch with

它会被藏起来。然后我可以改变分支

git checkout other-nice-branch