Hintron
Hintron

Reputation: 331

Git: How to save a branch's history into a single file

I'm iterating on a code feature in a Git branch that will eventually be boiled down into a single commit/patch via rebase. However, I still want to keep the original commits around in a single history file somehow, so I have a record of my work. What is the best way to do this?

The best method I can think of right now would be to use git format-patch master and then concatenate together all of the patch files generated into a single file. The only problem with this approach is that I'm lazy. I can't be bothered to do all that outside of git.

Upvotes: 1

Views: 2128

Answers (2)

Hintron
Hintron

Reputation: 331

Another solution is this:

git format-patch --stdout <commit_or_branch> > <patchfile>

This concatenates all the commits after <commit_or_branch> into a single output, and > redirects that output from stdout into a patch file of the name <patchfile>. This patch file can now be easily applied via git am <patchfile>.

We use this method to transact in multi-commit patch files at my company, and it works very nicely.


How is the solution above different than this solution mentioned by Marcin Pietraszek?

git log --cc <some_commit>...HEAD > all_history_in_a_single_file.txt

The difference is that Marcin's solution does not produce a "patch" file that can be consumed by git am.

Here's an example to show how they are different for the same two commits. My method:

$ git format-patch --stdout origin/master 
From 33b83d0314c9c3090a0e27e4c2b46beb58ee1739 Mon Sep 17 00:00:00 2001
From: REDACTED
Date: Wed, 14 Apr 2021 18:21:13 -0600
Subject: [PATCH 1/2] Test 1

---
 INSTALL | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/INSTALL b/INSTALL
index fb926ff73b..ae3c3e312d 100644
--- a/INSTALL
+++ b/INSTALL
@@ -1,6 +1,6 @@
 Copyright 1994, 1995, 1996, 1999, 2000, 2001, 2002 Free Software
 Foundation, Inc.
-
+Hi
    This file is free documentation; the Free Software Foundation gives
 unlimited permission to copy, distribute and modify it.
 
-- 
2.25.1


From 88ff375059e12d6053ab43cfdeeefd179e24a2db Mon Sep 17 00:00:00 2001
From: REDACTED
Date: Wed, 14 Apr 2021 18:21:37 -0600
Subject: [PATCH 2/2] test 2

---
 INSTALL | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/INSTALL b/INSTALL
index ae3c3e312d..97d6a8f86a 100644
--- a/INSTALL
+++ b/INSTALL
@@ -1,6 +1,6 @@
 Copyright 1994, 1995, 1996, 1999, 2000, 2001, 2002 Free Software
 Foundation, Inc.
-Hi
+Hello
    This file is free documentation; the Free Software Foundation gives
 unlimited permission to copy, distribute and modify it.
 
-- 
2.25.1

Marcin's method:

$ git log --cc origin/master...HEAD 
commit 88ff375059e12d6053ab43cfdeeefd179e24a2db (HEAD -> temp)
Author: REDACTED
Date:   Wed Apr 14 18:21:37 2021 -0600

    test 2

diff --git a/INSTALL b/INSTALL
index ae3c3e312d..97d6a8f86a 100644
--- a/INSTALL
+++ b/INSTALL
@@ -1,6 +1,6 @@
 Copyright 1994, 1995, 1996, 1999, 2000, 2001, 2002 Free Software
 Foundation, Inc.
-Hi
+Hello
    This file is free documentation; the Free Software Foundation gives
 unlimited permission to copy, distribute and modify it.
 

commit 33b83d0314c9c3090a0e27e4c2b46beb58ee1739
Author: REDACTED
Date:   Wed Apr 14 18:21:13 2021 -0600

    Test 1

diff --git a/INSTALL b/INSTALL
index fb926ff73b..ae3c3e312d 100644
--- a/INSTALL
+++ b/INSTALL
@@ -1,6 +1,6 @@
 Copyright 1994, 1995, 1996, 1999, 2000, 2001, 2002 Free Software
 Foundation, Inc.
-
+Hi
    This file is free documentation; the Free Software Foundation gives
 unlimited permission to copy, distribute and modify it.

Upvotes: 0

Marcin Pietraszek
Marcin Pietraszek

Reputation: 3214

Branches in git are cheap. Branch is just a file showing a commit in your repository (I'm deliberately skipping potential GC when you'll squash, merge and delete branch).

The "git-way" of doing that would be just to:

git branch feature-backup
git rebase <some_commit> 

After that feature-backup branch will still have your old history. You don't need to push feature-backup branch to remote. It could be just your local branch.

The other approach to have the changes from your branch would be:

git diff <some_commit>...HEAD > all_commits_in_a_single_file.patch
git rebase <some_commit>

With that you'll have all the changes that are on your branch in a single patch file.

If you'd like to have all the commits separated you could use:

git log --cc <some_commit>...HEAD > all_history_in_a_single_file.txt

The last option would give you all concatenated diffs with commit messages and stuff.

Upvotes: 1

Related Questions