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.

4
  • Just create the file? Commented Apr 9, 2021 at 19:59
  • 2
    I'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? Commented Apr 9, 2021 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. Commented Apr 9, 2021 at 20:01
  • Did you already commit some files that should've been gitignored? Commented Apr 9, 2021 at 20:13

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

I'll try it, thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.