This guide explains how to save your project code to GitHub or GitLab using Git. By the end, you will:
- β Install Git and set it up on your computer.
- β Create a new repository on GitHub or GitLab.
- β Link your local project to the repository.
- β Save (commit) your work and upload (push) it to the repository.
- β Keep your code updated over time.
Git is a tool that helps track changes in your code. It allows you to:
- Save different versions of your project without making multiple copies.
- Undo mistakes easily by restoring previous versions.
- Collaborate with others by merging code changes efficiently.
GitHub and GitLab are websites that store your Git projects online so you can:
- Back up your code safely.
- Work on your project from any computer.
- Share your work with others.
Before using Git, you need to install it on your computer.
Open Command Prompt (Windows) or Terminal (Mac/Linux) and type:
git --versionIf Git is installed, you will see something like:
git version 2.34.1If you donβt see a version number, install Git using the steps below.
Install Git
Windows: Download and install from Git for Windows.
Mac: Run:
brew install gitLinux (Debian/Ubuntu): Run:
sudo apt install gitOnce installed, verify by running:
git --versionπ Step 2: Set Up Your Git Information
Before using Git, configure your name and email (this helps track who made changes).
Run:
git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"To check your settings:
git config --listπ Step 3: Create a New Repository on GitHub or GitLab
A repository (repo) is where your projectβs files will be stored online.
1οΈβ£ Create a Repository on GitHub
Go to GitHub and log in.
Click the + button (top right) β New Repository.
Choose a repository name (e.g., my-first-repo).
Do NOT check "Initialize with a README" (we will add our own).
Click Create Repository.
GitHub will show a page with a repository URL (copy this, weβll need it later).
2οΈβ£ Create a Repository on GitLab
Go to GitLab and log in.
Click New Project β Create Blank Project.
Enter a repository name (e.g., my-first-repo).
Do NOT check "Initialize with a README".
Click Create Project.
Copy the repository URL provided.
π Step 4: Link Your Local Project to GitHub/GitLab
1οΈβ£ Open Your Project Folder Navigate to your project folder using Terminal (Mac/Linux) or Command Prompt (Windows). For example:
cd /path/to/your/project2οΈβ£ Initialize Git in Your Project Run:
git initThis creates a hidden .git/ folder, making your project a Git repository.
3οΈβ£ Add Your Files to Git To tell Git to track all your project files, run:
git add .The . means "add everything in this folder". Git now knows these files exist but hasn't saved them yet.
4οΈβ£ Save (Commit) Your Work Run:
git commit -m "Initial commit: added project files"The -m lets you add a commit message describing the change. This saves your work in Git locally (on your computer), but it is not yet online.
π Step 5: Connect to GitHub or GitLab
Now, we link our local project to the remote repository.
1οΈβ£ Add Your GitHub or GitLab Repository URL Copy the repository URL from Step 3 and run:
git remote add origin https://github.com/your-username/my-first-repo.gitor (for GitLab):
git remote add origin https://gitlab.com/your-username/my-first-repo.gitVerify the connection:
git remote -vYou should see:
origin https://github.com/your-username/my-first-repo.git (fetch)
origin https://github.com/your-username/my-first-repo.git (push)2οΈβ£ Upload (Push) Your Code Now, send your local files to GitHub/GitLab by running:
git branch -M main # Renames the main branch (optional)
git push -u origin mainYou will be prompted to enter your GitHub or GitLab credentials.
Once done, go to your repository online, and you should see your files!
π Step 6: Keeping Your Code Updated
After making changes to your code, use these three commands to update GitHub/GitLab:
git add .
git commit -m "Updated feature XYZ"
git push origin mainπΉ git add . β Adds all changes to Git.1
πΉ git commit -m "message" β Saves the changes locally.
πΉ git push origin main β Uploads changes to GitHub/GitLab.\
π Troubleshooting
β "fatal: remote origin already exists" This happens if you already linked your repo. Fix it by running: git remote remove origin git remote add origin https://github.com/your-username/my-first-repo.git
β "Permission denied (publickey)" If GitHub/GitLab asks for SSH access but fails, use HTTPS instead:
git remote set-url origin https://github.com/your-username/my-first-repo.git
π Summary
| Step | Command |
|---|---|
| Check Git Version | git --version |
| Set Up Git | git config --global user.name "Your Name" |
| Initialize a Git Repo | git init |
| Add Files | git add . |
| Commit Changes | git commit -m "Initial commit" |
| Connect to GitHub/GitLab | git remote add origin REPO_URL |
| Push Code Online | git push origin main |
| Update Code | git add . && git commit -m "message" && git push origin main |
π Congratulations! Youβve successfully uploaded your code to GitHub or GitLab! π