1

Is there a way to create a new commit in Git, using a file which provides both the changes and the commit message, like the output from git show does?

It would be nice when the other metadata fields, like Author, AuthorDate and so on would also be taken into account.

philipp2100
  • 162
  • 1
  • 11

1 Answers1

0

Yes, git show has a -p flag for this purpose.

git show -p abcd1234 > path/to/file.patch

It'll generate a text file (you can open it with any text editor) with a content in the following form (mind the metadata you wanted)

commit 8aab31565962f681639d0a7b6b5b8c0d3fe6b695
Author: John Doe <john.doe@corporation.com>
Date:   Tue May 28 17:05:01 2019 +0200

    Made some critical changes to function foo_bar

diff --git a/path/to/file b/path/to/file
index 8a443961df..5b5ad4726a 100755
--- a/path/to/file
+++ b/path/to/file
@@ -2620,6 +2620,6 @@ function foo_bar() {
 /**
  * Function documentation
  */
-function foo_bar() {
+function foo_bar() {
   someFunction("param");
 }

then you'll be able at some future point to apply the patch elsewhere with

git checkout someBranch
git apply path/to/file.patch

See the appropriate doc section for the details.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • This doesn't work for me. While the files are changed, the changes are not committed. The result is the same as if I used `patch -p1 – philipp2100 May 29 '19 at 09:37
  • @philipp2100 To be noted also : be aware that there won't be any diff shown in the case of a merge commit. – Romain Valeri May 29 '19 at 09:44
  • `git apply` doesn't make a commit from the patch. `git am` *does*, but needs its input in the form generated by `git format-patch`, which is slightly different. – torek May 29 '19 at 16:30
  • @torek Agreed, but in some contexts, wouldn't it be handy to have the opportunity to inspect the changes brought by the patch, and commit manually, for example to add something to the original commit message? – Romain Valeri May 29 '19 at 21:21
  • Yes, depending on context I sometimes prefer `git apply` - but @philipp2100 was looking for a variant that acts like `git am`. – torek May 29 '19 at 21:36