Can I get a patch-compatible output from git-diff?

前端 未结 5 993
傲寒
傲寒 2020-12-07 07:43

I am doing something very simple wrong. I\'m trying to prepare an ordinary patch file, so I can reapply some changes:

$ git diff > before
$ git diff some         


        
相关标签:
5条回答
  • 2020-12-07 07:49

    Just use -p1: you will need to use -p0 in the --no-prefix case anyway, so you can just leave out the --no-prefix and use -p1:

    $ git diff > save.patch
    $ patch -p1 < save.patch
    
    $ git diff --no-prefix > save.patch
    $ patch -p0 < save.patch
    
    0 讨论(0)
  • 2020-12-07 07:49
    1. I save the diff of the current directory (including uncommitted files) against the current HEAD.
    2. Then you can transport the save.patch file to wherever (including binary files).
    3. On your target machine, apply the patch using git apply <file>

    Note: it diff's the currently staged files too.

    $ git diff --binary --staged HEAD > save.patch
    $ git reset --hard
    $ <transport it>
    $ git apply save.patch
    
    0 讨论(0)
  • 2020-12-07 07:50

    A useful trick to avoid creating temporary patch files:

    git diff | patch -p1 -d [dst-dir]
    
    0 讨论(0)
  • 2020-12-07 08:01

    If you want to use patch you need to remove the a/ b/ prefixes that git uses by default. You can do this with the --no-prefix option (you can also do this with patch's -p option):

    git diff --no-prefix [<other git-diff arguments>]
    

    Usually though, it is easier to use straight git diff and then use the output to feed to git apply.

    Most of the time I try to avoid using textual patches. Usually one or more of temporary commits combined with rebase, git stash and bundles are easier to manage.

    For your use case I think that stash is most appropriate.

    # save uncommitted changes
    git stash
    
    # do a merge or some other operation
    git merge some-branch
    
    # re-apply changes, removing stash if successful
    # (you may be asked to resolve conflicts).
    git stash pop
    
    0 讨论(0)
  • 2020-12-07 08:06

    The git diffs have an extra path segment prepended to the file paths. You can strip the this entry in the path by specifying -p1 with patch, like so:

    patch -p1 < save.patch
    
    0 讨论(0)
提交回复
热议问题