Honouring .gitignore settings

Honouring .gitignore settings

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.

Stephen

Hi, my name is Stephen Finchett. I have been a software engineer for over 30 years and worked on complex, business critical, multi-user systems for all of my career. For the last 15 years, I have been concentrating on web based solutions using the Microsoft Stack including ASP.Net, C#, TypeScript, SQL Server and running everything at scale within Kubernetes.

Finchett.com
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.