Overview
Earlier today, I added a great feature to my builds whereby I create a new BuildVersion.json file containing the latest commit and the date and time of the build. I also provide a nice and simple API over the top so that I can determine the exact version by going to https://sitename/api/version – it works well.
Each time I build locally, it generates the BuildVersion.json file and, of course, the file is changed resulting in it needing to be committed into Git. So, that is an easy fix I thought, I will just add it to the Git Ignore file.
However, this wasn’t enough. Each time the file is changed, it still appeared in my changes and needed to be ignored manually.
The fix
The problem was that I had committed the file before I changed the .gitignore file.
So, I had to run the following lines to remove everything from Git and put it all back again, this time honouring the updated .gitignore file: –
git rm -rf --cached .
git add .
The official documentation for the above commands can be found here: – https://git-scm.com/docs
Also, so I have a reminder regarding how I changed the .gitignore file, it is here: –
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore
# User-specific files
*.rsuser
*.suo
*.user
*.userosscache
*.sln.docstates
# My custom settings here
BuildVersion.json
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Mono auto generated files
mono_crash.*
Conclusion
The moral of the story is either add to your .gitignore file BEFORE you commit; or run the commands above to sort things out.