3.9. Creating Your First Repository
Table of Contents
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:
- A folder that contains your project files.
- A hidden
.gitfolder that stores all history and metadata. - The basic unit you push to and pull from hosting services like GitHub and GitLab.
You will create two kinds of repositories:
- Local repository on your own computer.
- Remote repository on a server like GitHub or GitLab.
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:
- Create a local repository from scratch.
- Create a remote repository on GitHub and GitLab.
- Connect your local and remote repositories.
- Perform your first commit and push.
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:
mkdir -p ~/projects/backend-demo
cd ~/projects/backend-demoExample on Windows (PowerShell or cmd):
mkdir C:\code\backend-demo
cd C:\code\backend-demoNow this folder is just a normal directory with no Git history.
Initializing the Repository
Inside the project folder, run:
git initGit output will be similar to:
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:
git statusYou should see something like:
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:
echo "# Backend Demo Project" > README.mdCheck status again:
git statusYou should see:
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.
git add README.mdCheck status:
git statusYou should see:
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:
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:
git commit -m "Initial commit: add README"If Git does not yet know who you are, it may show an error like:
*** 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:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"Then run the commit again.
After committing, check status:
git statusShould show:
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:
git logYou will see something like:
commit 3fbdf1a2b8c5e1234567890abcdef1234567890 (HEAD -> master)
Author: Your Name <you@example.com>
Date: Thu Aug 1 12:34:56 2026 +0000
Initial commit: add READMEThis 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:
- Go to https://github.com.
- Click “Sign up.”
- 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:
- On the top right, click the + icon.
- Choose New repository.
You will see a form with several fields. A typical setup:
| Field | Example value | Notes |
|---|---|---|
| Owner | your-username | Your GitHub account |
| Repository name | backend-demo | Use short, lowercase, hyphen-separated name |
| Description | First backend demo repo | Optional but helpful |
| Visibility | Public or Private | Public is fine for learning |
| Initialize... | Leave all unchecked | Especially 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:
…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 mainYou will use these instructions in the next section.
Connecting Local and Remote
You now have:
- A local repository with at least one commit.
- An empty remote repository on GitHub.
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):
git remote add origin https://github.com/your-username/backend-demo.gitThis means:
remote addcreates a new remote connection.originis the standard default name for the main remote.- The URL points to your GitHub repository.
You can confirm the remote:
git remote -vYou should see:
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:
git branch
If you see master and you want to rename it to main (recommended to match GitHub default):
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:
git push -u origin mainWhat this means:
pushsends commits to a remote.originis the remote name.mainis the branch you are pushing.-usetsorigin mainas the default upstream branch, so later you can just typegit push.
Git will ask you to authenticate. This can happen in several ways:
- Web-based login popup.
- Personal Access Token instead of password.
- GitHub CLI or SSH keys.
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:
- Go to https://gitlab.com.
- Click “Register.”
- Fill in the details and verify your email.
Creating a New Repository on GitLab
In GitLab, projects are repositories. After logging in:
- Click New project or + and then New project.
- Choose Create blank project.
Fill the form:
| Field | Example value | Notes |
|---|---|---|
| Project name | backend-demo | Same style as GitHub |
| Project slug | backend-demo | Usually auto-filled from the name |
| Project URL | Keep default / your-username | Where your project lives in GitLab |
| Visibility level | Public or Private | Public is fine for learning |
| Initialize repo | Uncheck “Initialize repository with a README” if you already have one locally |
Click Create project.
On the empty project page, GitLab shows instructions like:
…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 mainSince you already have a local repo and commit, you only need to:
- Add the remote.
- Push.
Connecting and Pushing to GitLab
You have two options:
- Use the same local project and add GitLab as another remote.
- Create a separate local project for GitLab.
For learning, it is useful to see how to handle multiple remotes.
Adding GitLab as a Second Remote
From inside your existing project folder:
git remote add gitlab https://gitlab.com/your-username/backend-demo.gitNow you have two remotes:
git remote -vOutput:
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:
git push -u gitlab mainYour 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:
- Temporary files.
- Compiled artifacts.
- Local configuration.
- Virtual environment folders like
venv/.
You normally add a .gitignore file to tell Git which files or folders to ignore.
In your project:
echo "venv/" > .gitignore
echo "__pycache__/" >> .gitignore
echo "*.pyc" >> .gitignoreCheck:
cat .gitignoreYou should see:
venv/
__pycache__/
*.pycStage and commit:
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 tooNow 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.
- Start work
cd ~/projects/backend-demo
git status- Create or edit files
touch app.py
echo "print('Hello backend')" > app.py- Check what changed
git status- Stage changes
git add app.py- Commit with a clear message
git commit -m "Add simple hello backend script"- Push to GitHub
git push- Optionally push to GitLab
git push gitlab mainRepeat this cycle as you build out your backend code.
Common Problems and Fixes
Mistyped Remote URL
If you added the wrong remote URL:
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:
git init
git remote add origin <url>Then proceed as usual.
Nothing to Push
If git push says:
Everything up-to-dateBut you expect files on GitHub, make sure you:
- Added files with
git add. - Committed with
git commit. - Pushed the correct branch to the correct remote.
Summary
You now know how to:
- Create a local Git repository with
git init. - Add files, make your first commit, and see history.
- Create remote repositories on GitHub and GitLab.
- Connect local and remote with
git remote add. - Push your code with
git push. - Use
.gitignoreto keep unwanted files out of your repo.
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
KAHIBARO