如何“git reset --soft HEAD^”单个文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21469678/
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
How to "git reset --soft HEAD^" a single file?
提问by Rhinon
When I am splitting a commit during interactive rebase, I often would like to extract specific files from the last commit.
当我在交互式 rebase 期间拆分提交时,我经常想从上次提交中提取特定文件。
My current process is to
我目前的流程是
- Copy the last commit message to my clipboard,
- git reset --soft HEAD^ (last commit is undone, changes are staged)
- Unstage files I wanted extract
- Re-commit (pasting in the copied commit message)
- Add/commit the remaining files
- Continue with rebase
- 将最后一条提交消息复制到我的剪贴板,
- git reset --soft HEAD^(最后一次提交已撤消,更改已暂存)
- 取消暂存我想要提取的文件
- 重新提交(粘贴复制的提交信息)
- 添加/提交剩余的文件
- 继续变基
I feel like this would be simpler if I could soft reset a specific file.
我觉得如果我可以软重置特定文件,这会更简单。
回答by mellowfish
Are you trying to do git checkout $COMMIT_HASH some_file.ext
?
你想做git checkout $COMMIT_HASH some_file.ext
什么?
You can use this to revert a file to it's previous state, and git stages this change.
您可以使用它来将文件恢复到以前的状态,并且 git 会暂存此更改。
回答by Emil Davtyan
It seems this does what you want :
看来这可以满足您的要求:
git reset [-q] [commit] [--] paths…
This form resets the index entries for allpaths
to their state atcommit
. (It does not affect the working tree, nor the current branch.)This means that git reset is the opposite of git add .
git reset [-q] [commit] [--] paths…
此表单将所有索引条目重置paths
为它们在 处的状态commit
。(它不影响工作树,也不影响当前分支。)这意味着 git reset 与 git add 相反。
回答by hojin
I am not sure it's an optimal solution.
我不确定这是一个最佳解决方案。
git reset HEAD~
git add .
git commit
(optional) git reset --hard HEAD
回答by Siddharth Pandey
An easy way could be
一个简单的方法可能是
Add all files
git add .
Now reset the files you do not want to commit
git reset 'file1.ext' git reset 'file2.ext' ... so on
Continue your commit and push
添加所有文件
git 添加。
现在重置您不想提交的文件
git reset 'file1.ext' git reset 'file2.ext' ... 以此类推
继续你的提交和推送