51

I did some search online. I know that you can use format-patch after commit, but my situation is a little different.

I want to create a patch, similar to "dpk" in SVN, so I can send it out for code review, but I don't yet want to commit it.

How can I achieve this with Git?

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
doglin
  • 1,651
  • 4
  • 28
  • 38
  • 6
    A major point of a DVCS like git is that there is no reason at all to avoid committing something. – Michael Borgwardt Mar 21 '12 at 18:42
  • 2
    Nobody has answered the question.. He's not asked anything about commits.. hes asked how do you produce a patch that can be distributed to other users. – Stephen Oct 26 '13 at 21:57
  • Related: [git format-patch without commiting](http://stackoverflow.com/q/7837218/183120) – legends2k May 10 '15 at 08:27
  • Is there a reason you don't prefer the answer from @RayLuo ? You may want to edit your title if you want to generate a diff *after* commit; I definitely came expecting a different accepted answer. – ruffin May 31 '17 at 00:40

5 Answers5

144

When other guys had already given some answer which comply with git convention, the OP's question, "create a patch without commit", can be also solved in this way:

git diff > my_patch.txt

Later you can apply this patch, also without a commit, by:

git apply my_patch.txt

But if you are just working locally, a git checkout another_branch -m is good enough to bring all your current uncommit changes to that another_branch, without even patch and apply.

RayLuo
  • 17,257
  • 6
  • 88
  • 73
  • 2
    in case you want to remove this applied patch: git apply -R my_patch.txt – Sourabh Sep 28 '16 at 04:37
  • 4
    Like this we don't include new/untracked files! If you need to include new files, you should add all files to the index (git add) and then to a $ git diff --cached > my_patch.txt – dxvargas Dec 05 '16 at 00:33
  • 6
    This also does not include staged files. You'll need to run `git diff --staged > my_patch.txt` to include them – Vadim Kotov Oct 04 '17 at 13:27
  • @alper, the `patch` is not magic. If you attemp to apply a patch to some significantly-different code, the auto-merge might fail. Same thing happens all the time even when you attempt merging 2 very different git branches. You will need to manually solve that. – RayLuo Oct 05 '19 at 21:22
12

general step to generate patch without commit at last

  1. commit your local changes using

    git commit -a -m "specific message"
    

    Note : don't push this commit.

  2. generate patch

    git format-patch -s -n -1 HEAD   
    

    it will generate 0001-.patch

  3. revert back local commit

    git reset --soft HEAD~1
    

    to delete commit but keep your work

    git reset --hard HEAD~1
    

    to delete commit with your work

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Pintu Patel
  • 143
  • 1
  • 5
8

Committing in Git is a cheap and entirely local operation, so there is no reason to avoid committing as long as you don't push it anywhere.

Just make a new local branch and commit your changes there. You can always delete the branch later if you don't want it anymore, or you can keep the branch and use it for working on whatever you're doing, then merge (or rebase) it into the master branch when it's ready. This is a good workflow to use when working with Git.

$ git checkout -b feature-foo  # create and switch to new branch feature-foo
$ git commit

# do whatever you need to do

$ git checkout master          # switch back to the master branch
$ git merge feature-foo        # merge your change into master (optional)
$ git branch -d feature-foo    # delete the branch
hammar
  • 138,522
  • 17
  • 304
  • 385
7

Like @hammar said, commit is cheap and then you can blow away the commit with git reset etc.

You can also stash and then do:

git stash show -p
manojlds
  • 290,304
  • 63
  • 469
  • 417
1

A commit to a local repo in git is not "binding". You can commit your changes, create your patch and then do a soft reset on your branch to the previous commit and it is like your commit never happened.

That being said, there really is no reason you HAVE to reset your branch after creating the patch. You can leave the commit in the repo and just avoid pushing it until the code review is done. If you have to go back and make changes to the original commit you have options at that point.

And if you create a branch for the commit as hammar suggests it makes it even easier to go back and make changes later without having to do any annoying rebasing and such in the main branch before pushing.

mockobject
  • 1,797
  • 16
  • 26