我不明白为什么.git在终端中执行 create-react-app 时会自动创建文件夹。
该.gitignore目录不存在,但一个.git文件夹。
有谁知道为什么?这是文件夹的图片
create-react-app是一个为您创建反应骨架的应用程序。
CLI 命令还将文件夹设置为 git 文件夹
这git init是此脚本的一部分
https://github.com/facebook/create-react-app/blob/47e9e2c7a07bfe60b52011cf71de5ca33bdeb6e3/packages/react-scripts/scripts/init.js
代码的相关部分是这样的:
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;
Run Code Online (Sandbox Code Playgroud)
}
正如您所看到的,CLI 命令创建 git 存储库,添加并提交生成的文件作为初始提交。
这就是您拥有.git文件夹的原因。
我的猜测是,因为 create-react-app 基于 facebook 的 GitHub 存储库,并且在他们的工作流程中,他们使用 git 来管理/提交你的工作,当你创建新项目时,他们将其作为默认值包含在内。
您始终可以在与 .git 文件夹相同的级别手动添加 .gitignore (它不需要位于 .git 文件夹内)。我更喜欢使用 vim 来做到这一点......
vim .gitignore
Run Code Online (Sandbox Code Playgroud)
然后您可以按i开始编辑文件并写入您想要忽略的任何文件夹(即node_modules、build)。
然后按 Esc 和:wq!保存文件。
如果您想将更改提交到远程存储库(例如 git hub),您将使用以下命令,并更改 URL 以指向您的存储库。
# 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
Run Code Online (Sandbox Code Playgroud)
在使用 master 以外的分支时,还有一些 git 命令需要考虑,但这应该足以让您开始。
希望这能回答您的问题,
马特