Create a git patch from the uncommitted changes in the current working directory

后端 未结 7 1520
小蘑菇
小蘑菇 2020-11-27 08:53

Say I have uncommitted changes in my working directory. How can I make a patch from those without having to create a commit?

7条回答
  •  北海茫月
    2020-11-27 09:11

    If you haven't yet commited the changes, then:

    git diff > mypatch.patch
    

    But sometimes it happens that part of the stuff you're doing are new files that are untracked and won't be in your git diff output. So, one way to do a patch is to stage everything for a new commit (git add each file, or just git add .) but don't do the commit, and then:

    git diff --cached > mypatch.patch
    

    Add the 'binary' option if you want to add binary files to the patch (e.g. mp3 files):

    git diff --cached --binary > mypatch.patch
    

    You can later apply the patch:

    git apply mypatch.patch
    

提交回复
热议问题