Git:如何将分支的历史记录保存到单个文件中



我正在迭代 Git 分支中的一个代码功能,该功能最终将通过变基归结为单个提交/补丁。但是,我仍然希望以某种方式将原始提交保留在一个历史记录文件中,因此我有我的工作记录。最好的方法是什么?

我现在能想到的最好的方法是使用git format-patch master,然后将生成的所有补丁文件连接成一个文件。这种方法的唯一问题是我很懒。我懒得在 git 之外做所有这些事情。

git 中的分支很便宜。分支只是一个在存储库中显示提交的文件(当您压缩、合并和删除分支时,我故意跳过潜在的 GC(。

这样做的"git-way"只是:

git branch feature-backup
git rebase <some_commit> 

在那之后feature-backup分支仍将保留您的旧历史记录。您无需将分支推送feature-backup远程。它可能只是您当地的分支机构。

从分支进行更改的另一种方法是:

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

这样,您将在单个补丁文件中拥有分支上的所有更改。

如果你想将所有提交分开,你可以使用:

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

最后一个选项将为您提供所有带有提交消息和其他内容的串联差异。

另一个解决方案是这样的:

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

这会将<commit_or_branch>后的所有提交连接成单个输出,>将该输出从 stdout 重定向到名为<patchfile>的修补程序文件中。现在可以通过git am <patchfile>轻松应用此修补程序文件。

我们在我的公司使用这种方法在多提交补丁文件中进行交易,它工作得很好。


上面的解决方案与Marcin Pietraszek提到的这个解决方案有何不同?

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

不同之处在于,Marcin 的解决方案不会生成可供git am使用的"补丁"文件。

下面是一个示例,用于说明它们对于相同的两个提交有何不同。我的方法:

$ 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

马尔钦的方法:

$ 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.

最新更新