How to Add an Existing Project to GitHub or GitLab – Complete Step-by-Step Git Guide

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

TaskCommandDescription
Initialize Gitgit initStart version control in a local folder
Add filesgit add -AAdd all changes to the staging area
Create a commitgit commit -m “msg”Save a version snapshot
Connect to remotegit remote add origin URLLink local repo with GitHub/GitLab
Clone repositorygit clone URLDownload a remote repository
Rename/set branchgit branch -M mainUse “main” as the default branch
Upload changesgit push -u origin mainPush all commits to the server
Copy foldercp -a old/. new/Copy files including permissions
Rename foldermv old newRename directories or files
Check statusgit statusDisplay untracked/changed files
Show historygit logView 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.

EmojiMeaningUse Case
🎉 (:tada:)Initial commitFirst version of the project
🐛 (:bug:)Bug fixFixing an error or regression
✨ (:sparkles:)New featureAdding new functionality
🔧 (:wrench:)ConfigurationEditing config files or settings
🔥 (:fire:)Remove codeDeleting unused files or features
📝 (:memo:)DocumentationUpdating 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.

Durchschnittliche Bewertung 0 / 5. Bewertungen: 0

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top