git 单个文件的硬重置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7147270/
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
Hard reset of a single file
提问by Emiliano
I currently have three modified files in my working directory. However I want one of them to be reset to the HEAD status.
我目前在我的工作目录中有三个修改过的文件。但是我希望其中一个被重置为 HEAD 状态。
In SVN, I'd use svn revert <filename>
(followed by svn update <filename>
if needed) but in Git I should use git reset --hard
. However this command cannot operate on a single file.
在 SVN 中,我会使用svn revert <filename>
(svn update <filename>
如果需要,可以使用)但在 Git 中我应该使用git reset --hard
. 但是这个命令不能对单个文件进行操作。
Is there any way in Git to discard the changes to a single file and overwrite it with a fresh HEAD copy?
Git 中有没有办法放弃对单个文件的更改并用新的 HEAD 副本覆盖它?
回答by Mark Longair
You can use the following command:
您可以使用以下命令:
git checkout HEAD -- my-file.txt
... which will update both the working copy of my-file.txt
and its state in the index with that from HEAD.
...这将my-file.txt
使用 HEAD 中的工作副本及其在索引中的状态进行更新。
--
basically means: treat every argument after this point as a file name. More details in this answer. Thanks to VonCfor pointing this out.
回答by Acumenus
Reset to head:
重置为头部:
To hard reset a single file to HEAD:
要将单个文件硬重置为 HEAD:
git checkout @ -- myfile.ext
Note that @
is short for HEAD
. An older version of git may not support the short form.
请注意,它@
是 的缩写HEAD
。旧版本的 git 可能不支持短格式。
Reset to index:
重置为索引:
To hard reset a single file to the index, assuming the index is non-empty, otherwise to HEAD:
将单个文件硬重置为index,假设索引非空,否则为 HEAD:
git checkout -- myfile.ext
The point is that to be safe, you don't want to leave out @
or HEAD
from the command unless you specifically mean to reset to the indexonly.
关键是为了安全起见,除非您特别想仅重置为索引,否则您不想遗漏@
或HEAD
从命令中删除。
回答by Ann Raiho
To revert to upstream/master do:
要恢复到上游/主站,请执行以下操作:
git checkout upstream/master -- myfile.txt
回答by AxeEffect
Since Git 2.23(August 2019) you can use restore
(more info):
自Git 2.23(2019 年 8 月)起,您可以使用restore
(更多信息):
git restore pathTo/MyFile
The above will restore MyFile
on HEAD
(the last commit) on the current branch.
上面的代码将恢复MyFile
上HEAD
(最后一次提交)对当前分支。
If you want to get the changes from other commit you can go backwards on the commit history. The below command will get MyFile
two commits previous to the last one. You need now the -s
(--source
) option since now you use master~2
and not master
(the default) as you restore source:
如果您想从其他提交中获取更改,您可以在提交历史上倒退。下面的命令将MyFile
在最后一次提交之前获得两次提交。您现在需要-s
( --source
) 选项,因为现在您在恢复源时使用master~2
而不是master
(默认):
git restore -s master~2 pathTo/MyFile
You can also get the file from other branch!
您也可以从其他分支获取文件!
git restore -s my-feature-branch pathTo/MyFile
回答by Emanuel Friedrich
Reference to HEAD is not necessary.
不需要引用 HEAD。
git checkout -- file.js
is sufficient
git checkout -- file.js
足够了
回答by HariKishore
you can use the below command for reset of single file
您可以使用以下命令重置单个文件
git checkout HEAD -- path_to_file/file_name
List all changed files to get path_to_file/filename
with below command
列出所有更改的文件以path_to_file/filename
使用以下命令获取
git status
回答by ADDYQU
You can use the following command:
您可以使用以下命令:
git reset -- my-file.txt
git reset -- my-file.txt
which will update both the working copy of my-file.txt
when added.
这将更新my-file.txt
添加时的工作副本。
回答by Milad Rahimi
You can use the following command:
您可以使用以下命令:
git checkout filename
If you have a branch with the same file name you have to use this command:
如果您有一个具有相同文件名的分支,则必须使用以下命令:
git checkout -- filename
回答by kmiklas
A simple, easy, hands-on, way to get you out of hot water, especially if you're not so comfortable with git:
一种让您摆脱热水的简单、容易、动手的方法,尤其是当您对 git 不太熟悉时:
View the log of your file
git log myFile.js
commit 1023057173029091u23f01w276931f7f42595f84f Author: kmiklas Date: Tue Aug 7 09:29:34 2018 -0400
JIRA-12345 - Refactor with new architecture.
Note hash of file:
1023057173029091u23f01w276931f7f42595f84f
Show the file using the hash. Make sure it's what you want:
git show 1023057173029091u23f01w276931f7f42595f84f:./myFile.js
Redirect file to a local copy
git show 1023057173029091u23f01w276931f7f42595f84f:./myFile.js > myFile.07aug2018.js
Back up your current file.
cp myFile.js myFile.bak.js
Open both files in your favorite text editor.
vim myFile.js
vim myFile.07aug2018.jsCopy n' paste code from myFile.07aug2018.js to myFile.js, and save.
Commit and push myFile.js
Again view the log, and confirm that your file is properly in place.
Tell your clients to pull the latest, happily watch it work with the old version in place.
查看文件的日志
git log myFile.js
提交 1023057173029091u23f01w276931f7f42595f84f 作者:kmiklas 日期:2018 年 8 月 7 日星期二 09:29:34 -0400
JIRA-12345 - 使用新架构重构。
注意文件的哈希值:
1023057173029091u23f01w276931f7f42595f84f
使用哈希显示文件。确保它是你想要的:
git 显示 1023057173029091u23f01w276931f7f42595f84f:./myFile.js
将文件重定向到本地副本
git 显示 1023057173029091u23f01w276931f7f42595f84f:./myFile.js > myFile.07aug2018.js
备份您当前的文件。
cp myFile.js myFile.bak.js
在您喜欢的文本编辑器中打开这两个文件。
vim myFile.js
vim myFile.07aug2018.js将粘贴代码从 myFile.07aug2018.js 复制到 myFile.js,并保存。
提交并推送 myFile.js
再次查看日志,并确认您的文件正确就位。
告诉你的客户拉取最新的,愉快地看着它与旧版本一起工作。
Not the sexiest, or most git-centric solution, and definitely a "manual" reset/reversion, but it works. It requires minimal knowledge of git, and doesn't disturb the commit history.
不是最性感或最以 git 为中心的解决方案,绝对是“手动”重置/恢复,但它有效。它需要最少的 git 知识,并且不会干扰提交历史。