2016-02-08 32 views
2

最好使用SourceTree或简单的git命令,我将如何撤消对先前提交中的1个文件的更改。换一种说法,如何反转提交,但仅限于提交的许多文件中的一个?如何在先前的提交中恢复对1个文件的更改

寻求避免必须扭转整个提交,然后重新提交除1个文件的更改之外的所有内容。

编辑: 我最终通过编辑手动“恢复”了1个文件。但是,得到了两个很好看的答案,我会选择一个看起来在更多情况下工作的答案。

回答

2

务必:

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 
3

如果你正在寻找撤消最新的承诺,它尚未推,你可以发出以下命令:

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

请注意,只有在要提交的提交是最后一个时才适用。另外,'--amend'只在该提交尚未被推送时才起作用。 –

+0

谢谢,我已经编辑了我的答案。 – Yoel

0

要恢复的变化任意提交中的文件,

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 

但是如果不需要的更改是在最近的提交中,您可以只ch直接使用未更改的版本

git checkout @^ $thatfile   # restore mainline-parent content 
相关问题