Monday, March 7, 2016

Making git respect .gitignore after the fact!

Imagine the situation where you wrote your code and then decided to add it to your git repo. Pretty easy right?

git init
git add .


Before you commit, you want to see what's going to be committed. So you do

git status

Now you see whole bunch of config and target files that have no business being in the repo. Not a problem, you can use .gitignore right? First remove what you added, create .gitignore file and you can re add again only source files.

git rm -r .

create .gitignore with
/target/**
.settings/**
.classpath
.project

and re-add

git add .

Check what's about to be committed... and what?!?!? old files? How can this be? Did I messed up my regex? spelled gitignore wrong or forgot the leading period? Nope, everything seems correct...

After reading gitignore help guide... you need to clear your cache!!! Here is what you do
Instead of running

git rm -r .

Run this
git rm -r --cached .

cached flag is the key difference.

After this command, re-add, verify and finally commit:

git add .
git commit -m "source files only!!!"

No comments:

Post a Comment