Last updated on December 13, 2017

Getting started with Git and Excel: Set up gitignore

Posted by Björn Stiel - Comments

By default, Git sees every file in your working directory. This has the ugly side effect that sooner or later you end up with a temporary Excel file under version control.

When you open your Excel workbook Book1.xlsb, Excel creates a temporary file called ~$Book1.xlsb. It should be automatically deleted after the workbook is correctly closed.

Though ~$Book1.xlsb is usually hidden, Git treats it like any other file, unless Git is explicitly instructed to ignore it. This is what .gitignore does: In .gitignore you can list path and file name (patterns) that you want Git to ignore.

Create a gitignore file

Create a file named .gitignore in the root folder of your repository’s working directory. Add the following conent to the .gitignore :

~$*

This ensures that Git ignores any file starting with ~$. For a full reference of valid patterns, see https://git-scm.com/docs/gitignore.

Add, commit and push .gitignore like any other file.

git add .gitignore
git commit -m 'Adding gitignore'
git push origin master

Gotchas

If you have already committed a ~$ temporary Excel file, adding ~$* to .gitignore will have no effect. The reason is that files that are already tracked by Git are not affected.

To stop tracking a file that is currently tracked, run

git rm --cached ~$Book1.xlsb

to remove ~$Book1.xlsb from the cache (adjust this for your specific file path).

Other ways to ignore temporary Excel files

An alternative approach is to define a global ignore file. If you define your global gitignore patterns in C:\Users\Bjoern\.gitignore_global, you can tell Git with the following command:

git config --global core.excludesfile C:\Users\Bjoern\.gitignore_global

Every rule which goes into this file applies to every Git repository in your user account. This is especially useful for OS-specific files like .DS_Store on macOS or thumbs.db on Windows.

If you go down this route, remember that every user has to define their global gitignore file as it is not part of the repository.