Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git ignore & changing the past

Tags:

git

gitignore

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?

like image 640
anonymous coward Avatar asked Jul 22 '11 19:07

anonymous coward


People also ask

What is git ignore for?

. gitignore tells git which files (or patterns) it should ignore. It's usually used to avoid committing transient files from your working directory that aren't useful to other collaborators, such as compilation products, temporary files IDEs create, etc.

How do I ignore a file in git?

Use your favorite text editor to open the file called . git/info/exclude within the root of your Git repository. Any rule you add here will not be checked in, and will only ignore files for your local repository.

Is there a git ignore command?

There is no explicit git ignore command; instead, the . gitignore file must be edited and committed by hand when you have new files that you wish to ignore. The . gitignore files hold patterns that are matched against file names in your repository to determine whether or not they should be ignored.

How do I ignore a folder in git?

If you want to maintain a folder and not the files inside it, just put a ". gitignore" file in the folder with "*" as the content. This file will make Git ignore all content from the repository.


1 Answers

I've just done this and some of the other answers/comments nearly got me there, but I had to make some changes.

I've used filter-branch to introduce my .gitignore file at the start of history, and apply it to all previous commits.

This is the command:

git filter-branch --tree-filter 'git rm --cached -rf . >/dev/null ; cp ~/Desktop/new-gitignore .gitignore ; git add . ; git clean -d -X -f >/dev/null' -- --all

As you can see, it assumes you have a copy of the wanted .gitignore file on your Desktop (~/Desktop/new-gitignore).

I found that only after I've removed and re-added (git rm ..., git add .) all the files would git clean remove all the files that the new .gitignore is telling it to ignore. Notice its not necessary to add the files again after clean, as tree-filter will just commit everything as is.

I've included >/dev/null to reduce the output of the script, but you can safely remove this if you want to see the whole process.

like image 60
mcfedr Avatar answered Oct 01 '22 06:10

mcfedr