gitpatchgit-patch

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?


Solution

  • 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