[英]How to revert changes to 1 file in previous commit
最好使用SourceTree或簡單的git命令,在上一次提交中如何撤消對1個文件的更改。 換句話說,如何撤消提交,但僅對提交的多個文件中的一個進行撤消?
希望避免不得不撤消整個提交,然后重新提交除1個文件以外的所有更改。
編輯:我最終只是通過編輯手動“還原”了1個文件。 但是,盡管有2個好看的答案,但我將選擇在更多情況下似乎可行的答案。
如果您要撤消最新的提交但尚未提交,則可以發出以下命令:
git checkout HEAD^ -- <file_path> # revert and stage the problematic file
git commit --amend # edit the latest commmit
做:
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
要將更改以任意提交的方式還原到文件,
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
但是如果不需要的更改是在最近的提交中,您可以直接使用簽出未更改的版本
git checkout @^ $thatfile # restore mainline-parent content
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.