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

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 something_here > save.patch
$ git checkout . 
$ patch < save.patch
$ git diff > after
$ diff before after

With something_here blank it almost works, but the file names aren’t right. I think I’m just I’m missing some option.

  • Fix missing commit
  • How do I completely clean a git working directory?
  • Maven and Gitlab: release:prepare uses the wrong SCM URL
  • “git log -1 fullpath/myfile” with libgit2
  • Change the root directory of a Git repository
  • Fastest way to merge branches via gitlab (or git)?
  • In real life, I am going to do a merge after the checkout, so the patch might fail there, but you see what I’m getting at.

    My fault here for asking the wrong question. The actual question is, I want to save my changes away, do a merge, then re-apply the changes, if possible? I asked it the wrong way because I am used to using patch to solve these sorts of problems and git diff looked like that’s what it wanted me to do.

    Charles Bailey’s comment had the right answer. For me, git-apply is the right thing to do (git-stash looks more heavy-weight than I need and rebasing and bundles is definitely beyond my current skill level.) I’m going to accept the answer Charles gave (because you can’t accept a comment). Thanks for all the suggestions.

    Edit, 6 years later
    As anyone familiar with the subject knows, I over-estimated the difficulty of git stash. Pretty much every day or so, I will use the following sequence:

    $ git stash
    $ git merge
    $ git stash pop

  • See Nth previous version of file
  • git clone https/ssl error
  • (Git) How can I set parameters using alias
  • How to git merge --squash so that github “network” diagram shows the merge
  • git aws.push uploaded new php application to Elastic Beanstalk
  • unexpected changes after checkout
  • 6 Solutions collect form web for “Can I get a patch-compatible output from git-diff?”

    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

    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

    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

    A useful trick to avoid creating temporary patch files:

    git diff | patch -p1 -d [dst-dir]

    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

    As mentioned by @ndim

    $ git diff > save.patch

    creates the patch, the save.patch file gets created in the same code base directory. This file can be shared with others. Once you have the save.patch file, run

    $ patch -p1 < save.patch

    to apply the patch to the code base

    Git Baby is a git and github fan, let's start git clone.