我正在跟踪this answer,以从git历史记录中删除包含凭据的单个文件。我有git 2.35.1和过滤器22826b5a68b6。我需要的命令显然是:
git-filter-repo --path auth.json --invert-paths
如果我试图将此应用于我的工作回购,我会得到以下错误:
Aborting: Refusing to destructively overwrite repo history since
this does not look like a fresh clone.
(expected freshly packed repo)
因此,我使用git clone
签出了一个新副本,该命令成功运行:
Parsed 861 commits
New history written in 0.69 seconds; now repacking/cleaning...
Repacking your repo and cleaning out old unneeded objects
HEAD is now at 7212384 Update app.css
Enumerating objects: 8203, done.
Counting objects: 100% (8203/8203), done.
Delta compression using up to 24 threads
Compressing objects: 100% (2310/2310), done.
Writing objects: 100% (8203/8203), done.
Total 8203 (delta 5630), reused 8196 (delta 5623), pack-reused 0
Completely finished after 2.85 seconds.
我可以看到文件已经被删除了。但当我去推的时候
git push --force
fatal: No configured push destination.
由于某种原因,它丢失了它克隆的遥控器,所以我手动将它添加回:
git remote add origin git@git.example.com:abc/xyz.git
但以下几个方面都失败了:
fatal: The current branch master has no upstream branch.
所以我加上
git push --set-upstream origin master
但这也失败了:
To git.example.com:abc/xyz.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git.example.com:abc/xyz.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
但我知道自从查过后就没有什么东西被推到这个回购上了。重复该过程具有相同的结果。如果我做了一个git pull
来更新它,那么它就会再次因为this does not look like a fresh clone
错误而失败,就在我开始的地方。
我绕了几圈,终于克服了所有的错误,结果发现这对我的回购没有什么影响--文件还在那里。
因此,我的问题是,我应该做哪些具体的步骤来使这个过滤过程在一个新克隆的回购上工作?
发布于 2022-03-23 04:57:28
你离我太近了。
在前面的步骤中,您需要git push --force
的原因是,您将在远程上取消提交,并将它们替换为新的。由于您的遥控器已经没有了,所以跳过第一个强制推命令,然后只需将强制添加到最后的push命令:
git push --set-upstream origin master --force
someone:我几乎总是更喜欢使用--force-with-lease
而不是--force
,因为如果有人在上次获取(或者在本例中是克隆的)和推送之间向远程分支添加了新的提交,那么它就会出错,这一点您还没有看到。把他们吹走可能是不礼貌的。在使用--force-with-lease
时,如果遇到错误,只需执行git fetch
,查看新提交并决定是否可以删除它们。如果是,那么再次使用--force-with-lease
,它将工作(除非在获取之后的最后一分钟再次出现新的提交)。
在这个特殊的情况下,您需要重新添加您的遥控器,您必须先获取,否则--force-with-lease
将无法工作,如果是我,我可能会考虑这样做,如果在您克隆的时候和您将要强制推进重写的回购时,远程上出现了新的提交。在这种情况下,我将将您的最后命令更改为以下步骤:
git fetch
# inspect origin/master to see if new commits appeared after your clone
git push --set-upstream origin master --force-with-lease
或者,在您的情况下,一旦您决定重写一个分支,暂时锁定分支(或移除对它的权限),并在您的强制推送后将其解锁。然后你就知道了,在你完成之前,没有人会添加提交。
发布于 2022-03-23 15:10:33
TTT的回答很有帮助,尤其是关于过滤器的评论--回购做git init
--问题在于操作顺序。在它生效之前,我已经做了很多次了,我把它变成了一个脚本,明确了所需要的内容和顺序:
#!/usr/bin/env bash
set -xv
git clone git@git.example.com:abc/xyz.git project
cd project
git filter-repo --path auth.json --invert-paths
git remote add origin git@git.example.com:abc/xyz.git
git push --set-upstream origin main --force
完成此操作后,我遇到了许多更新现有克隆的问题,但通常情况下,它们都是通过接受来自远程的所有更改来解决的。
https://stackoverflow.com/questions/71577268
复制相似问题