2

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.

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61

2 Answers2

3

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.

tomerpacific
  • 4,704
  • 13
  • 34
  • 52
3

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.

Jijo John
  • 1,368
  • 2
  • 17
  • 31