藏到变化到特定的文件 [英] Stash changes to specific files

查看:80
本文介绍了藏到变化到特定的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的git项目,我愚蠢地将其导入到Eclipse中并运行自动格式化。现在,项目中的每个文件都显示为已修改。我宁愿恢复所有只有格式化的文件,而没有其他更改,而不是提交格式化的文件。例如:

  $ git status 
#在分支主机上
#没有为commit提交的更改:
#(使用git add< file> ...来更新将要提交的内容)
#(使用git checkout - < file> ...放弃工作目录中的更改)
#(在子模块中提交或放弃未跟踪或修改的内容)

#修改:dir / file1.cpp
#修改:dir / file1.h
#modified:dir / file2.cpp
#modified:dir / file2.h
#modified:dir / file3.cpp
#modified:dir / file3.h
#modified :dir / file4.cpp
#modified:dir / file4.h

我知道 file2.cpp file2.h file3.cpp 已经被内容修改(即,不只是格式化)。我想隐藏对这三个文件的更改,然后签出旧版本,以便我可以在之后重新应用这些文件的更改。我宁愿避免这样的事情:

  $ cp file2.cpp〜/ tmp 
$ git checkout blahblahblah
$ cp〜/ tmp / file2.cpp。

如果有一个明显的方法可以做到这一点,不会涉及隐藏,请告诉我。无论如何完成任务。

您可以添加然后隐藏其余文件并清除存储:

  git add file2.cpp file2.h file3.cpp 
git stash --keep-index

此时,您已经隐藏了不需要的更改。如果你想永久地摆脱它们,运行:

  git存储放置
file2.cpp
file2.h $ b> / code>,以及 file3.cpp 进行提交。如果你想存储这些文件(而不是提交它们):

  git reset 
git stash

现在您将进入之前的提交阶段,只保留这三个文件。



更新:

Git 2.13及更高版本包含更直接的方式来存储特定文件, c> git stash push 正如VonC在他的回答中所述


I have a large git project that I, stupidly, imported to eclipse and ran an autoformat on. Now, every file in the project is showing as modified. Rather than commit my formatted files, I would rather revert all the files that I have only been formatted and not had other changes. For instance:

$ git status
# On branch master
# 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)
#   (commit or discard the untracked or modified content in submodules)

#     modified: dir/file1.cpp
#     modified: dir/file1.h
#     modified: dir/file2.cpp
#     modified: dir/file2.h
#     modified: dir/file3.cpp
#     modified: dir/file3.h
#     modified: dir/file4.cpp
#     modified: dir/file4.h

I know that file2.cpp, file2.h, and file3.cpp have been modified with content (i.e., not just formatted). I want to stash the changes to these three files and then checkout an old revision, so that I can reapply the changes to these files after. I would rather avoid something like:

$ cp file2.cpp ~/tmp
$ git checkout blahblahblah
$ cp ~/tmp/file2.cpp .

If there's an obvious way to do this that doesnt involve stashing, let me know. whatever gets the job done.

解决方案

You can add the files with changes you want to keep, then stash the rest of the files and clear the stash:

git add file2.cpp file2.h file3.cpp
git stash --keep-index

At this point, you've stashed your unwanted changes. If you'd like to permanently get rid of them, run:

git stash drop

Now you have file2.cpp, file2.h, and file3.cpp staged for commit. If you then want to stash these files (and not commit them):

git reset
git stash

Now you'll be at your previous commit, with only those three files stashed.

Update:

Git 2.13 and later includes a more direct way to stash specific files with git stash push, as VonC explains in his answer.

这篇关于藏到变化到特定的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆