Convert a local folder to a Git repo
About
Converting a local folder to a Git repository means initializing Git tracking in that folder so Git can:
Track file changes
Record history
Enable version control
Connect the folder to a remote Git repository (optional)
This process does not move files or change their content — it simply adds Git tracking capabilities to the folder.
When we do this ?
We convert a folder to a Git repo when:
We are starting a new project that’s not under version control
We want to add Git tracking to an existing codebase
We plan to push the folder to a remote Git server like GitHub or GitLab
Steps to Convert a Local Folder to a Git Repo
1. Open Terminal and Navigate to the Folder
cd /path/to/our/folder
This should be our project directory with files already present.
2. Initialize the Git Repository
git init
This creates a .git
folder inside our directory. That hidden folder stores all Git configuration, branches, commits, and metadata.
3. Stage the Files for Commit
git add .
This adds all files in the folder to the staging area (i.e., marks them to be included in the next commit).
4. Create the First Commit
git commit -m "Initial commit"
This creates the first snapshot of our project.
At this point, our folder is fully Git-tracked locally. It now has commit history and can use all Git features.
Steps to Connect to a Remote Repository
If we want to push our local Git repo to a platform like GitHub or GitLab:
5. Add the Remote Repository URL
git remote add origin https://our-remote-url.com/our-repo.git
Replace
origin
with a custom name if we want, butorigin
is the default and commonly used.
6. Push the Repo to Remote (first push)
git push -u origin main
If our branch is called
master
, replacemain
withmaster
.
The -u
flag sets the upstream so that future git push
and git pull
commands automatically know which remote and branch to use.
Important Notes
We can add a
.gitignore
file before or aftergit init
to exclude files/folders from being tracked.If we are working with a team, make sure to push to a remote repo so others can access our code.
git init
is non-destructive – it won't delete or overwrite our files.
Last updated
Was this helpful?