Skip to content

techric/How-to-Save-Code-to-A-Repository-Using-Git

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 

Repository files navigation

How-to-Save-Code-to-A-Repository-Using-Git

πŸš€ Beginner's Guide: How to Save Code to GitHub or GitLab

πŸ“Œ Overview

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.

πŸ“‚ What is Git? What is GitHub/GitLab?

Git

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 / GitLab

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.

πŸ“‚ Step 1: Install Git

Before using Git, you need to install it on your computer.

Check if Git is already installed

Open Command Prompt (Windows) or Terminal (Mac/Linux) and type:

git --version

If Git is installed, you will see something like:

git version 2.34.1

If 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 git

Linux (Debian/Ubuntu): Run:

sudo apt install git

Once 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/project

2️⃣ Initialize Git in Your Project Run:

git init

This 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.git

or (for GitLab):

git remote add origin https://gitlab.com/your-username/my-first-repo.git

Verify the connection:

git remote -v

You 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 main

You 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! πŸš€

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published