Git undo auto merging on a specific file only, not the whole branch

后端 未结 3 1169
醉梦人生
醉梦人生 2021-02-18 18:04

Is there a way I can undo a auto-merge of one file that has been changed instead of the whole branch in Git?

I want to undo this merge:

Auto-mergi

3条回答
  •  感情败类
    2021-02-18 18:38

    You cannot reset a single file, because reset only works on whole commits (it basically "forgets" newer commits). Also, you cannot "change" an existing commit - more on that below.

    In your case, you have several options:

    The safest method would be to just checkout the old version of your file, and check-in again

    $ git checkout HEAD~1 -- public/stylesheet/application.css
    # Back at old version, change already staged
    $ git commit -m "Reverted change to public/stylesheet/application.css"
    

    This will simply create a new commit that turns back your accidental changes, and should be your preferred option unless you know exactly what you are doing.

    Second, reset the repository as above, and re-check in the commit:

    $ git reset HEAD~1
    # Now restore your file public/stylesheets/application.css
    $ git commit -m "Re-checkin of the commit"
    # You may use the old checkin message here
    

    This will effectively "change" the existing commit in the way that you will have the same commit in your history, with the same description, minus the modifications to the one file. However, this changes you history, so you must not use this if have already pushed the original commit to any upstream repository.

    Git does not allow you to change existing commits in any other way than by throwing away the existing ones and creating new ones. This will always "break" your history and should be only used if you exactly know what this feature does and why you want to do it.

提交回复
热议问题