Ignore files and folders - gitignore

The .gitignore file defines the files and folders pattern that git will not track.

Please note in a particular for credentials:

Previously tracked files are not taken into account retrospectively.

Accidentally previously tracked and committed files must be removed from the repo; they can nevertheless be found in the history.

Tracked credentials should be changed immediately or removed from git index (Remove already tracked files).

Create .gitignore file

Create the .gitignore file inside the repositories root folder. E.g.:

.
└── thisRepository
    ├── .git
    │   └── ...
    ├── folder1
    │   └── ...
    ├── .gitignore   <---
    ├── file1
    └── README.adoc
Ignore a file
/text.txt (1)
/config/secret.yml (2)
/app/*.log (3)
!/app/template.log (4)
/app/snapshot* (5)
1 ignores a single file inside the root folder
2 ignores a single file inside the subfolder /config
3 ignores all files ending with .log inside a specific subfolder /app
4 with exclamation mark: do not ignore this specific file template.log, although all other files with the same extension are ignored before (not working if the entire folder is ignored)
5 ignores all files with a name starting with snapshot inside a specific folder
Ignore a folder
config/ (1)
default (2)
1 ignores the entire folder /config
2 ignores both a file as well as a folder named default

Content / files and folders to hide typically:

  • credentials

  • environmental variables

  • IDE configuration folders

  • OS files

  • package manager files

  • runtime files

  • …​

Examples

Typical application

Ignore credentials and Python IDE configs
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# vscode
.vscode

# pycharm project files
*.idea/

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# Spyder project settings
.spyderproject
.spyproject
Ignore credentials and CakePHP IDE configs
# Environments
.env


# vscode
.vscode

# CakePHP 3

/vendor/*
/config/app.php

/tmp/cache/models/*
!/tmp/cache/models/empty
/tmp/cache/persistent/*
!/tmp/cache/persistent/empty
/tmp/cache/views/*
!/tmp/cache/views/empty
/tmp/sessions/*
!/tmp/sessions/empty
/tmp/tests/*
!/tmp/tests/empty

/logs/*
!/logs/empty

# CakePHP 2

/app/tmp/*
/app/Config/core.php
/app/Config/database.php
/vendors/*

Remove already tracked files

Tell git to remove the file from the index afterwards

First step: add the file to ignore to .gitignore

Example - .gitignore
.env

Second:

Remove the .env from the index
git rm --cached .env

The file remains on the local machine as an ignored file.

Finally:

add to staging, commit and push to gitlab
git add .gitignore
git commit -m 'Update gitignore'
git push

An now remove commits of this file from history

All files corresponding to the commits to be deleted must first be removed as described above.
ALSO THE CONTENT OF THE FILES AND THE FILES WERE DELETED !
MAKE A COPY OF THE REPOSITORY BEFORE NEXT STEPS !
Interactive rebase window. Delete the lines corresponding to the commits to delete and save the file via Ctrl X
#pick fd7ba6d Add netwrok public
pick 4e0d3fc Add file
pick d6d2ae5 Update
#pick 83d71cb Add env var file and variables
pick 3bf9212 Add file
pick c577a31 Change env vars
pick ceec2a1 Update
pick 99fe52a Update env file
#pick 54909fb Rename file
pick 27bca22 Update
#pick 25c8b2c Remove tracked env file # empty
pick c62f9c6 Update

# Rebase 1ed4686..c62f9c6 onto 1ed4686 (15 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
#                    commit's log message, unless -C is used, in which case
#                    keep only this commit's message; -c is same as -C but
#                    opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
#         create a merge commit using the original merge commit's
#         message (or the oneline, if no original merge commit was
#         specified); use -c <commit> to reword the commit message
# u, update-ref <ref> = track a placeholder for the <ref> to be updated
#                       to this position in the new commits. The <ref> is
#                       updated at the end of the rebase
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#


^G Help          ^O Write Out     ^W Where Is      ^K Cut           ^T Execute       ^C Location      M-U Undo         M-A Set Mark     M-] To Bracket   M-Q Previous     ^B Back
^X Exit          ^R Read File     ^\ Replace       ^U Paste         ^J Justify       ^/ Go To Line    M-E Redo         M-6 Copy         ^Q Where Was     M-W Next         ^F Forward
Now push that changes
git push origin +main

And check the history :-)

Unprotect protected branches in order to allow force pushes

Step 1

gitlab protected brances 0

Step 2

gitlab protected brances 1

PROTECT THE BRANCH AGAIN