如何“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

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

How to "git reset --soft HEAD^" a single file?

git

提问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

我目前的流程是

  1. Copy the last commit message to my clipboard,
  2. git reset --soft HEAD^ (last commit is undone, changes are staged)
  3. Unstage files I wanted extract
  4. Re-commit (pasting in the copied commit message)
  5. Add/commit the remaining files
  6. Continue with rebase
  1. 将最后一条提交消息复制到我的剪贴板,
  2. git reset --soft HEAD^(最后一次提交已撤消,更改已暂存)
  3. 取消暂存我想要提取的文件
  4. 重新提交(粘贴复制的提交信息)
  5. 添加/提交剩余的文件
  6. 继续变基

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 all pathsto their state at commit. (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

一个简单的方法可能是

  1. Add all files

    git add .

  2. Now reset the files you do not want to commit

    git reset 'file1.ext' git reset 'file2.ext' ... so on

  3. Continue your commit and push

  1. 添加所有文件

    git 添加。

  2. 现在重置您不想提交的文件

    git reset 'file1.ext' git reset 'file2.ext' ... 以此类推

  3. 继续你的提交和推送