git-filter-repo 丢失了远程仓库

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

git-filter-repo loses the remotes

问题

# Create a temporary file to store the commit messages
temp_file=$(mktemp)

main_head_hash=$(git rev-parse main)
suffix="⚠️ rebased since!"
# Use git log to retrieve the commit messages and store them in the temporary file
git log --pretty=format:%s $main_head_hash.. | grep '🔔 build version' | grep -v "$suffix" > $temp_file

# Create a file to store the replacements
echo > replacements.txt

# Iterate over the commit messages in the temporary file
while read commit_message; do
  # Print the replacement message to the replacements.txt file
  echo "$commit_message=>>$commit_message $suffix" >> replacements.txt
done < $temp_file

# # ⚠️⚠️ Rewriting history ⚠️⚠️
git filter-repo --replace-message replacements.txt --force

# # Remove the temporary files
rm $temp_file
rm replacements.txt

但我现在开始注意到它导致我的git存储库失去了远程设置。起初我以为是我使用的其他git工具,但现在我怀疑是这个脚本造成的 🧵️

如何避免失去远程设置?

英文:

I previously got this rewrite of history commit messages to work:

#!/bin/bash

# Create a temporary file to store the commit messages
temp_file=$(mktemp)

main_head_hash=$(git rev-parse main)
suffix=&quot;⚠️ rebased since!&quot;
# Use git log to retrieve the commit messages and store them in the temporary file
git log --pretty=format:%s $main_head_hash.. | grep &#39;&#128204; build version&#39; | grep -v &quot;$suffix&quot; &gt; $temp_file

# Create a file to store the replacements
echo &gt; replacements.txt

# Iterate over the commit messages in the temporary file
while read commit_message; do
  # Print the replacement message to the replacements.txt file
  echo &quot;$commit_message==&gt;$commit_message $suffix&quot; &gt;&gt; replacements.txt
done &lt; $temp_file

# # ⚠️⚠️ Rewriting history ⚠️⚠️
git filter-repo --replace-message replacements.txt --force

# # Remove the temporary files
rm $temp_file
rm replacements.txt

but I have no began to notice that it causes my git repo to lose the remotes setup. Af first I thought it was some other git tool I was using, but now I have this script being the suspect 🕵️

How do I avoid losing the remotes?

答案1

得分: 3

移除远程仓库是有意设计的。作者的引述如下:

> filter-repo默认重写整个仓库,因此它创建的历史与原始历史不兼容。在将重写的历史推送回原始位置时,有许多事情可能出错,其他工具没有详细讨论或警告这些事情,这会使许多人遇到问题。移除源远程仓库有助于避免这些错误;这是鼓励人们停下来阅读文档,确保他们做好准备并理解影响(并采取适当的预防措施)之前,执行可能会让他们后悔的操作的一种方式。 (他们仍然可以通过重新设置远程仓库、重新获取等方式推送到远程仓库,因此很容易绕过,但我希望他们至少注意到有些不同)。

注意,很容易重新添加远程仓库:

git remote add <repo-url-here>

或者,您还可以使用--refs--partial选项,在过滤过程中避免移除它。请注意这些选项不仅仅是“不移除远程仓库”,因此您应该决定它们是否对您可接受:

> --partial
>
> 进行部分历史重写,导致新旧历史混合。这意味着对于--replace-refs的默认值为update-no-add,禁用将refs/remotes/origin/重写为refs/heads/禁用移除源远程仓库,禁用移除未导出的refs,禁用过期的reflog,并禁用自动的后过滤gc。此外,它修改--tag-rename和--refname-callback选项,使其不是用新的refnames替换旧的refs,而是创建新的refs并保留旧的refs。请谨慎使用。
>
> --refs <refs+>
>
> 将历史重写限制为指定的refs。意味着--partial。除了--partial的常规注意事项(混合旧和新历史,不会自动重新映射refs/remotes/origin/*到refs/heads/*等)之外,当指定负修订时,这还可能导致删除退化的空合并提交的修剪问题。

英文:

Removing the remote is by design. Quote from the author:

> filter-repo defaults to rewriting the whole repository and as such it creates history incompatible with the original. There are lots of things that can go wrong with pushing a rewritten history back up to the original location, something that other tools did not discuss or warn about in detail and which will trip up many people. Removing the origin remote helps avoid these errors; it is encouragement for folks to stop and read the docs to make sure they are prepared and understand the ramifications (and take appropriate preventative steps) before they do something that may bite them. (They can still push to the remote by just setting it back up, can just re-fetch, etc., so it's easy to circumvent, but I want them to at least notice something is different).

Note it's easy to add the remote back:

git remote add &lt;repo-url-here&gt;

Or, you can also use the --refs or --partial options to avoid removing it during the filter. Note these options do more than just "not removing the remote", so you should decide whether they are acceptable to you:

> --partial
>
> Do a partial history rewrite, resulting in the mixture of old and new history. This implies a default of update-no-add for --replace-refs, disables rewriting refs/remotes/origin/* to refs/heads/, disables removing of the origin remote, disables removing unexported refs, disables expiring the reflog, and disables the automatic post-filter gc. Also, this modifies --tag-rename and --refname-callback options such that instead of replacing old refs with new refnames, it will instead create new refs and keep the old ones around. Use with caution.
>
> --refs <refs+>
>
>Limit history rewriting to the specified refs. Implies --partial. In addition to the normal caveats of --partial (mixing old and new history, no automatic remapping of refs/remotes/origin/
to refs/heads/*, etc.), this also may cause problems for pruning of degenerate empty merge commits when negative revisions are specified.

huangapple
  • 本文由 发表于 2023年3月3日 21:56:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627989.html
匿名

发表评论

匿名网友

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

确定