Home > Mobile >  How can I make the .git folder not disappear in the build folder after npm run build?
How can I make the .git folder not disappear in the build folder after npm run build?

Time:02-02

I have a React app and I want to make the build folder its own Git repository, but every time I run npm run build, the .git folder inside the regenerated /build directory disappears. How can I resolve this?

CodePudding user response:

I think you can use postbuild command in script in your package.json, which will run immediately after build. It will run automaticaly.

Example:

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "postbuild": "copy .git .\\build\\.git"
},

CodePudding user response:

In your package.json, change the build script to this:

"scripts": {
    [...]
    "build": "rm -rf build_old/.git && cp -R build/.git build_old/.git && react-scripts build && cp -R build_old/.git build/.git"
}

It does the following:

  1. Deletes the build_old/.git folder (make sure to create the build_old folder first).
  2. Copies your original .git folder to the build_old directory.
  3. Runs react-scripts build.
  4. Pastes the .git directory back.
  • Related