Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a .git folder automatically being created when creating a react app?

Tags:

git

reactjs

I don't understand why a .git folder is automatically created when I do create-react-app in the terminal.

The .gitignore folder isn't there but a .git folder is.
Does anyone know why? Here's a picture of the folder

enter image description here

like image 359
Nina Avatar asked Feb 27 '19 21:02

Nina


2 Answers

create-react-app is an application which creates a react skeleton for you.
The CLI command also set the folder as git folder

The git init is part of this script https://github.com/facebook/create-react-app/blob/47e9e2c7a07bfe60b52011cf71de5ca33bdeb6e3/packages/react-scripts/scripts/init.js

The relevant part of the code is this:

function tryGitInit(appPath) {
       …
  execSync('git init', { stdio: 'ignore' });
  execSync('git add -A', { stdio: 'ignore' });
  execSync('git commit -m "Initial commit from Create React App"',
           {stdio: 'ignore' });
  return true;
}

And as you can see the CLI command create git repository, add and commit the generated files as the initial commit.

This is why you have the .git folder.

like image 124
CodeWizard Avatar answered Sep 25 '22 08:09

CodeWizard


My guess is that because create-react-app is based on facebooks GitHub repository, and in their workflow, they use git to manage/commit your work they have included it as a default when you create a new project.

You can always add a .gitignore in manually at the same level as the .git folder (it does not need to be inside the .git folder). I prefer using vim to do it so...

vim .gitignore

Then you can press i to start editing the file and write any of the folders you want to ignore (i.e. node_modules, build).

And then press escape and :wq! to save the file.

If you want to commit your changes to a remote repository such as git hub you will be using the following commands, and changing the URL to point to your repo.

# stage all your changes
git add .

# commits them to your local repository with a message
git commit -m "This change has been made"

# add remote url
git remote add origin https://github.com/example-user/example-repo.git


# pushes the changes you have committed to master branch
git push -u

There is some more git commands to consider when working with branches other than master, but this should be enough to get you started.

Hope this answers your question,

Matt

like image 43
Matt Catellier Avatar answered Sep 25 '22 08:09

Matt Catellier