I’ve been creating React apps based on the create-react-app and adding them to my Github account. create-react-app generates many files in the project folder and I wanted to add this to a new Github repo. Originally, I “cheated” by renaming folders locally after I cloned my remote repo. But I wanted to find a cleaner way to import existing source code to Github so here’s what I came up with:
Steps
- generated the local files to be imported into Github. In my case, it was running create-react-app to generate a React starter project.
- git init – Git initialize the local folder
- create new repo in Github – I created mine with a README.md as well as a license file. The problem was that create-react-app also createded a README.md file so this would create a conflict.
- git remote add origin https://github.com/user/repo – to set the remote repo URL locally. Alternatively you can use a Git GUI like SourceTree (under Settings -> Remotes).
- rename any local files that are the same as the remote repo. In my case, I had a README.md in both my local and remote repos, which cause a pull error. So I had to rename my local README.md to something else (e.g. README1.md) before doing a git pull. The error I was getting was:
- The following untracked working tree files would be overwritten by merge: README.md Please move or remove them before you merge.
- git pull origin master – pull the change from the remote repo to the local one.
- Delete / update any common files between the remote rand local repositories. In my case, I deleted the pulled README.md locally and replaced it with the auto generated one (that create-react-app generated). I just added a title at the top.
- git add -A (stages all files)
- git commit -m “initial local commit” (commits to local repo)
- git push -u origin master (push to Github)
Further Resources
Import existing source code to GitHub (Stack Overflow)
Git says local branch is behind remote branch, but it’s not (Stack Overflow)