简体   繁体   中英

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:

  • Commands that only work on 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.

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!). 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). So I'm looking for a way to apply the .gitignore file to all of my history. The commands should work on Windows as I have no intention of installing Linux for this.

No need to add the .gitignore in the history (there is no added value to do it), just add it for your future commits.

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).

It will do the job for you!

This is working for me:

  • Install hg-git.
  • cd HgFolder
  • hg bookmark -r default master
  • mkdir ../GitFolder
  • cd ../GitFolder
  • git init --bare
  • cd ../HgFolder
  • hg push ../GitFolder
  • Move all files from GitFolder to a '.git' folder (in this GitFolder) and set this folder to hidden (not the subfolders and files).
  • cd ../GitFolder
  • git init
  • git remote add origin https://url.git
  • Copy all current content (including .gitignore) to GitFolder.
  • git add .
  • git commit -m "Added existing content and .gitignore".
  • git filter-branch --index-filter "git rm --cache d -r --ignore-unmatch 'LINES' 'FROM' 'GITIGNORE'" --prune-empty --tag-name-filter cat -- --all
  • git rm -r --cached .
  • git add .
  • git gc --prune=now --aggressive
  • 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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