THE GITHTML FIELD GUIDE
Initialize Git in an existing project safely
To put an existing project under Git, first confirm the project’s exact directory and whether it already belongs to a repository. Initialize only the intended folder, choose a branch name, add appropriate ignore rules, and stage selected files. Initialization creates local metadata; it does not upload files or create a GitHub repository.
Check the boundary before initialization
The most expensive first-repository mistake is choosing the wrong folder. Initializing a home directory or a parent containing several projects can expose unrelated files during later staging. Inspect the current path and directory contents in your file manager or terminal. Run git rev-parse --show-toplevel to discover an existing repository root; an error can be expected if there is no repository yet.
If the command reports an ancestor directory, stop and investigate. You may already be inside another repository, where adding a nested repository is a separate design decision. Do not delete an existing .git directory to start over. It contains history and configuration that may be valuable even when the visible working files look ordinary.
Initialize and stage a small first snapshot
For a genuinely unversioned project, initialize with an explicit initial branch name. Prepare ignore rules in your editor before staging. Build outputs, local credentials, dependency caches, and personal machine settings need different treatment from project source. The example assumes app.js and package.json are real files you have reviewed; substitute your actual paths.
- Inspect every initially staged file for credentials, private exports, and large binaries. A later ignore rule cannot erase an earlier committed secret.
- If identity configuration blocks the commit, configure the correct author name and email for this repository rather than copying another person’s example identity.
git init -b main
git status --short
git add -- app.js package.json
git diff --staged
git commit -m "Record initial application source"Verify the local result before connecting a remote
After committing, inspect git log -1 --stat and git status. The log should show the files you intended to preserve, and status should make any remaining local files understandable. Untracked files are not automatically mistakes: some may deliberately remain outside the first snapshot. Record why important project files were excluded so the next contributor can reproduce the setup.
Connecting a remote is a later step. If the GitHub repository already contains commits, clone it and reconcile the existing project deliberately instead of pushing unrelated history over it. If it is empty, add its verified URL and publish the intended branch when ready. Keeping initialization, first commit, and first upload separate makes each action reviewable and gives a clear stopping point when the destination is uncertain.
Keep the initial scope narrow enough that another contributor could explain what each tracked file is for without needing access to your personal machine.
Sources and further reading
AI-assisted writing with source-linked guidance and illustrative examples. Read our editorial approach or report a correction.