how can I create a gitignore file in a repository that already have a project in it? I started a project at the university and I forgot to add the gitignore file at the beginning.
-
Just create the file? – tkausl Apr 09 '21 at 19:59
-
2I'm guessing your problem is greater than adding the .gitignore, but rather cleaning all of the things that you should have been ignoring from the start? – Christian Gibbons Apr 09 '21 at 20:00
-
6`touch .gitignore; git add .gitignore; git commit -a -m"Add .gitignore file"`. Alternatively edit before commiting. Push after everything is done. – Eugene Sh. Apr 09 '21 at 20:01
-
Did you already commit some files that should've been gitignored? – HolyBlackCat Apr 09 '21 at 20:13
2 Answers
you can simply go inside the folder, for instance using the terminal (Mac) or Prompt (Windows), then create a .gitignore with the command:
touch .gitignore
or you can follow this instructions in the link.

- 4,704
- 13
- 34
- 52
In order to place the .gitignore file to the existing git repository, follow the steps below.
Note: Make sure to Commit any local changes and push to remote before placing the .gitignore file, otherwise, you will lose control of all the changed files while following further steps.
Start from a clean codebase.
git reset --hard HEAD # force reset local codebase to HEAD revision, removes any local changes.
git clean -fdx # remove any untracked/new files
Prepare your .gitignore file (See template repo) and place it in the root directory.
To untrack every files/paths that are now in your .gitignore file
git rm -r --cached . # This removes any changed files from the staging area.
git add .
git commit -m "Adding .gitignore to the repository"
This could be a duplicate question, I couldn't make it duplicate, the option is not available to me.

- 1,368
- 2
- 17
- 31