我不小心将一个大文件添加到特定分支上的git repo中,并且不得不更改该分支的提交历史记录,才能通过以下方式删除该文件:
git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch path_to_file" HEAD
现在,在运行命令的分支上,对于提交的提交,我看到了不同的SHA,我很满意,因为该仓库是相当新的,并且目前仅使用它。但是,当我检查master分支和另一个dev分支时,我看到同一提交的SHA有所不同。当我合并到开发人员或管理员(我还没有做)时,它不会失败吗?如何解决此问题?
执行的一系列git命令:
git checkout -b move_branch master
add files from location1 commit
add files from location2 commit
add files from location3 commit
merge the master branch of an older repo which was added as a remote introducing some files including the big file commit
git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch path_to_file" HEAD
add files from location5 commit
add files from location6 commit
A--B--C master
\
X--Y--Z--A--B--C--D--E move_branch (Z is the commit with big file)
X--Y--I--J--K--L--M--N move_branch (after filter-branch)
将分支合并到master不会失败。它也不会摆脱大文件。实际上,您实际上需要在master分支上运行此命令并强制推动此更改。由于这会重写您存储库的历史记录,因此您需要首先与团队进行沟通和计划。 这是破坏性的动作。
如果您一个人正在使用此存储库:
这是简单的情况。只需对master进行硬重置即可。
git checkout -b fix_commits master
git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch path_to_file" HEAD
git checkout master
git reset --hard fix_commits
# Push master if you need to
git push origin HEAD --force
如果有多个人在这个仓库上工作
您的最佳做法是:
从当前的master分支创建一个新分支,然后将其推送。
git checkout -b master_before_file_purge
git push origin -u HEAD
创建新分支以清除大文件,并重写其历史记录。推送此分支。
git checkout -b purge_large_files master
git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch path_to_file" HEAD
git push origin -u HEAD
与每个队友单独合作,将他们的私有树重新建立到purge_large_files
与团队合作,将基于母版的共享分支重新部署到purge_large_files
(您可能需要在这些分支上使用git push origin HEAD --force
)]] >>
将主控器硬重置为purge_large_files
:
git checkout master
git reset --hard purge_large_files
git push origin HEAD --force
与队友合作,确保他们可以毫无困难地掌握大师。
时间过去后,删除“之前”分支:
git checkout master_before_file_purge
git push origin --delete master_before_file_purge
[您和您的队友在从原点再次拉出这些孤立的提交对象后,需要在本地运行git gc
来清除它们。