- Why You Should Add an Existing Project to a Git Repository
- Key advantages of using Git, GitHub and GitLab
- Scenario 1: Your Git Repository Is Empty
- Step 1: Navigate to your project directory
- Step 2: Initialize a new Git repository
- Step 3: Add all files to Git
- Step 4: Create your first commit
- Step 5: Connect the local project to GitHub or GitLab
- Step 6: Create or rename the main branch
- Step 7: Push all files to the remote repository
- Scenario 2: The Repository Already Contains Files
- Step 1: Rename your old project directory
- Step 2: Clone the remote repository into a new folder
- Step 3: Copy your existing files into the cloned project
- Step 4: Add all files to the Git index
- Step 5: Commit the imported files
- Step 6: Push everything to GitHub or GitLab
- Overview of Important Git Commands and Linux Commands
- How to Write Good Git Commit Messages
- Best practices for commit messages
- Good examples
- What Are Gitmojis?
- Tips, Tricks and Best Practices
- Use a proper .gitignore
- Avoid storing large media files
- Use feature branches
- Commit small, frequent updates
- Use SSH keys
- Run git status regularly
- FAQ
- Glossary
Adding version control to an already existing project is one of the most common situations developers face. Many websites, WordPress installations, Node.js apps or custom scripts grow over time without ever being tracked in Git. At some point, managing changes manually becomes difficult, team collaboration gets messy, and backups are unreliable.
This is where Git, GitHub and GitLab come into play.
In this step-by-step Git tutorial, you will learn exactly how to add an existing project to a new or partially configured GitHub or GitLab repository. This guide also includes a detailed explanation of important Git commands, Linux commands, best practices, commit message strategies, Gitmoji usage, troubleshooting tips, an FAQ and a glossary.
Whether you are working on a small personal project or managing large production systems, this guide will help you turn a messy folder into a clean, version-controlled codebase.
Why You Should Add an Existing Project to a Git Repository
Version control is one of the most important tools in software development today. Even if your project started on a server, on your desktop, or as a quick experiment, moving it into a Git repository provides long-term benefits.
Key advantages of using Git, GitHub and GitLab
- Reliable version control – Every change is saved and traceable
- Easy rollback – Undo mistakes or bugs with a single command
- Clear history – See who changed what and when
- Better team collaboration – No more overwriting files or guessing what’s new
- Automated deployments – CI/CD pipelines on GitHub and GitLab
- Cleaner backups – Your entire project lives in a safe remote repository
- Branching workflows – Develop new features without breaking production
Using Git is essential for dynamic web projects such as:
- WordPress
- Laravel
- Node.js / Express
- React / Vue / Next.js
- Static HTML / CSS sites
- Python or PHP scripts
Even simple scripts benefit from version control, especially when used across servers or teams.
Scenario 1: Your Git Repository Is Empty
If the repository on GitHub or GitLab does not contain any files yet, the process is straightforward. You simply initialize Git inside your existing project and push everything to the remote.
Step 1: Navigate to your project directory
Open your terminal and switch to your project folder.
cd /path/to/your/project
Step 2: Initialize a new Git repository
git init
This creates a hidden .git folder that contains all version history and metadata.
Step 3: Add all files to Git
git add -A
This stages every file for the next commit.
Tip: Check the status before committing:
git status
Step 4: Create your first commit
git commit -m "Initial commit - import existing project"
This creates the first snapshot of your code.
Step 5: Connect the local project to GitHub or GitLab
git remote add origin https://github.com/user/project.git
or:
git remote add origin https://gitlab.com/user/project.git
The remote “origin” is your online repository.
Step 6: Create or rename the main branch
git branch -M main
This ensures your project uses the modern main branch instead of the older master naming.
Step 7: Push all files to the remote repository
git push -u origin main
Congratulations — your entire existing project is now safely version-controlled on GitHub or GitLab.
Scenario 2: The Repository Already Contains Files
If your GitHub or GitLab repository already includes files (for example README.md or a .gitignore), pushing your existing project directly would cause merge conflicts.
The cleaner workflow is to clone the repository first and copy your project into it.
Step 1: Rename your old project directory
mv project project.bkp
This keeps your old files safe while preparing the new Git version.
Step 2: Clone the remote repository into a new folder
git clone https://github.com/user/project.git project
or:
git clone https://gitlab.com/user/project.git project
Now the folder contains your remote repo’s initial structure.
Step 3: Copy your existing files into the cloned project
cp -a project.bkp/. project/
-a ensures permissions and timestamps stay intact.
Step 4: Add all files to the Git index
cd project
git add -A
Step 5: Commit the imported files
git commit -m "Import existing project into repository"
Step 6: Push everything to GitHub or GitLab
git push -u origin main
Your existing code is now cleanly integrated into the repository.
Overview of Important Git Commands and Linux Commands
| Task | Command | Description |
|---|---|---|
| Initialize Git | git init | Start version control in a local folder |
| Add files | git add -A | Add all changes to the staging area |
| Create a commit | git commit -m “msg” | Save a version snapshot |
| Connect to remote | git remote add origin URL | Link local repo with GitHub/GitLab |
| Clone repository | git clone URL | Download a remote repository |
| Rename/set branch | git branch -M main | Use “main” as the default branch |
| Upload changes | git push -u origin main | Push all commits to the server |
| Copy folder | cp -a old/. new/ | Copy files including permissions |
| Rename folder | mv old new | Rename directories or files |
| Check status | git status | Display untracked/changed files |
| Show history | git log | View commit history |
How to Write Good Git Commit Messages
Commit messages play a huge role in code quality. Good commit logs help you understand the history, debug issues faster and collaborate with others.
Best practices for commit messages
- Write in the imperative mood:
Add feature, Fix bug, Remove file - Keep the summary short (under 50 characters)
- Add a detailed explanation below if needed
- Avoid vague descriptions
- Reference issues or tasks when relevant
Good examples
- Add initial project structure
- Fix footer alignment on mobile
- Update Docker Compose configuration
- Remove unused CSS files
- Improve caching logic
What Are Gitmojis?
Gitmoji is a community project that assigns emojis to commit types. These emojis make your Git history more readable, fun and organized.
| Emoji | Meaning | Use Case |
|---|---|---|
| 🎉 (:tada:) | Initial commit | First version of the project |
| 🐛 (:bug:) | Bug fix | Fixing an error or regression |
| ✨ (:sparkles:) | New feature | Adding new functionality |
| 🔧 (:wrench:) | Configuration | Editing config files or settings |
| 🔥 (:fire:) | Remove code | Deleting unused files or features |
| 📝 (:memo:) | Documentation | Updating docs, README, comments |
Using Gitmojis is popular in open-source communities and helps teams scan commit logs much faster.
Tips, Tricks and Best Practices
Here are helpful recommendations for managing existing projects versioned with Git:
Use a proper .gitignore
Exclude unnecessary or sensitive directories such as:
node_modules/vendor/wp-content/uploads/cache/.env
Avoid storing large media files
Use object storage, a CDN or the filesystem instead.
Use feature branches
Develop new functions cleanly without breaking the main branch.
Commit small, frequent updates
Smaller commits are easier to understand and revert.
Use SSH keys
Avoid entering your username and password for every push.
Run git status regularly
This prevents accidents like pushing test files or local configs.
FAQ
Can I use Git for any type of project?
Absolutely. Git works for websites, scripts, backend services, mobile apps, databases, and even configuration files.
Should I store images or uploads in Git?
Usually no. They bloat the repository and slow down cloning.
What if I run into merge conflicts?
Edit the affected files manually and resolve the conflict markers.
Can I push to both GitHub and GitLab?
Yes, you can add multiple remotes:
git remote add github URL
git remote add gitlab URL
Glossary
Git Repository
A structured folder containing all project versions and history.
GitHub / GitLab
Platforms for storing and managing Git repositories.
Commit
A saved snapshot of code changes.
Push
Upload local commits to a remote server.
Clone
Download a remote repository to your machine.
Branch
A separate development line for features or fixes.
Staging Area (Index)
A middle layer where changes wait before being committed.

