简体   繁体   English

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

[英]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.我正在迭代 Git 分支中的代码功能,最终将通过 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.我现在能想到的最好方法是使用git format-patch master ,然后将所有生成的补丁文件连接到一个文件中。 The only problem with this approach is that I'm lazy.这种方法的唯一问题是我很懒。 I can't be bothered to do all that outside of git.我懒得在 git 之外做所有这些。

Branches in git are cheap. git 中的分支很便宜。 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).分支只是一个显示存储库中提交的文件(当您压缩、合并和删除分支时,我故意跳过潜在的 GC)。

The "git-way" of doing that would be just to:这样做的“git-way”就是:

git branch feature-backup
git rebase <some_commit> 

After that feature-backup branch will still have your old history.在那个feature-backup分支之后,你的旧历史仍然存在。 You don't need to push feature-backup branch to remote.您不需要将feature-backup分支推送到远程。 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.最后一个选项将为您提供所有带有提交消息和内容的连接差异。

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> .这将<commit_or_branch>之后的所有提交连接到单个输出中,并且>将该输出从 stdout 重定向到名称为<patchfile>的补丁文件中。 This patch file can now be easily applied via git am <patchfile> .现在可以通过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?上面的解决方案与 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 .不同之处在于 Marcin 的解决方案不会生成可以被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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM