CH 42 / 75Part 6: Connecting to GitHub

Putting an Existing Project on GitHub

What you will learn on this page

  • Turn a folder that doesn't use Git into a repository and put it on GitHub
  • Check for secrets and large files before your first commit
  • Use git add -n to preview which files will be recorded
  • Understand GitHub's file size limits
On this page9 items

Part 6 went one step at a time from the idea of remotes (36) through authentication, creating a repository, push, pull and upstream (41). This page pulls it all together. You’ll take a folder that was never managed with Git and put it on GitHub from scratch. These are the steps for when you start managing something you already have locally, such as work documents or a half-built website.

The overall flow#

Six steps to put an existing folder on GitHub. 1. Check the contents, 2. Write .gitignore, 3. git init, 4. Check with add -n, then add and commit, 5. Create an empty repository on GitHub, 6. Send with remote add and push -u
Steps 1 and 2 can only be done before recording

Steps 3 and 4 are from Part 2, and steps 5 and 6 are from Part 6. What’s new is that you must always do steps 1 and 2 before the first commit. Anything that goes into the first commit stays in the history and is hard to remove later (Part 2, #12).

For practice, prepare a folder of kitchen notes. Mixed in are a secret settings file (.env) and a file macOS creates automatically (.DS_Store).

text6 lines
mkdir -p ~/kitchen-notes/photos
cd ~/kitchen-notes
echo "# Kitchen Notes" > README.md
echo "Weekend meal prep: curry, marinated eggs" > meal-prep.md
echo "API_KEY=abc123" > .env
touch .DS_Store

Assume the photos folder contains pictures of dishes.

1. Check the contents#

Before recording, look through the folder’s contents and make sure there’s nothing that shouldn’t be uploaded.

What to checkExamplesWhat to do
Secrets.env, key files, settings containing passwordsExclude with .gitignore
Automatically generated files.DS_Store, logs, cachesExclude with .gitignore
Large filesVideos, large images, data exportsDon’t upload, or use a separate mechanism (below)
Things others hold rights toPurchased assets, confidential documentsDon’t put them in a Public repository

Listing with ls -a, including hidden files, reduces oversights.

2. Write .gitignore / 3. git init#

Once you’ve decided what to exclude, first turn the folder into a repository.

text1 lines
git init
text1 lines
Initialized empty Git repository in /Users/taro/kitchen-notes/.git/

Check what goes in with git add -n#

Here, instead of jumping straight to git add ., check first what will be included. -n means “don’t actually do anything; just show what would be done” (a dry run).

text1 lines
git add -n .
text5 lines
add '.DS_Store'
add '.env'
add 'README.md'
add 'meal-prep.md'
add 'photos/curry.jpg'

This shows that .env and .DS_Store would be included. Write a .gitignore and check again.

text2 lines
printf '.env\n.DS_Store\n' > .gitignore
git add -n .
text4 lines
add '.gitignore'
add 'README.md'
add 'meal-prep.md'
add 'photos/curry.jpg'

The two files you wanted to exclude are gone, and the .gitignore itself is now included instead. The .gitignore is a file you should record (Part 2, #12).

4. The first commit#

text2 lines
git add .
git commit -m "Start managing kitchen notes with Git"
text6 lines
[main (root-commit) d15c20c] Start managing kitchen notes with Git
 4 files changed, 4 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 README.md
 create mode 100644 meal-prep.md
 create mode 100644 photos/curry.jpg

root-commit marks the very first commit, which has no parent. Everything up to here works without an internet connection.

5. Create it empty on GitHub / 6. Send it#

Create a new repository named kitchen-notes on GitHub. The steps are the same as in Part 6, #38; the important point is to create it empty, without adding a README, .gitignore or license. You already have a first commit locally, so if GitHub also has a separate first commit, the two histories won’t connect.

Once it’s created, register the URL and send.

text2 lines
git remote add origin https://github.com/your-username/kitchen-notes.git
git push -u origin main

The output has the same form as in 39; you’re done when you see * [new branch] main -> main and branch 'main' set up to track 'origin/main'.. Open the repository in your browser and confirm that .env and .DS_Store are not there.

FAQ#

Q. I created it on GitHub with a README added, and my push was rejected.

A. The README commit on GitHub and your local first commit are separate histories. Bring in GitHub’s history and combine them with git pull --allow-unrelated-histories origin main, then push. --allow-unrelated-histories means “it’s OK to combine two histories that share no common ancestor.” If the README content conflicts, fix it following Part 4, #24.

Q. I pushed with a secret file still included.

A. First, revoke that secret (password or token) and create a new one. Do this before removing it from the history. If the repository is Public, someone may have seen it the moment you pushed. The GitHub feature that detects and alerts you to secrets is covered in Part 8, #57.

Summary#

  • When putting an existing folder on GitHub, check for secrets and large files before the first commit
  • Write what to exclude in .gitignore before the first git add
  • git add -n . lets you preview which files will be recorded
  • Create an empty repository on GitHub and send with git remote add and git push -u
  • GitHub doesn’t accept files larger than 100 MiB

Next page#

43. Issues: Making Work Visible — This starts Part 7. You’ll look at Issues, which record tasks and bugs on GitHub and share them with others.