This post demonstrates how to use ‘exclude’ and .gitignore files to exclude files from your GitHub repos, including the backup files that PowerShell Studio creates. Both are text files, but a .gitignore file is part of your repo, so it’s versioned and shared. The ‘exclude’ file is private to your clone on disk; it’s not shared or versioned.
Special thanks to @jeffhicks for suggesting an exclude file.
In addition to the files that are critical to your Git repository, you might have a few extra files in the clone of your repo, like temp files, a TODO list, a technical paper that you’re reading on the subject, or tools that you’re using for the project.
If you’re using PowerShell Studio to edit files in your GitHub repos, you might encounter a git status like this one.
C:\GitHub\PesterTDD [master +3 ~0 -1 !]> git status On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: Module.Help.Tests.ps1 Untracked files: (use "git add ..." to include in what will be committed) Module.Help.Tests.RestorePoint.ps1 Module.Help.Tests.TempPoint.ps1 no changes added to commit (use "git add" and/or "git commit -a")
You might have expected a “not staged” message for the script that you’re editing, but you did not expect to see the “untracked files” message. In this case, GitHub found two file types that PowerShell Studio creates to back up your files:
Typically, you don’t include files like these in your Git or GitHub repository, even though they appear in your cloned copy on disk.
Here are a few strategies to ignore or exclude files in your clone, so they don’t appear in the online copy of your repo, and don’t affect the repositories of the source repo or any forks.
An exclude file is a text file that lists the names and name patterns (with wildcard characters and regular expressions) of files that you want Git to ignore. When a file in your repo matches the name pattern, Git does not track or version the file. If you change a file listed in your ‘exclude’ file, git status does not detect the change and git add will not add the file.
Unlike a .gitignore file, an ‘exclude’ file is private. It exists in and affects only the local clone. It is not versioned or shared. Contributors and collaborators cannot see your ‘exclude’ file and it doesn’t affect their forks or clones.
To use an ‘exclude’ file:
For example, your .gitignore file might list all .TempPoint.ps1 and .RestorePoint.ps1 files. You can also hide other files, including files that you keep in your clone for reference and CustomMenu.inf files that create custom tools in PowerShell Studio and PrimalScript.
#In ./git/info/exclude *.TempPoint.* *.RestorePoint.* PesterGlossary.docx CustomMenu.inf |
C:\GitHub\Intro [development +0 ~1 -0]> git status
On branch development
Your branch is up-to-date with 'origin/development'.
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: Get-FileEncoding.ps1
no changes added to commit (use "git add" and/or "git commit -a")
Like an ‘exclude’ file, a .gitignore file is a text file that lists the names and name patterns (with wildcard characters and regular expressions) of files that you want Git to ignore. When a file in your repo matches the name pattern, Git does not track or version the file. If you change a file listed in your .gitignore file, git status does not detect the change and git add will not add the file.
However, the .gitignore file is a part of your repository. It’s versioned and shared. When you add it to your clone, you use ‘git add,’ git commit,’ and ‘git push’ command to add it to your repository. If contributors fork your repo, they get your .gitignore file.
This strategy is excellent when the entire working group should all ignore the same files. Having a standard and shared .gitignore contributes to the uniformity of the process and makes maintenance easier.
To create a .gitignore file
New-Item -ItemType File -Path \.gitignore |
.gitignore. # Dots before and after the "gitignore"
|
File Explorer resolves the file name as .gitignore, as Git requires.
#In .gitignore *.TempPoint.* *.RestorePoint.* |
You can also hide other files, including files that you keep in your clone for reference and CustomMenu.inf files that create custom tools in PowerShell Studio and PrimalScript.
#In .gitignore *.TempPoint.* *.RestorePoint.* PesterGlossary.docx CustomMenu.inf |
C:\GitHub\PesterTDD [master +0 ~2 -0]> git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: .gitignore
modified: Module.Help.Tests.ps1
no changes added to commit (use "git add" and/or "git commit -a")
Unlike an ‘exclude’ file, the .gitignore file is part of the Git repo. When you clone, or fork and clone the repo, you get the .gitignore file, too. For example, there’s a .gitignore file in the PowerShell team PSScriptAnalyzer repo.
C:\GitHub\PesterTDD [master +0 ~2 -0]> git add .gitignore C:\GitHub\PesterTDD [master +0 ~2 -0]> git commit .gitignore -m "Added TempPoint and RestorePoint to gitignore" [master 0d15795] Added TempPoint and RestorePoint to gitignore 1 files changed, 52 insertions(+) C:\GitHub\PesterTDD [master]> git push Counting objects: 7, done. Delta compression using up to 4 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (4/4), 1.07 KiB | 0 bytes/s, done. Total 4 (delta 2), reused 0 (delta 0) To https://github.com/juneb/PesterTDD.git a0b6214..0d15795 master -> master
PowerShell Studio and PrimalScript have some very cool features to support Version Recall, Git, Mercurial, Subversion, TFS, and other version control and source control systems. If you have questions, post them in our product forums for registered customers or our trial software questions forum, if you are not a registered customer.
June Blender is a technology evangelist at SAPIEN Technologies, Inc. You can reach her at This email address is being protected from spambots. You need JavaScript enabled to view it. or follow her on Twitter at @juneb_get_help.