Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

gitignore - Git ignore & changing the past

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...