Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a git patch from the uncommitted changes in the current working directory

Tags:

git

git-patch

Say I have uncommitted changes in my working directory. How can I make a patch from those without having to create a commit?

like image 307
vrish88 Avatar asked Mar 01 '11 19:03

vrish88


People also ask

How do I create a patch in git?

To create a Git patch file, you have to use the “git format-patch” command, specify the branch and the target directory where you want your patches to be stored.

What is git patch command?

GIT patch or GIT diff is used to share the changes made by you to others without pushing it to main branch of the repository. This way other people can check your changes from the GIT patch file you made and suggest the necessary corrections.

What is create patch in Intellij?

Create a patch from an entire commit Locate the commit that you want to create a patch from in the Log tab of the Version Control tool window Alt+9 and select Create Patch from the context menu. In the Patch File Settings dialog, modify the default patch file location if necessary, and click OK.


2 Answers

If you haven't yet commited the changes, then:

git diff > mypatch.patch 

But sometimes it happens that part of the stuff you're doing are new files that are untracked and won't be in your git diff output. So, one way to do a patch is to stage everything for a new commit (git add each file, or just git add .) but don't do the commit, and then:

git diff --cached > mypatch.patch 

Add the 'binary' option if you want to add binary files to the patch (e.g. mp3 files):

git diff --cached --binary > mypatch.patch 

You can later apply the patch:

git apply mypatch.patch 
like image 157
jcarballo Avatar answered Sep 18 '22 17:09

jcarballo


git diff for unstaged changes.

git diff --cached for staged changes.

git diff HEAD for both staged and unstaged changes.

like image 43
sigjuice Avatar answered Sep 17 '22 17:09

sigjuice