How to Use Git: Install Git and Make Your First Commit

To use Git, install it, tell it your name and email once, and turn a project folder into a repository with git init. After that, you save versions of your work in two steps: git add picks the changes, and git commit saves them as a snapshot.

What you'll build or solve

You'll install Git, set it up, and create a small practice project with two commits in its history. Done looks like git log --oneline listing both commits.

Prerequisites

  • A computer running Windows, macOS, or Linux
  • Permission to install software
  • A terminal: Git Bash on Windows (it comes with Git), Terminal on macOS or Linux
  • Optional: a code editor such as VS Code for editing files

Step-by-step instructions

Step 1: Install Git

Windows:

  1. Download Git for Windows from git-scm.com/downloads.
  2. Run the installer. The default options work for these tutorials.
  3. One screen asks which editor Git should use. The default is Vim. If you don't know Vim, pick an editor you know, such as Notepad or VS Code.
  4. Open Git Bash from the Start menu. Run every command in these tutorials in Git Bash.

macOS:

Open Terminal and run:

git --version

If Git isn't installed yet, macOS shows a dialog offering to install the command line developer tools. Click Install, wait for it to finish, then run git --version again.

If you use Homebrew, you can install Git with it instead:

brew install git

Linux (Debian or Ubuntu):

sudo apt update
sudo apt install git

On Fedora, use sudo dnf install git.


Step 2: Check that Git works

git --version

You should see a version number:

git version 2.47.3

Your number may differ. Any recent version works, but these tutorials assume Git 2.28 or newer.

What to look for

If you see command not found or 'git' is not recognized, close the terminal, open a new one, and try again. On Windows, make sure you're in Git Bash.


Step 3: Set your name and email

Git stores a name and email with every commit. Set them once for your computer:

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

Use your real name and the email you use, or plan to use, for GitHub.

Then set the name of the first branch in new projects to main, which GitHub also uses:

git config --global init.defaultBranch main

Check your settings:

git config --global --list
user.name=Your Name
user.email=you@example.com
init.defaultbranch=main

Step 4: Create a project folder and a repository

Make a practice folder, move into it, and turn it into a Git repository:

mkdir git-practice
cd git-practice
git init
Initialized empty Git repository in /home/you/git-practice/.git/

The path shows where your folder is, so yours will look different. git init creates a hidden .git folder. That's where Git keeps your project's history. Don't edit or delete it.


Step 5: Create a file and check the status

Create a file called README.md with one line of text:

echo "# Git practice" > README.md

You can also create the file in your code editor and save it in the git-practice folder.

Now ask Git what changed:

git status
On branch main

No commits yet

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

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

"Untracked" means Git sees the file but isn't saving its versions yet.


Step 6: Stage the file with git add

git add README.md
git status
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   README.md

The file is now staged. Staging picks which changes go into the next commit. To stage every changed file in the folder at once, use git add ..

On Windows, git add may also print warning: in the working copy of 'README.md', LF will be replaced by CRLF the next time Git touches it. That's harmless. Git for Windows is only adjusting line endings.


Step 7: Make your first commit

git commit -m "Add README"
[main (root-commit) c8ee487] Add README
 1 file changed, 1 insertion(+)
 create mode 100644 README.md

-m adds a short message that describes the change. c8ee487 is the start of the commit's ID, called a hash. Your hash will be different.


Step 8: Make a second commit and view the history

Add a line to the file, then check the status:

echo "Learning Git step by step." >> README.md
git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")

>> adds a line to the end of the file. A single > would replace the whole file.

Stage and commit the change, then view the history:

git add README.md
git commit -m "Describe the project"
git log --oneline
1c05a7f (HEAD -> main) Describe the project
c8ee487 Add README

The newest commit is at the top. (HEAD -> main) marks the branch you're on. This is the loop you'll repeat in every project: edit files, git add, git commit.


Common mistakes and how to fix them

Mistake 1: Committing before setting your name and email

You might see:

Author identity unknown

*** Please tell me who you are.

Why it breaks:

Git refuses to commit, or on some computers commits with a made-up name and email based on your computer's name. Set them first.

Fix:

Run the two git config --global commands from Step 3, then commit again. If Git already made the commit with the wrong name, fix it with git commit --amend --reset-author --no-edit after setting your name and email.


Mistake 2: Running Git commands outside the project folder

You might see:

fatal: not a git repository (or any of the parent directories): .git

Why it breaks:

Your terminal is in a folder that isn't a Git repository.

Fix:

Move into the project folder, then run the command again:

cd git-practice

Run pwd to see which folder you're in.


Mistake 3: Running git commit without -m

Git opens a text editor and waits for a commit message. If it's Vim and you don't know how to use it, press Esc, type :cq, and press Enter. This quits Vim with an error, so Git cancels the commit even if a message is already filled in. If it's nano, a common default on Ubuntu and Debian, press Ctrl+X to exit. In Notepad or VS Code, Git waits until you close the file, so close it without typing a message to cancel the commit. Nothing is lost. Then run the commit again with -m "Your message".


Troubleshooting

If git init prints hints about master, you skipped init.defaultBranch. Rename the branch with git branch -m main.

If git status says nothing to commit, working tree clean, everything is already committed.

If git add reports did not match any files, run ls to list the files in the folder, and check the file name's spelling.


Quick recap

  • Install Git, then check it with git --version.
  • Set your name, email, and default branch once with git config --global.
  • Turn a folder into a repository with git init.
  • Check what changed with git status.
  • Stage with git add, save with git commit -m "message".
  • See your history with git log --oneline.

Next steps

Your commits only exist on your computer so far. Next, push your project to GitHub so it's backed up online. Then you'll learn to create a branch and merge it.

To get more out of Git, write code worth saving. Mimo's Full-Stack Developer and Front-End Developer paths teach you to code and have you build portfolio projects on GitHub. See all beginner guides in the Git and GitHub tutorials.