-1

This question is from rest of other question because it asks why .gitignore is useful compared to git commands?

I have recently started using git using command line. I created a local repo and created a file test.txt inside this repo. Command git status tells me that test.txt is a untracked file. Command git add test.txt adds my file to be tracked. Then command git commit -m "message" commits my tracked file.

My question is if i can already specify which files should be tracked and commit from commands then what is the use of .gitignore file? In which case should i use the .gitignore file?

Anudocs
  • 686
  • 1
  • 13
  • 54
  • 5
    The files whose names or paths match the patterns listed in `.gitignore` are not displayed as untracked by `git status` and `git add` ignores them. Read the docs: https://git-scm.com/docs/gitignore – axiac Jun 27 '19 at 07:10
  • is it the best practice to add and commit .gitignore file and push into online repo? – Anudocs Jun 27 '19 at 07:12
  • 1
    `git add test.txt folder/text2.txt` works well only until you have a few files to be specifically added, imagine adding a lot more files. Usually we need to ignore very few files as compared to the number of files we need to add. `.gitignore` is very convenient to manage such stuff – Mehravish Temkar Jun 27 '19 at 07:14
  • Taking a python project for example, when you run python scripts for tests, *.pyc files are created. These files are not expected to be committed because there would be endless conflicts. Add `*.pyc` to `.gitignore` and then you won't add them by mistake. – ElpieKay Jun 27 '19 at 07:14
  • 1
    You want to ignore, for example, the configuration files because they usually contain users and passwords, file paths or urls specific to an environment. You also want to ignore the generated files. And you want the entire team to use the same ignore rules. The way to achieve this goal is by adding `.gitignore` to the repository. – axiac Jun 27 '19 at 07:15

1 Answers1

1

If you create a file in your repository named .gitignore, Git uses it to determine which files and directories to ignore, before you make a commit.

A .gitignore file should be committed into your repository, in order to share the ignore rules with any other users that clone the repository.

Pavan Nagadiya
  • 652
  • 4
  • 10