重建 git 历史记录的脚本,对每个版本应用代码清理

发布于 2024-08-26 09:27:49 字数 127 浏览 8 评论 0原文

有没有人有一个 git 脚本,可以浏览历史记录,检查每个版本,应用清理脚本,然后将清理后的版本签入另一个存储库?

我有一些正在开发的代码,但我与代码格式不一致,例如制表符与空格等。我想重写我的整个历史记录以与新标准保持一致。

Has anyone got a script for git that can go through the history, check out each version, apply a cleanup script, then check the cleaned version into another repository?

I have some code which I've been developing, but I haven't been consistent with code formatting e.g. tabs vs spaces etc. I'd like to rewrite my entire history to be consistent with the new standards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨后彩虹 2024-09-02 09:27:49

git filter-branch 命令可以满足您的需要。

例如:

# Make a backup!
cp -r <repo> <repo>.backup
cd <repo>
# Replace tabs with two spaces in all .cpp and .h files in all branches.
git filter-branch --tree-filter \
  "find \( -iname '*.cpp' -o -iname '*.h' \) \
   -exec sed -i -re 's/\t/  /g' {} \;" -- --all
# Delete branch backups created by 'git filter-branch'.
# From the end of `man git filter-branch`; more cleanup
# suggestions there.
git for-each-ref --format="%(refname)" refs/original/ | \
  xargs -n 1 git update-ref -d
# You still have ../<repo>.backup, in case something went wrong.

但是要小心...这会改变 git 存储库。
如果有人有一个克隆......它将不再连接到您的新存储库。
来自 man git filter-branch :

警告!重写的历史记录将具有不同的对象名称
所有对象都不会与原始分支汇聚。你
将无法轻松推送和分发重写的分支
在原始分支的顶部。如果出现以下情况,请不要使用此命令
你不知道完整的含义,并且无论如何都要避免使用它,
如果一个简单的提交足以解决您的问题。 (看
git-rebase(1) 中的“从上游 REBASE 恢复”部分
有关重写已发表历史的更多信息。)

The git filter-branch command does what you need.

For example:

# Make a backup!
cp -r <repo> <repo>.backup
cd <repo>
# Replace tabs with two spaces in all .cpp and .h files in all branches.
git filter-branch --tree-filter \
  "find \( -iname '*.cpp' -o -iname '*.h' \) \
   -exec sed -i -re 's/\t/  /g' {} \;" -- --all
# Delete branch backups created by 'git filter-branch'.
# From the end of `man git filter-branch`; more cleanup
# suggestions there.
git for-each-ref --format="%(refname)" refs/original/ | \
  xargs -n 1 git update-ref -d
# You still have ../<repo>.backup, in case something went wrong.

But be careful... this transforms the git repository.
If somebody has a clone... it will not be connected to your new repo anymore.
From man git filter-branch:

WARNING! The rewritten history will have different object names for
all the objects and will not converge with the original branch. You
will not be able to easily push and distribute the rewritten branch
on top of the original branch. Please do not use this command if
you do not know the full implications, and avoid using it anyway,
if a simple single commit would suffice to fix your problem. (See
the "RECOVERING FROM UPSTREAM REBASE" section in git-rebase(1) for
further information about rewriting published history.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文