Skip to main content

How to Create a Git Repository and Make Your First Commit

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

Git keeps a history of every change you make to a project, so you can see what changed, when, and go back if something breaks. Three commands turn any folder into a Git repository with its first saved snapshot: git init, git add and git commit. This guide runs them on a real project and shows every line of output.

What a Repository and a Commit Are​

  • A repository is your project folder plus a hidden .git folder, where Git keeps the history.
  • A commit is a saved snapshot of your files, with a message that says what changed.
  • Between the two sits the staging area: you choose which changes go into the next commit with git add, then save them with git commit.

The example here is the project folder from the series How to Connect a .NET C# Web API to a MySQL Database. It holds three files: .gitignore, .env.example and compose.yaml. The same commands work for any folder.

Prerequisites​

  • Git, installed from git-scm.com. Check it in a terminal:
Terminal
git --version
Output
git version 2.56.0
  • A terminal: Terminal on macOS, any shell on Linux, or PowerShell on Windows. Every command in this guide works unchanged in bash, zsh and PowerShell.

Step 1: Tell Git Who You Are​

Every commit records an author name and email. Set them once per computer:

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

Put your own name and email between the quotes. --global saves them for every repository on this computer. Check them with:

Terminal
git config --global user.name

If you skip this step, Git tries to guess, and on macOS it builds an identity from your computer's user name and host name. Here is what I got in a test folder with no identity set (my name and host name replaced):

Output
[master (root-commit) 05d6846] First commit
Committer: Your Name <you@Your-Computer.lan>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

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

After doing this, you may fix the identity used for this commit with:

git commit --amend --reset-author

1 file changed, 1 insertion(+)
create mode 100644 notes.txt

A guessed address like that ends up in your history forever, so set your identity before your first real commit.

Step 2: Create the Repository​

Open a terminal in your project folder and run:

Terminal
git init -b main
Output
Initialized empty Git repository in /Users/you/projects/dewiride-inventory-api/.git/

git init creates the hidden .git folder. -b main names the first branch main, the name GitHub, Azure Repos and most teams use today. Without -b, Git 2.56 still calls it master and prints a long hint:

Output (git init without -b)
hint: Using 'master' as the name for the initial branch. This default branch name
hint: will change to "main" in Git 3.0. To configure the initial branch name
hint: to use in all of your new repositories, which will suppress this warning,
hint: call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>
hint:
hint: Disable this message with "git config set advice.defaultBranchName false"
Initialized empty Git repository in /Users/you/projects/demo/.git/

Step 3: See What Git Sees​

Terminal
git status
Output
On branch main

No commits yet

Untracked files:
(use "git add <file>..." to include in what will be committed)
.env.example
.gitignore
compose.yaml

nothing added to commit but untracked files present (use "git add" to track)

Untracked means Git can see the files but isn't saving them yet. A fourth file, .env, holds real passwords and is missing from the list on purpose: the .gitignore file tells Git to ignore it. How to Add a .gitignore to a .NET C# Project shows how that works.

Check the list before every first commit

Anything listed under "Untracked files" goes into the commit when you run git add .. If you see a password file, a key or a database dump there, add it to .gitignore first.

Step 4: Stage the Files​

Terminal
git add .

The . means "everything in this folder that isn't ignored". The command prints nothing. Run git status again to see the result:

Output
On branch main

No commits yet

Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .env.example
new file: .gitignore
new file: compose.yaml

The files moved from "Untracked" to "Changes to be committed": they are staged.

Step 5: Commit​

Terminal
git commit -m "Add the local MySQL 9.7 development database"
Output
[main (root-commit) 1b0f10d] Add the local MySQL 9.7 development database
3 files changed, 524 insertions(+)
create mode 100644 .env.example
create mode 100644 .gitignore
create mode 100644 compose.yaml
  • -m gives the commit its message. Write what the change does, as if finishing the sentence "This commit will…".
  • root-commit means this is the first commit in the repository.
  • 1b0f10d is the short ID of the commit. Yours will be different.

Step 6: Look at the History​

Terminal
git log --oneline
Output
1b0f10d Add the local MySQL 9.7 development database

Each commit appears on one line, newest first. That's your project's first saved point.

A terminal running git init -b main, git add . and git commit -m "Add the local MySQL 9.7 development database", which creates commit 1b0f10d with three files, followed by git log --oneline showing that one commit.

Common Mistakes​

MistakeWhat happensDo this instead
Committing before setting user.name and user.emailGit guesses an identity from your computer name, and the commit keeps itRun the two git config --global commands first
Running git init in your home folder instead of the project folderGit starts tracking everything in your home foldercd into the project folder first, and check the path in the Initialized… line
git add . before creating .gitignoreSecrets and build output get stagedCreate .gitignore first, then check git status
A commit message like "update"The history doesn't say what changedDescribe the change: "Add the local MySQL 9.7 development database"

Troubleshooting​

SymptomLikely causeFix
git: command not found or 'git' is not recognizedGit isn't installed, or the terminal was opened before installing itInstall Git, then open a new terminal
Author identity unknown and the commit failsGit couldn't guess an email address on this computerSet user.name and user.email, then commit again
nothing to commit, working tree cleanEvery file is already committed, or all of them are ignoredCheck git status --ignored to see what's ignored
The branch is called mastergit init was run without -b maingit branch -m main renames it

FAQ​

What is the difference between git add and git commit?​

git add chooses which changes go into the next snapshot (the staging area). git commit saves that snapshot permanently, with a message. Splitting them lets you commit some changes and keep others for later.

Do I need GitHub to use Git?​

No. Everything here happens on your own computer. GitHub, Azure Repos and similar services are places you can push a repository to later, to back it up or share it.

Can I set main as the default branch for every new repository?​

Yes: git config --global init.defaultBranch main. After that, plain git init uses main without the hint.

Conclusion​

git init -b main created the repository, git add . staged the project's files, and git commit saved them as the first snapshot, which git log --oneline now shows. Set your name and email once per computer, and keep a .gitignore in place before your first git add, so secrets never enter the history.

Additional Resources​

Stay Updated

Subscribe to our newsletter for the latest tutorials, tech insights, and developer news.

By subscribing, you agree to our privacy policy. Unsubscribe at any time.