KAHIBARO
Discord Login Register

3.9. Creating Your First Repository

Why Repositories Matter

When you write code, you need a place to store it, track changes, and collaborate with others. A Git repository is that place.

A repository, or “repo,” is:

You will create two kinds of repositories:

You can work completely locally, but most real projects use both.

Rule: Every serious project should live in a version-controlled repository, not in random folders like final_version_new2_really_final/.

In this chapter, you will:

For all examples, you need Git installed and basic command-line knowledge from previous chapters.


Creating a Local Repository

You can create a new repository from scratch or turn an existing folder into a repository. Both ways use the same Git command: git init.

Creating a New Project Folder

Pick a folder where you keep your code, for example ~/projects or C:\Users\you\code.

Example on Linux/macOS:

bash
mkdir -p ~/projects/backend-demo
cd ~/projects/backend-demo

Example on Windows (PowerShell or cmd):

bash
mkdir C:\code\backend-demo
cd C:\code\backend-demo

Now this folder is just a normal directory with no Git history.

Initializing the Repository

Inside the project folder, run:

bash
git init

Git output will be similar to:

text
Initialized empty Git repository in /home/you/projects/backend-demo/.git/

Git created a hidden .git folder. This turns the directory into a repository.

You can verify that Git sees this folder as a repo:

bash
git status

You should see something like:

text
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)

The exact branch name might be master or main depending on your Git version.

Rule: Never manually edit or delete files inside the .git folder. Git manages it. Breaking .git can destroy your history.

Adding a First File

Create a simple file so the repo has content.

Example:

bash
echo "# Backend Demo Project" > README.md

Check status again:

bash
git status

You should see:

text
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        README.md

“Untracked” means Git sees the file but is not tracking it yet.


Initial Commit

To save a snapshot of your project, you create a commit. A commit is a recorded state of tracked files at a point in time.

Staging Changes with `git add`

You first tell Git which files to include in the commit. This is called staging.

bash
git add README.md

Check status:

bash
git status

You should see:

text
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   README.md

“Changes to be committed” means staged.

You can also stage everything in the current directory:

bash
git add .

Use this carefully, so you do not accidentally add files you do not want in the repo.

Writing the First Commit

Now create your first commit with a message:

bash
git commit -m "Initial commit: add README"

If Git does not yet know who you are, it may show an error like:

text
*** Please tell me who you are.
Run
  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

Set your name and email:

bash
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Then run the commit again.

After committing, check status:

bash
git status

Should show:

text
On branch master
nothing to commit, working tree clean

“Working tree clean” means everything is committed and there are no new changes.

Viewing History

To see your commit:

bash
git log

You will see something like:

text
commit 3fbdf1a2b8c5e1234567890abcdef1234567890 (HEAD -> master)
Author: Your Name <you@example.com>
Date:   Thu Aug 1 12:34:56 2026 +0000
    Initial commit: add README

This confirms your repository has at least one commit.


Creating a GitHub Repository

Now you will create a remote repository on GitHub and connect it to your local one.

Creating a GitHub Account

If you do not have one:

  1. Go to https://github.com.
  2. Click “Sign up.”
  3. Follow the steps to create an account and verify your email.

You can use the free plan. It is enough for personal and learning projects.

Creating a New Repository on GitHub

Once logged in:

  1. On the top right, click the + icon.
  2. Choose New repository.

You will see a form with several fields. A typical setup:

FieldExample valueNotes
Owneryour-usernameYour GitHub account
Repository namebackend-demoUse short, lowercase, hyphen-separated name
DescriptionFirst backend demo repoOptional but helpful
VisibilityPublic or PrivatePublic is fine for learning
Initialize...Leave all uncheckedEspecially do not add a README if you already have one locally

Click Create repository.

After creation, GitHub shows a page with instructions. You will see something like:

text
…or push an existing repository from the command line
git remote add origin https://github.com/your-username/backend-demo.git
git branch -M main
git push -u origin main

You will use these instructions in the next section.


Connecting Local and Remote

You now have:

Next you need to link them so you can push and pull.

Adding the Remote

From inside your local project folder, run the command GitHub provided (adjusted for your URL):

bash
git remote add origin https://github.com/your-username/backend-demo.git

This means:

You can confirm the remote:

bash
git remote -v

You should see:

text
origin  https://github.com/your-username/backend-demo.git (fetch)
origin  https://github.com/your-username/backend-demo.git (push)

Setting the Default Branch (if needed)

New Git installations usually use main as the default branch. Older setups might use master.

Check your branch:

bash
git branch

If you see master and you want to rename it to main (recommended to match GitHub default):

bash
git branch -M main

The -M flag forces the rename even if main exists.


Pushing to GitHub (First Push)

Now send your local commits to GitHub:

bash
git push -u origin main

What this means:

Git will ask you to authenticate. This can happen in several ways:

For beginners, the browser or token method is common.

When the push succeeds, go back to your GitHub repository page and refresh. You should now see your files and commit.

Rule: After git push completes with no errors, your code and history are stored safely on GitHub. This protects you if your laptop is lost or your disk fails.


Creating a GitLab Repository

GitLab is an alternative to GitHub. The process is very similar. You can learn both, because many companies use GitLab.

Creating a GitLab Account

If you do not have one:

  1. Go to https://gitlab.com.
  2. Click “Register.”
  3. Fill in the details and verify your email.

Creating a New Repository on GitLab

In GitLab, projects are repositories. After logging in:

  1. Click New project or + and then New project.
  2. Choose Create blank project.

Fill the form:

FieldExample valueNotes
Project namebackend-demoSame style as GitHub
Project slugbackend-demoUsually auto-filled from the name
Project URLKeep default / your-usernameWhere your project lives in GitLab
Visibility levelPublic or PrivatePublic is fine for learning
Initialize repoUncheck “Initialize repository with a README” if you already have one locally

Click Create project.

On the empty project page, GitLab shows instructions like:

text
…push an existing folder
cd existing_folder
git init
git remote add origin https://gitlab.com/your-username/backend-demo.git
git add .
git commit -m "Initial commit"
git push -u origin main

Since you already have a local repo and commit, you only need to:

Connecting and Pushing to GitLab

You have two options:

For learning, it is useful to see how to handle multiple remotes.

Adding GitLab as a Second Remote

From inside your existing project folder:

bash
git remote add gitlab https://gitlab.com/your-username/backend-demo.git

Now you have two remotes:

bash
git remote -v

Output:

text
gitlab  https://gitlab.com/your-username/backend-demo.git (fetch)
gitlab  https://gitlab.com/your-username/backend-demo.git (push)
origin  https://github.com/your-username/backend-demo.git (fetch)
origin  https://github.com/your-username/backend-demo.git (push)

To push to GitLab:

bash
git push -u gitlab main

Your project is now hosted on both GitHub and GitLab.

If you prefer to have separate projects, you can repeat the earlier steps in another local directory and follow GitLab’s own command examples.


Creating a .gitignore File

Many files should not be stored in Git, for example:

You normally add a .gitignore file to tell Git which files or folders to ignore.

In your project:

bash
echo "venv/" > .gitignore
echo "__pycache__/" >> .gitignore
echo "*.pyc" >> .gitignore

Check:

bash
cat .gitignore

You should see:

text
venv/
__pycache__/
*.pyc

Stage and commit:

bash
git add .gitignore
git commit -m "Add basic Python .gitignore"
git push   # uses origin main by default if set earlier
git push gitlab main   # if you want GitLab to get this too

Now these paths are ignored locally and will not be accidentally committed.


Basic Everyday Workflow Example

To make this concrete, here is a simple daily sequence for working on a backend project with your new repository.

  1. Start work
bash
cd ~/projects/backend-demo
git status
  1. Create or edit files
bash
touch app.py
echo "print('Hello backend')" > app.py
  1. Check what changed
bash
git status
  1. Stage changes
bash
git add app.py
  1. Commit with a clear message
bash
git commit -m "Add simple hello backend script"
  1. Push to GitHub
bash
git push
  1. Optionally push to GitLab
bash
git push gitlab main

Repeat this cycle as you build out your backend code.


Common Problems and Fixes

Mistyped Remote URL

If you added the wrong remote URL:

bash
git remote set-url origin https://github.com/your-username/correct-name.git

Check again with git remote -v.

Forgot to Initialize Before Adding

If you forgot git init and git remote add fails, run:

bash
git init
git remote add origin <url>

Then proceed as usual.

Nothing to Push

If git push says:

text
Everything up-to-date

But you expect files on GitHub, make sure you:

  1. Added files with git add.
  2. Committed with git commit.
  3. Pushed the correct branch to the correct remote.

Summary

You now know how to:

With this foundation, you are ready to follow the Basic Git Workflow in later chapters and use repositories for all your backend development projects.

Views: 9

Comments

Please login to add a comment.

Don't have an account? Register now!