Git ignore & changing the past

做~自己de王妃 提交于 2020-01-10 01:40:09

问题


I only recently added a .gitignore file to ignore some specific data files in a subdirectory of mine. I'd really like to go back in time and remove them from previous commits/history as well.

Is this easily accomplished? Where/how should I start?


回答1:


This will be a two-step procedure:

Introduce the .gitignore file

For this to work, the early commits are going to have to have your .gitignore file. So, you need to:

  1. Check out the root commit: git checkout -b temp $(git rev-list HEAD | tail -1)
  2. Add the .gitignore file.
  3. git commit --amend
  4. Rebase all of your existing branches onto "temp". This may or may not work seamlessly, so you might need to fiddle with it if any conflicts spring up.
  5. Check out your working branch and delete the temp branch.

Rewrite history

Now you can automatically remove these files from all of the branches by this command:

git filter-branch --tree-filter 'git clean -f -X' -- --all

This will rewrite all of your branches to remove ignored files. It may take a while depending on the size of your repository.




回答2:


See removing file from every commit section. It is exactly your case.

But be careful, as this feature as every history rewriting one DOES change every commit. So if you have public repository and someone could have based some work on it - it would lead to problems.




回答3:


Look at something like below using filter-branch

git filter-branch --tree-filter 'rm -f passwords.txt' HEAD

http://progit.org/book/ch6-4.html




回答4:


git filter-branch is a possible solution. Make sure you understand the implications before you utilize it, as the changes are permanent. You won't be easily able to merge your filtered branch with the old one.

git filter-branch --index-filter 'git rm --cached --ignore-unmatch filename' HEAD

Swap filename for the specific file you'd like gone.



来源:https://stackoverflow.com/questions/6795342/git-ignore-changing-the-past

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!