This entry has been copied from the Jonathan Chanon blog.Although his entry was written several years ago, I have recently started to struggle with GitHub and I’ve run into this problem. So below explains how to resolve it.

This happens to me too often and I always end up googling the answer so this post is probably more of a location I know I can come to find the answer, although by writing it down hopefully it may sink in that I should stop getting too excited on a new project.

New project scenario

You’re all very excited about your new project and you think its about time you committed this to source control. Obviously you’re using Git so you initialise a new repository and commit your files. You then setup a remote repository at Github and it asks you whether you want it create a .gitignore file – you do. So now you have a repository remotely and locally. Easiest thing to do is pull from the remote, setup your remote and push to it. The other scenario might be you’ve committed locally and then realise you need to add a .gitignore file which you do and then commit.

In both cases you will now see the files in your standardised .gitignore file are not being ignored. After a few head scratches you realise its because the .gitignore file should be added to your repo first before any commits.

The solution!

Long story short you have to remove all tracked files and add them back in using the below commands