简体   繁体   English

如何在上一次提交中将更改还原到1个文件

[英]How to revert changes to 1 file in previous commit

Using SourceTree preferably or a simple git command, how would I undo the changes to 1 file in previous commit. 最好使用SourceTree或简单的git命令,在上一次提交中如何撤消对1个文件的更改。 Saying it another way, how to reverse a commit, but only for 1 of many files that were committed? 换句话说,如何撤消提交,但仅对提交的多个文件中的一个进行撤消?

Looking to avoid having to reverse the whole commit and then recommit all but the 1 file's changes. 希望避免不得不撤消整个提交,然后重新提交除1个文件以外的所有更改。

edit: I ended up just manually "reverting" the 1 file by editing it. 编辑:我最终只是通过编辑手动“还原”了1个文件。 But got 2 good looking answers though, I'll pick the one that appears to work in more cases. 但是,尽管有2个好看的答案,但我将选择在更多情况下似乎可行的答案。

If you're looking to undo the latest commit and it wasn't pushed yet, you may issue the following commands: 如果您要撤消最新的提交但尚未提交,则可以发出以下命令:

git checkout HEAD^ -- <file_path>  # revert and stage the problematic file
git commit --amend                 # edit the latest commmit

Do: 做:

git revert <commit> --no-commit     #reverts the whole commit, putting changes in index and working dir
git reset HEAD .                    #clears index of changes
git add <fileyouwanttorevert>       #adds changes to that one file to index
git commit -m "Reverting the file"  #commits that one file's changes
git checkout .                      #gets rid of all the changes in working directory

To revert the changes to a file in an arbitrary commit, 要将更改以任意提交的方式还原到文件,

git revert $thecommit              # revert the whole commit
git reset --hard @{1}              # in what turns out to have been a throwaway commit
git checkout @{1} $thatfile        # take what you want

but if the unwanted changes are in the most recent commit you can just check out the unaltered version directly with 但是如果不需要的更改是在最近的提交中,您可以直接使用签出未更改的版本

git checkout @^ $thatfile          # restore mainline-parent content

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM