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.
Converting an existing non-empty directory into a Git working directory and then pushing its contents to a remote repository involves a series of straightforward steps. This process allows you to manage your project with version control, which is beneficial for maintaining the integrity of your project, collaboration, tracking changes, and reverting to previous versions when necessary.
Initial Set-Up
To begin, you need to have Git installed on your machine. You can download and install Git from git-scm.com.
Check Git Installation
You can verify the installation by running the following command in your terminal:
Step 1: Initialize the Local Git Repository
Navigate to your project's root directory from your command line:
Once inside your project directory, convert it into a Git repository by running:
This command creates a new subdirectory named .git that houses all necessary repository files — a skeleton repository. There are no changes tracked yet, until you've made your first commit.
Step 2: Adding Files to Staging Area
Before committing your files, you need to add them to the staging area. The staging area is a file, generally contained in your Git directory, that stores information about what will go into your next commit.
To add all files in the directory (including subdirectories):
To add specific files:
Step 3: Committing Changes
After staging your files, you need to commit them, which records changes to the repository. You’ll provide a commit message, which is a brief description of the changes made.
Step 4: Link to Remote Repository
To push your commits to a remote repository, you need to specify a new remote URL pointing to a repository where you have write access.
Creating a Remote Repository
- Go to a service like GitHub, GitLab, or Bitbucket.
- Create a new repository.
- Do not initialize with README, .gitignore or License. This way, the repository will be created empty.
Link Your Local Repository to the Remote
Find the URL for your remote repository and add it to your local repository configuration:
Replace https://github.com/username/repository.git with the URL of your newly created remote repository.
Step 5: Push Changes to Remote Repository
Now, you can push your commits to the remote repository:
This command pushes the changes from your local master branch to the origin remote (alias for your remote URL), and -u sets the upstream reference for future operations.
Summary Table
Here is a summary of the basic commands used during the setup and initial push:
| Action | Command | Description |
| Initialize local repository | git init | Initializes a new git repository |
| Stage all modifications | git add . | Adds all files to staging area |
| Commit staged files | git commit -m "<commit_message>" | Records changes locally |
| Add remote repository | git remote add origin <remote_url> | Links local repo to remote repository |
| Push local commits to remote branch | git push -u origin master | Uploads committed files to remote |
Additional Tips
- Ignoring Files: You may want to ignore certain files (like log files or build directories) from being tracked by Git. To achieve this, create a
.gitignorefile in your project root and list the patterns of the files you want to ignore. - Check Status Frequently: Use
git statusto get a summary of which files have changes, which are staged, and which are not being tracked by Git.
Conclusion
Transforming a non-empty directory into a Git repository is a powerful step towards greater control over your project's versions and collaboration. The steps outlined provide a foundation for these capabilities, facilitating an environment where changes are tracked and managed effectively.

