Create-react-app: Suggestion: Automatically create a new Git repository and add initial files

Created on 11 Dec 2016  路  22Comments  路  Source: facebook/create-react-app

One of the nice small things with the Angular CLI is that it automatically creates a new Git repository and adds the initial files to it as the initial commit after initializing a new project. It is not a major thing but it is something I find myself doing pretty much every time I create a new project, even if it is just for test purposes as it makes it easy to see what I changed.

Is this something other people would be interested in seeing me add?

proposal

Most helpful comment

We generally try to avoid flags.

I think there was a discussion about initializing git earlier in https://github.com/facebookincubator/create-react-app/issues/385.

I鈥檓 down to do this by default if we also solve this:

From my personal experience most of the time the React client code is part of a bigger project with server code that is already under version control. Also there are a lot of people using Mercurial.

So if we鈥檙e inside a Git/Hg project we shouldn鈥檛 try to initialize a new repo.

All 22 comments

Maybe it can be a flag so that it can be turned on/off as people prefers? Maybe something like create-react-app hello-world --noGit

We generally try to avoid flags.

I think there was a discussion about initializing git earlier in https://github.com/facebookincubator/create-react-app/issues/385.

I鈥檓 down to do this by default if we also solve this:

From my personal experience most of the time the React client code is part of a bigger project with server code that is already under version control. Also there are a lot of people using Mercurial.

So if we鈥檙e inside a Git/Hg project we shouldn鈥檛 try to initialize a new repo.

That makes sense, the reason for my flag-idea was, just like you said, that you might want to use it in an existing repo.

We should be able to detect this. After all, Git/Hg somehow detect they鈥檙e inside repos 馃槈 .

That .git folder might have something to do with it 馃槈

In the Angular CLI they run git rev-parse --is-inside-work-tree to see if they are already in a Git repo, see here.

If already in a Git repo we could do a git add . and git commit which would be fine if there is nothing added yet. However if there are other changes already added they would get committed as part of the same commit. I would prefer to do nothing in that case and just let the user decide.

Not sure why I didn't see #385. I did search but did not come across that issue. From reading that I would not be in favor of adding a remote. Quite often I am just experimenting and even if I want to push it to a remote I might use BitBucket or some other alternative over GitHub. I would keep it very similar to what the Angular CLI does: Create a new repo if not already in one and add the generated code as the initial commit.

From the last comment by @mareksuscak I am unsure how much work he already did do. Don't want to start two PR's that compete with each other.

I would keep it very similar to what the Angular CLI does: Create a new repo if not already in one and add the generated code as the initial commit.

馃憤

From the last comment by @mareksuscak I am unsure how much work he already did do. Don't want to start two PR's that compete with each other.

@mareksuscak can you give us some context?

I haven't done anything beyond examining how ember-cli does the same thing. I made a skeleton but I think I removed the branch afterward so go ahead if you're in a hurry.

I think it's important to have an opt-out for this feature, because I wouldn't want the CLI making the first commit.
I feel like part of my regular workflow would become create-react-app app1 && cd app1/ && rm -rf .git/ then init the repo myself.

@Timer Curious why? You could always amend it..

@gaearon main reason is that I want very specific files to be present in the first commit; namely simply a LICENSE file and possibly a bare README.

Not to mention I would like myself to be the root author; not the CLI.
In this situation -- removing .git and starting from scratch seems easier than amending the first commit to my liking.

It seems like we're introducing much more complexity versus someone just typing git init and doing their own thing.

In this situation -- removing .git and starting from scratch seems easier than amending the first commit to my liking.

Isn't it as easy as passing --amend to your first commit? Not trolling, genuinely curious.

I would like myself to be the root author; not the CLI.

That鈥檚 how I鈥檇 imagine it working.

@gaearon I often utilize many different emails depending on the repository, so I'd be amending with additional flags such as --author "".

It's not super easy to remove a file from the first commit, because you can't (as far as I know) rewind the commit via standard git reset --[soft/mixed] HEAD^.

The process is more convoluted, requiring the user to run git rebase --root -i and selecting to edit the root commit, then running git reset [files] for the files they want to remove followed by git commit --amend and git rebase --continue.


I would love for someone to improve my knowledge of git, but I dunno how to do this without the latter.

That鈥檚 how I鈥檇 imagine it working.

This was more of an assumption from the existing CLIs, which seem to commit it as a user/email of the CLI (e.g. create-react-app and [email protected]) and not your global user.name and user.email.

Notice the committer (I realize now you said "... sans the Tomster ..."):

@mareksuscak, Not in a hurry or so but I will go ahead and start this and create a PR.

@Timer and @gaearon, Both the Ember and Angular CLI create a commit with the CLI as the author and the current user as the committer. I would argue that is correct as the user didn't write a single line of code yet, the CLI did that. But personally I don't care very much and would be happy with just the commit message stating that it was an initial commit from Create-React-App.

@Timer, As far as I can tell you can remove files from the first commit without any problem. Just did a quick test after an ng new test-app. Added a file, deleted one and changed a third and did a git commit --amend and the result was exactly as I expected. The changes, including the delete, where part of the initial commit and the commit headers where unchanged.

Yes @mauricedb, but what if I want to remove a file from the initial commit while keeping it in my local directory?

e.g. CLI commits LICENSE, README.md and index.js.
I want to make the first commit contain only LICENSE and README.md, and the second commit index.js. But I don't want to go through the extra steps of doing cp index.js index.js.bak, git rm index.js, git commit --amend, mv index.js.bak index.js, git add index.js, git commit -m "Add index.js". Now multiply this process by 10 files w/ different extensions.

In that scenario I'd just do rm -rf .git/, git init, git add LICENSE README.md, git commit -m "Initial commit".

@Timer, Right I misunderstood your goal. In that case I believe you are right and there is no simple way with git. I think the scenario you describe could be the easiest option. But it feels weird to delete a git repo so you can create one.

I feel like people caring that much about what goes into initial commit is going to be a tiny minority of our users. But maybe I鈥檓 wrong.

@gaearon, That is my gut feeling as well but have no data to back it up.

There is a relatively simple workaround with the steps @Timer suggests. Personally I would not make this configurable through a command line option unless there was more demand for it. And adding an opt-out option should be easy in a future PR.

I'm sure there will be very few complaints about it and I'm not trying to talk anyone out of implementing this.

Please understand most of my comments were not an argument but simply an elaboration when asked. Anyway, I think this would be a good default and me removing .git/ or passing a flag is probably similar in keystrokes so I'm indifferent about an option being added.

@gaearon

I have the changes in a branch that should work but I am running into issues testing if it really works the way it is supposed to. I wanted to test using npm run create-react-app ../test1 but can't do so on my laptop because I am running Windows 10 and that calls a Bash script.

So I tried running it in a Linux Docker container but not having much luck there either. Using the following commands:

docker run -it -v c:/Repos/create-react-app:/repos/create-react-app node bash
cd /repos/create-react-app
npm run create-react-app ../test1

I see the following error:

sh: 1: tasks/cra.sh: not found

Even though the file is there.

My branch is here: https://github.com/mauricedb/create-react-app/tree/git-init

Local workspace file ('angular.json') could not be found.
Error: Local workspace file ('angular.json') could not be found.
at WorkspaceLoader._getProjectWorkspaceFilePath (C:\Users\SS\AppData\Roaming\npm\node_modules\@angular\cli\models\workspace-loader.js:37:19)
at WorkspaceLoader.loadWorkspace (C:\Users\SS\AppData\Roaming\npm\node_modules\@angular\cli\models\workspace-loader.js:24:21)
at ServeCommand._loadWorkspaceAndArchitect (C:\Users\SS\AppData\Roaming\npm\node_modules\@angular\cli\models\architect-command.js:180:32)
at ServeCommand. (C:\Users\SS\AppData\Roaming\npm\node_modules\@angular\cli\models\architect-command.js:47:25)
at Generator.next ()
at C:\Users\SS\AppData\Roaming\npm\node_modules\@angular\cli\models\architect-command.js:7:71
at new Promise ()
at __awaiter (C:\Users\SS\AppData\Roaming\npm\node_modules\@angular\cli\models\architect-command.js:3:12)
at ServeCommand.initialize (C:\Users\SS\AppData\Roaming\npm\node_modules\@angular\cli\models\architect-command.js:46:16)
at Object. (C:\Users\SS\AppData\Roaming\npm\node_modules\@angular\cli\models\command-runner.js:87:23)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ap13p picture ap13p  路  3Comments

fson picture fson  路  3Comments

Evan-GK picture Evan-GK  路  3Comments

alleroux picture alleroux  路  3Comments

Aranir picture Aranir  路  3Comments