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 -nto 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#
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).
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_StoreAssume 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 check | Examples | What to do |
|---|---|---|
| Secrets | .env, key files, settings containing passwords | Exclude with .gitignore |
| Automatically generated files | .DS_Store, logs, caches | Exclude with .gitignore |
| Large files | Videos, large images, data exports | Don’t upload, or use a separate mechanism (below) |
| Things others hold rights to | Purchased assets, confidential documents | Don’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.
git initInitialized 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).
git add -n .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.
printf '.env\n.DS_Store\n' > .gitignore
git add -n .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#
git add .
git commit -m "Start managing kitchen notes with Git"[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.jpgroot-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.
git remote add origin https://github.com/your-username/kitchen-notes.git
git push -u origin mainThe 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 addandgit 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.