2016-02-08 29 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 
相關問題