Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive
 

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.


Why ignore files?

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:

  • .TempPoint.ps1 file: Enables the Rewind feature.
    Saves the state of the file when it was first opened in this session of PowerShell Studio. To return to this state at any time during this PowerShell Studio session, even if you have saved, or closed and reopened the file, click Source Control and in the Restore Points section, click Rewind.PowerShell Studio deletes all TempPoint files when it closes, but you might find them in directories if PowerShell Studio is still open or it closes irregularly.

     

  • .RestorePoint.ps1 file: Enables the Restore point feature.
    Saves the state of the file when you click Create Restore Point (Source Control\Restore Point\Create). To return to this state at any time, click Source Control and in the Restore Points section, click Restore.For more information about the Restore Point features of PowerShell Studio, see the Rewind in PowerShell Studio: A built-in safety net for your scripts video on YouTube.

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.

 

Use a private ‘exclude’ file

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:

  1. Open the exclude file in any text editor. Git creates an ‘exclude’ file in a hidden directory of every clone. The path is .\git\info\exclude.

     

  2. Add the file names or name patterns that you want Git to ignore. Wildcards are supported.

    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
  3. When you run git status, Git identifies .ps1 files that changed, but it does not find any files in the ‘exclude’ file, and it doesn’t find the exclude file.
    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")

 

Use a shared ‘.gitignore’ file

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

    1. In your Git clone directory on disk, if it doesn’t already exist, create a .gitignore text file. In Windows, it’s a bit tricky to create a file with a file name extension and no name. To create this file:
      • In Windows PowerShell:
        New-Item -ItemType File -Path \.gitignore
      • In File Explorer, in the clone directory, right-click, click New, Text document. Name the new document:
        .gitignore.    # Dots before and after the "gitignore"

        File Explorer resolves the file name as .gitignore, as Git requires.

    2. In the .gitignore file, add the file names or name patterns that you want Git to ignore. Wildcards are supported.For example, your .gitignore file might list all .TempPoint.ps1 and .RestorePoint.ps1 files.
      #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
    3. When you run git status, Git finds the .gitignore file and the .ps1 file, but not the restore point files.
      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")
      
    4. Add, commit, and push the .gitignore file and include it in your pull requests.

      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.

If you have questions about our products, please post in our support forum.
For licensed customers, use the forum associated with your product in our Product Support Forums for Registered Customers.
For users of trial versions, please post in our Former and Future Customers - Questions forum.
Copyright © 2024 SAPIEN Technologies, Inc.