In working with the apprentices that I work with a common issues is forgetting to add a .gitignore file when you initially setup the repository. This can suck in the long run as many files that you don’t want to track start showing up in your repository and worse yet in merge conflicts with other users. A great example of this is the files within the target/ folder in NetBeans (Java) or for my .NET, Visual Studio folks, the files within the bin/ directory. In either case these are the files that are getting built by the code that we wrote and we only want the code in the repository not the assemblies and jars, right… So how do we fix the mistake once it has already happened?…
To fix this issue you want to start by getting the .gitignore file in the repository pronto. To do this, why create it from scratch… instead here is a link to a github repo with many .gitignore files already created and ready for you copying pleasure.
Find the .gitignore file that you need and add it to the repository but don’t commit just yet.
With the .gitignore file in play start running git rm
to get rid of the unwanted files.
Example: git rm -rf MyProject/bin/
the git rm
is to remove the files from the repository
-r
will recursively traverse the directory for any subfolders
-f
forces the files out
MyProject/bin/
is the directory to remove, this can also be a single file as well.
Now once you run this command you will see the files marked as deleted but they should not now nor ever show back up to be added to the repository with the .gitignore properly in the repository. Thus, it is safe to remove the files and forget they were ever part of the repo.
Special Note: in working in teams, make sure other members of the team pull your changes after deleting such files. If they don’t know to look for the deletions they may accidentally override your changes and all the files back. A good practice here would be to get everyone on the same commit, remove the files and then have everyone pull the new commit without the files and continue working.