How to convert existing non-empty directory into a Git working directory and push files to a remote repository
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Converting an existing non-empty folder into a Git repository is easy technically, but the first commit is where many long-term mistakes are introduced. The safe workflow is to initialize Git, add ignore rules before staging, inspect what will be committed, and only then connect a remote. That extra caution prevents accidental secrets, generated files, and messy initial history.
Initialize Git in the Existing Directory
Change into the project directory and initialize the repository:
This creates a .git directory that stores the repository metadata. Your working files stay in place; Git simply starts tracking the directory.
At that point, verify status:
You should see the current files as untracked.
Add a .gitignore Before the First Commit
This is the step people skip most often. Add ignore rules before staging everything so you do not accidentally commit build output, virtual environments, or secrets.
Only once git status looks correct should you stage the project.
Stage and Create the First Commit
If Git does not know your identity yet, configure it first:
You can inspect exactly what the first commit will contain with git diff --cached before you commit.
Connect the Remote Repository
After the local repository looks correct, add a remote and choose a branch name. main is the modern default in most hosted Git services:
The -u flag sets origin/main as the upstream branch so later git push commands can omit the remote and branch name.
Full Command Sequence
For a normal new setup, the whole flow is:
If the remote repository already contains commits, do not blindly push over it. Fetch first and inspect the remote history.
What If the Remote Is Not Empty?
If the remote already has a README, license file, or another initial commit, your first push may fail with a non-fast-forward error. In that case, fetch first and inspect the existing history:
From there, decide whether you want to merge the histories, rebase your local work onto the remote branch, or start over with a fresh remote. The important part is to make that choice deliberately instead of force-pushing by reflex.
Common Pitfalls
- Adding
.gitignoreaftergit add .. Fix: define ignore rules before staging the first snapshot. - Initializing Git in the wrong directory. Fix: confirm you are at the real project root before running
git init. - Using the wrong default branch name for the remote. Fix: align the local branch with your hosting platform or team convention.
- Pushing to a non-empty remote without checking its history. Fix: fetch first and decide whether to merge, rebase, or recreate the remote.
- Treating the first commit like a dump of everything on disk. Fix: inspect
git statusandgit diff --cachedbefore committing.
Summary
- '
git initturns an existing directory into a Git repository without moving your files.' - Add
.gitignorebefore the firstgit add .. - Review the staged content before creating the initial commit.
- Add the remote only after the local repository is in a clean state.
- Use
mainor your team's standard branch name and avoid force-pushing blindly to non-empty remotes.

