How to create a patch without commit in Git

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?


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.


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


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

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