Import existing source code into GitHub

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

  1. generated the local files to be imported into Github. In my case, it was running create-react-app to generate a React starter project.
  2. git init – Git initialize the local folder
  3. 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.
  4. 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).
  5. 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.
  6. git pull origin master – pull the change from the remote repo to the local one.
  7. 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.
  8. git add -A (stages all files)
  9. git commit -m “initial local commit” (commits to local repo)
  10. 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)