9

I wanted to know whether it is possible to create patch files in git. I mean if I have done some changes in few files then patch file should be created with respect to the filename.

git diff displays the changes on the console but Is it possible to separate the output into different files?

5
  • What do you mean too limited data? That's the way I usually create a patch, or git diff master > filename.patch. When you patch it in you'll need to strip off the first part of the path with patch -p1 < filename.patch. Commented Jul 27, 2017 at 13:29
  • Sorry my bad @dj2 It saves successfully into single file but can it be saved to different files. for eg if file1 and file2 were modified can file1.patch and file2.patch be created for the same. Commented Jul 27, 2017 at 13:38
  • Why do you want to split the patch in individual files? Commented Jul 27, 2017 at 13:42
  • @t.niese The requirement is in such a way. I know I can achieve the same using eclipse. But is it possible using only git that is what I am wondering. Commented Jul 27, 2017 at 13:46
  • If you want each patch file contain the changes on a single file then put the file path into the git diff command. Is as easy as that. Commented Jul 27, 2017 at 13:47

2 Answers 2

11

git format-patch

Say you've made a commit aaa111 which modifies foo.txt, bar.txt and hello.txt.

git format-patch -1 aaa111

It generates a patch including three files' changes.

git format-patch -1 aaa111 -- foo.txt

It generates a patch including only the change of foo.txt.

git format-patch -1 aaa111 --stdout -- bar.txt > aaa111.bar.patch

It generates a patch named as aaa111.bar.patch which includes only the change of bar.txt

Update 2022-06-04

For the commit that touches binaries, you should add --binary to git format-patch.

Sign up to request clarification or add additional context in comments.

2 Comments

@ElpieKay Can you explain what -1 stands for (and what it is doing) please? I checked git format-patch -h but that isn't listed. Also, if I what to create patches for the recent 3 commits, each commit in one file, what should I do? thx.
@xpt -h lists only a few options. Use git format-patch --help or man git format-patch instead. -1 is an instance of -<n>, which "prepares patches from the topmost <n> commits". Without -1, it creates patches for every commit reachable from aaa111. With -1, only the patch of aaa111 is created. So with git format-patch -3 it creates patches for the recent 3 commits. Note that -<n> is different from -n. Both are available options for git format-patch.
1

The following script creates patches for the files modified on the most recent commit (HEAD~1):

# Enumerate the files modified on the desired commit
for file in $(git diff --name-only HEAD~1); do
    # Generate the name of the patch file: replac '/' with '_'
    # in the paths of the modified files and add the .patch termination
    patch=${file//\//_}.patch
    # Create one patch for each modified file
    git diff HEAD~1 -- $file > $patch
done

You can modify the generation of the patch file name to include a path (to not generate them in the current directory). Also to generate a diff between different commits, replace HEAD~1 in both places with the appropriate commit(s) identifiers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.