How to create a patch without commit in Git?
Asked Answered
G

5

54

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?

Gee answered 21/3, 2012 at 18:34 Comment(4)
A major point of a DVCS like git is that there is no reason at all to avoid committing something.Luxuriance
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.Lamere
Related: git format-patch without commitingEadwine
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.Kilmarx
I
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
Ichthyosis answered 21/3, 2012 at 18:45 Comment(3)
Not helpful. Sometimes i want to see the local changes before commit so that i can catch things i didnt intent to commit.Lamere
You can do a git diff for that.Pteridophyte
@Lamere git add --patchReverend
M
151

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.

Mascle answered 23/2, 2014 at 23:46 Comment(4)
in case you want to remove this applied patch: git apply -R my_patch.txtApuleius
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.txtOdyssey
This also does not include staged files. You'll need to run git diff --staged > my_patch.txt to include themTristis
@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.Mascle
F
13

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

Friary answered 27/2, 2016 at 13:46 Comment(0)
I
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
Ichthyosis answered 21/3, 2012 at 18:45 Comment(3)
Not helpful. Sometimes i want to see the local changes before commit so that i can catch things i didnt intent to commit.Lamere
You can do a git diff for that.Pteridophyte
@Lamere git add --patchReverend
L
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
Lail answered 21/3, 2012 at 18:53 Comment(0)
Z
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.

Zipporah answered 21/3, 2012 at 18:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.