Git分支名称是否会在项目历史中持久保存?

huangapple go评论62阅读模式
英文:

Does a git branch name get persisted in a projects history?

问题

I was working on some code in the master branch and needed to switch focus. I (without thinking properly about it) opened a new branch with a vulgar branch name, and committed my code there. Now i need to use this code.

For professional reasons I don't want this vulgar branch name to appear anywhere where my colleagues and bosses might be able to see.

If I branch off of my vulgar branch to a new branch, will there be anything in the history of my new branch, or the master branch after committing, referencing the vulgar branch name?

My assumption is that branch names aren't used, and only the commit history gets recorded?

英文:

I was working on some code in the master branch and needed to switch focus. I (without thinking properly about it) opened a new branch with a vulgar branch name, and committed my code there. Now i need to use this code.

For professional reasons I don't want this vulgar branch name to appear anywhere where my colleagues and bosses might be able to see.

If I branch off of my vulgar branch to a new branch, will their be anything in the history of my new branch, or the master branch after committing, referencing the vulgar branch name?

My assumption is that branch names aren't used, and only the commit history gets recorded?

答案1

得分: 1

Git不直接将分支名称存储在历史记录中。但是,您应该查找合并提交,因为在那里分支名称是提交消息的一部分。如果您没有将此分支合并,则它不会出现在项目历史记录中的任何地方。在这种情况下,您只需执行以下命令:git checkout -b nice && git branch -D bad-name && git push --delete origin bad-name && git push origin nice

您可以使用git log --all --grep bad-name来检查不良提交。

英文:

Git stores branch names not directly into the history. But you should look for merge commits, since there the branch name is part of the commit message. If you did not had any merges of this branch, it won't appear anywhere in the project history. In such a situation you just git checkout -b nice && git branch -D bad-name && git push --delete origin bad-name && git push origin nice.

You can check for bad commits with git log --all --grep bad-name

答案2

得分: 1

从安全角度来看,您的根本问题不仅仅是那个分支的名称,而是您在一个可能最终与公众分享的存储库中使用了不适合公开的内容。

如果仅仅分支的名称已经是一个如此大的问题,那么很有可能您还在其他地方使用了不希望与公众分享的措辞,比如提交消息、代码注释,甚至是函数或变量名称。

我建议您首先将所有的更改压缩成一个单独的提交,然后从您希望将其合并的任何地方创建一个新的分支,挑选出这个单独的提交,然后在提交任何PR或将其发布到任何地方之前非常小心地仔细检查所有更改,逐行检查。

如果您想要格外小心:

  • 压缩所有更改:
    git rebase -i <last-known-good-commit>
    到处使用 fixup
  • 一旦只有一个提交,导出它:
    git log -p -1 > ~/that-commit.diff
  • 在其他地方克隆上游存储库。
  • 创建集成分支:
    cd <someplace-else>
    git checkout -b today-is-wednesday
  • 应用那个补丁
    git apply ~/that-commit.diff

    patch -p1 < ~/that-commit.diff
  • 喝杯咖啡
  • 非常仔细验证
    git add -i

然后您应该有一个干净的存储库,可以安全地推送。


虽然Git本身不会直接在历史中存储分支名称,但其他工具,如您的编辑器,可能会在您不期望的地方存储它。

因此,我建议将整个存储库视为受到威胁,而不仅仅是希望一切顺利的希望,而是花一些额外时间将补丁复制到一个“干净”的版本中,并仔细审核它们。

如果您的本地分支中有多个提交,并且希望保留它们的历史记录,您可以使用诸如 git format-patchgit am 等工具。

无论如何,请仔细检查,确保不仅是Git历史记录,而且您没有在实际代码中添加任何不希望分享的内容。

英文:

From a security perspective, your fundamental issue isn't really just that particular branch name, but your overall use of .... let's say things not intended for the public ... in a repository that might eventually be shared with the public.

If the mere name of that branch is already such a big issue, chances are that you might have also used ... wording that you might not want to share with the public ... in other places, such as commit messages, code comments, or even function or variable names.

I would suggest you first squash all of your changes into a single commit, then create a new branch off of whatever you want to merge it into, cherry-pick that single commit and then very carefully go over all of the changes, line-by-line before making any PR or publishing it anywhere.

If you want to be extra cautious:

  • Squash all of your changes:
    git rebase -i &lt;last-known-good-commit&gt;
    use fixup everywhere
  • Once you only have one commit, export it:
    git log -p -1 &gt; ~/that-commit.diff
  • Clone upstream repository someplace else.
  • Create integration branch:
    cd &lt;someplace-else&gt;
    git checkout -b today-is-wednesday
  • Apply that patch
    git apply ~/that-commit.diff
    or
    patch -p1 &lt; ~/that-commit.diff
  • Get cup of coffee
  • Verify very carefully
    git add -i

Then you should have a clean repo and can safely push.


While Git itself won't directly store branch names in the history, there is a possibility that other tools such as your editor might store it in places you might not expect.

I would therefore consider the entire repository to be compromised and rather spend that extra time copying patches over to a "clean" version - while carefully reviewing them - instead of just hoping for the best.

In case you have multiple commits in your local branch and would like to preserve their history, you could use something like git format-patch and git am.

In any case, double check that it is not just the Git history, but that you also didn't add anything to the actual code that you don't want to share.

答案3

得分: 0

分支名称是(起始)提交哈希的一个“属性”。

如果你远程删除了分支,历史记录保留了提交,但如预期那样失去了已删除分支的名称。

为了确保它无法通过引用远程恢复,你应该运行远程的 git gc --aggressive。请记住,GitHub 和 GitLab 会定期自动执行此操作。

英文:

The branch name is an "attribute" of a (starting) commit hash.

If you remove the branch remotely, the hystory holds the commits, but loses, as expected, the name of the deleted branch.

To be sure that it cannot be remotely recovered by refs you should run a remote git gc --aggressive. Keep in mind that GitHub and GitLab do it automatically on a timely basis.

答案4

得分: 0

分支名称只是一个引用(ref)。[1]如果您刚刚创建了它并使用了一段时间,那么它很可能没有被共享到任何地方。

所以简短的答案是,除非您已经将任何内容推送到其他地方,否则您不必担心。

本地分支名称

当您创建一个分支名称时,如果启用了 reflog,您将创建该_ref_以及一个 reflog。[2]

例如(在三次提交之后):

4a77267(HEAD -> vulgar)vulgar@{0}:提交:初始化
835ca5e vulgar@{1}:提交:初始化
4d4cd99 vulgar@{2}:提交:初始化
3c45b79(main)vulgar@{3}:分支:从 HEAD 创建

如果您--move此名称,那么 reflog 将提到先前的名称:

4a77267(HEAD -> nice-name)nice-name@{0}:分支:将 refs/heads/vulgar 重命名为 refs/heads/nice-name
4a77267(HEAD -> nice-name)nice-name@{1}:提交:初始化
835ca5e nice-name@{2}:提交:初始化
4d4cd99 nice-name@{3}:提交:初始化
3c45b79(main)nice-name@{4}:分支:从 HEAD 创建

如果您删除分支,那么 reflog 也将被删除:

$ git branch -D vulgar
$ git reflog vulgar
fatal: ambiguous argument 'vulgar': unknown revision or path not in the working tree.

但是HEAD的 reflog 可能仍然提到分支名称:

[…] checkout: moving from vulgar to main

您可能能够使用以下命令删除HEAD的 reflog:

git reflog expire --expire=now

但是之后使用git reflog进行检查

本地分支名称:合并提交消息

请注意默认的合并提交消息:

合并分支 'vulgar'

远程分支名称

只有在您推送分支时才需要担心:

git push origin vulgar

如果是这样,您应该删除该分支:

git push --delete origin vulgar
git remote prune origin

远程可能启用了 reflog。如果是这样(根据我在非裸体远程的测试),那么 vulgar 分支名称的 reflog 将仍然存在,即使git push --delete origin vulgar之后也是如此。

注释

  1. Ref 是指向提交的指针。所以它不仅仅是一个名称
  2. Reflog 仅在本地存在,或者更确切地说,仅在该存储库中存在(不像在 pushfetch 上一样共享)。
英文:

A branch name is just a reference (ref).&#8239;[1] If you just created
it and used it a bit then it is very unlikely that it has been shared
anywhere.

So the short answer is that you don’t have to worry unless you have
pushed anything anywhere.

Branch name locally

When you create a branch name you will create that ref as well as a
reflog&#8239;[2] for it, if the reflog is enabled.

For example (after three commits):

4a77267 (HEAD -&gt; vulgar) vulgar@{0}: commit: Init
835ca5e vulgar@{1}: commit: Init
4d4cd99 vulgar@{2}: commit: Init
3c45b79 (main) vulgar@{3}: branch: Created from HEAD

If you --move this name then the reflog will mention the previous
name:

4a77267 (HEAD -&gt; nice-name) nice-name@{0}: Branch: renamed refs/heads/vulgar to refs/heads/nice-name
4a77267 (HEAD -&gt; nice-name) nice-name@{1}: commit: Init
835ca5e nice-name@{2}: commit: Init
4d4cd99 nice-name@{3}: commit: Init
3c45b79 (main) nice-name@{4}: branch: Created from HEAD

If you delete the branch then the reflog is deleted as well:

$ git branch -D vulgar
$ git reflog vulgar
fatal: ambiguous argument &#39;vulgar&#39;: unknown revision or path not in the working tree.

But the reflog for HEAD might still mention the branch name:

[…] checkout: moving from vulgar to main

You might be able to delete the reflog for HEAD with:

git reflog expire --expire=now

But check with git reflog afterwards

Branch name locally: merge commit messages

Be mindful of default merge commit messages:

Merge branch &#39;vulgar&#39;

Branch name on the remote

You should only be concerned if you have pushed the branch:

git push origin vulgar

If so you should delete the branch:

git push --delete origin vulgar
git remote prune origin

The remote might have reflog enabled. If so (according to my testing in
a non-bare remote) then the reflog for the vulgar branch name will still
be there, even after git push --delete origin vulgar.

Notes

  1. A ref is a pointer to a commit. So there isn’t much more to it than a
    name
  2. The reflog is local-only, or rather specific to that repository (not
    shared anywhere like on push or fetch)

huangapple
  • 本文由 发表于 2023年5月10日 17:07:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216677.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定