Exclude A File From Git Source Code Control in VSCode

Like many developers, I use VSCode for my projects. Primarily PowerShell, but also other things like the PHP code used for my ham radio club website or markdown projects.

It can be useful to have extra files, that I don’t need or want to have saved in Git. One example, I often have a “scratchpad.ps1” file. It’s a place I can prototype code, test out ideas, before I copy them into my main project. If this file gets lost, or damaged, I don’t really care as it’s just temporary.

Another example stems from my need to demonstrate code on video, for my Pluralsight courses, or live at SQL Saturdays and code camps. I often need to login to a website or database, and don’t want to have my credentials hard coded in my script for everyone to see.

To solve this, I simply place my needed information in a text file, then use Get-Content (with the -Raw switch) to read it into a variable. I don’t want this text file though to be placed in my public github repositories.

Excluding a file is simple. In the root folder of your source controlled project is a folder named .git. (Note the period on the front, also note that on some operating systems it may be hidden by default.) Under it is another folder called info.

In .git/info is a file called exclude (with no extension). To it, simply add the names of the files you want to exclude from source control. Here’s an example:

# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
uid.txt

The lines that begin with a pound sign (#) are comments, and are included by git.

Under it is the file I added, uid.txt. This file is will now be excluded from any git commits, and will not be uploaded to github or whatever tool you use for git. You can see this in the VSCode file tree, the file will appear in an off color font. In a dark mode theme, this will be a gray.

The arrow points to the uid.txt file that was excluded from the project.

That’s all there is to it. Now you can include extra files, such as scratch pads, notes, or even passwords that you don’t need (or want) to have as part of your git repository. Just keep in mind it’s your responsibility to back these files up.

Suppress Write-Verbose When Calling A function in PowerShell

In a project for one of my recent Pluralsight courses, “Everyday PowerShell for Developers on Linux, macOS, and Windows“, I had created some functions in my DataFabricator module. Some of these called many child functions.

I wanted to be able to call the parent function using Verbose in order to track progress. As you may be aware though, when you call a parent function using Verbose, it carries down to all of the functions it calls. This caused a large volume of verbose messages making it difficult to find the information I wanted. Hence I needed to suppress verbose in the called functions.

Let’s see how. First let’s create two simple functions that don’t suppress verbose messages.

function Parent1()
{
  [CmdletBinding()]
  param()

  Write-Verbose "Verbose: Parent1"
  Write-Host "Parent1"
  Child1
}

function Child1()
{
  [CmdletBinding()]
  param()

  Write-Verbose "Verbose: Child1"
  Write-Host "Child1"
}

When we call the parent function with the verbose switch, we get the expected output.

Parent1 -Verbose

VERBOSE: Verbose: Parent1
Parent1
VERBOSE: Verbose: Child1
Child1

Now let’s create an alternate version of the parent function. To suppress verbose on a called child function, all that is needed is to add the verbose switch, but followed by a colon and a $false.

function Parent2()
{
  [CmdletBinding()]
  param()

  Write-Verbose "Verbose: Parent2"
  Write-Host "Parent2"
  Child1 -Verbose:$false
}

Here is the output when we call the new Parent2.

Parent2 -Verbose

VERBOSE: Verbose: Parent2
Parent2
Child1

As you can see, the verbose statements in the Child1 function do not trigger. All that was needed was to add “-Verbose:$false” as a switch when calling Child2.

And that’s all there is to it, using this simple technique you can suppress verbose messages when calling a child function, whether verbose was used to call the parent or not.