简体   繁体   English

Git忽略并更改历史记录(在Windows上)

[英]Git ignore and changing the history (on Windows)

I've already read several posts about this here (like Git ignore & changing the past , How to remove files that are listed in the .gitignore but still on the repository? , and Applying .gitignore to committed files ), but they have several problems: 我已经在这里阅读了几篇有关此问题的文章(例如Gitignore和更改过去如何删除.gitignore中列出但仍在存储库中的 文件以及如何将.gitignore应用于已提交的文件 ),但是它们有几篇问题:

  • Commands that only work on Linux. 仅在Linux上有效的命令。
  • Incomplete commands (like the first post I've linked to). 命令不完整(例如我链接到的第一篇文章)。
  • Only for one file. 仅用于一个文件。

I have pretty much no experience with Git so I was hoping for some help here. 我几乎没有使用Git的经验,所以我希望在这里有所帮助。

What I'm basically trying to do is rescue one of my projects history. 我基本上想做的是挽救我的项目历史之一。 It's currently Hg and I converted it to Git with Hg-Git (all very easy) and it includes the history (great!). 目前是Hg,我用Hg-Git将其转换为Git(都非常简单),并且包含了历史记录(很棒!)。 However, I also added a .gitignore file and added several new files & folders that I want completely gone from the history (like the bin and obj folders, but also files from ReSharper). 但是,我还添加了一个.gitignore文件,并添加了一些我想完全脱离历史记录的新文件和文件夹(例如binobj文件夹,以及ReSharper中的文件)。 So I'm looking for a way to apply the .gitignore file to all of my history. 因此,我正在寻找一种将.gitignore文件应用于所有历史记录的方法。 The commands should work on Windows as I have no intention of installing Linux for this. 该命令应在Windows上运行,因为我无意为此安装Linux。

No need to add the .gitignore in the history (there is no added value to do it), just add it for your future commits. 无需在历史记录中添加.gitignore(没有附加值即可执行),只需将其添加以用于将来的提交即可。

For the remove of files and directories in your history, use bfg-repo-cleaner which is fast, easy and works very well on Windows (done in scala). 要删除历史记录中的文件和目录,请使用bfg-repo-cleaner ,该工具快速,简便并且在Windows上运行良好(在scala中完成)。

It will do the job for you! 它将为您完成工作!

This is working for me: 这为我工作:

  • Install hg-git. 安装hg-git。
  • cd HgFolder cd HgFolder
  • hg bookmark -r default master hg书签-r默认主
  • mkdir ../GitFolder mkdir ../GitFolder
  • cd ../GitFolder cd ../GitFolder
  • git init --bare git init-裸
  • cd ../HgFolder cd ../HgFolder
  • hg push ../GitFolder 汞推../GitFolder
  • Move all files from GitFolder to a '.git' folder (in this GitFolder) and set this folder to hidden (not the subfolders and files). 将所有文件从GitFolder移到“ .git”文件夹(在此GitFolder中),然后将此文件夹设置为隐藏(而不是子文件夹和文件)。
  • cd ../GitFolder cd ../GitFolder
  • git init git初始化
  • git remote add origin https://url.git git remote add origin https://url.git
  • Copy all current content (including .gitignore) to GitFolder. 将所有当前内容(包括.gitignore)复制到GitFolder。
  • git add . git添加
  • git commit -m "Added existing content and .gitignore". git commit -m“添加了现有内容和.gitignore”。
  • git filter-branch --index-filter "git rm --cache d -r --ignore-unmatch 'LINES' 'FROM' 'GITIGNORE'" --prune-empty --tag-name-filter cat -- --all git filter-branch --index-filter“ git rm --cache d -r --ignore-unmatch'LINES''FROM''GITIGNORE'” --prune-empty --tag-name-filter cat--所有
  • git rm -r --cached . git rm -r --cached。
  • git add . git添加
  • git gc --prune=now --aggressive git gc --prune = now --aggressive
  • git push origin master --force git push origin master --force

There is probably an easier way to do this and it might not be perfect but this had the result I wanted. 可能有一种更简单的方法来执行此操作,它可能并不完美,但这已达到我想要的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM