Skip to content
Laviyx Learn

Try

Git and GitHub course menu

git init, add and commit: Your First Git Repository

Create a Git repository with git init, check its state with git status, stage files with git add, save a snapshot with git commit, and learn the basic Git workflow step by step.

Beginner7 min readUpdated
On this page

In this lesson you will create a repository and make your first commits. The same small project is used in many lessons: a folder called notes.

Create a repository#

Make a folder and turn it into a repository:

Terminal
mkdir notes
cd notes
git init
Output
Initialized empty Git repository in /home/you/notes/.git/

git init creates a hidden .git folder. That folder is the repository: it stores the history, branches and settings. Deleting it removes the history but leaves your files.

Terminal
ls -a
Output
.
..
.git

Check the state with git status#

git status is the command you will run most. It changes nothing; it only reports.

Terminal
git status
Output
On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)

Add a file and look again:

Terminal
echo "Buy milk" > notes.txt
git status
Output
On branch main

No commits yet

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

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

The file is untracked: it is in the folder, but Git is not recording it yet.

Stage the file#

git add puts a file on the list for the next commit. This list is called the staging area.

Terminal
git add notes.txt
git status
Output
On branch main

No commits yet

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

Commit#

git commit saves everything on that list as a permanent snapshot. -m gives the message:

Terminal
git commit -m "Add shopping note"
Output
[main (root-commit) 75e182b] Add shopping note
 1 file changed, 1 insertion(+)
 create mode 100644 notes.txt

The first line shows the branch, a short ID for this commit, and your message. root-commit means it is the very first commit in the repository.

The basic cycle#

Edit, stage, commit. Do it again with a change:

Terminal
echo "Call the bank" >> notes.txt
git status
Output
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:   notes.txt

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

Git sees that a tracked file was modified. Stage and commit it:

Terminal
git add notes.txt
git commit -m "Add reminder to call the bank"
git status
Output
[main 6b74313] Add reminder to call the bank
 1 file changed, 1 insertion(+)
On branch main
nothing to commit, working tree clean

nothing to commit, working tree clean means every change is saved. Now see the history:

Terminal
git log --oneline
Output
6b74313 Add reminder to call the bank
75e182b Add shopping note

Two commits, newest first. You now know the basic loop you will use every day:

Text
edit files  -->  git add  -->  git commit

Adding many files#

CommandWhat it stages
git add notes.txtOne file
git add docs/A whole folder
git add .Everything changed in this folder and below

Common mistakes#

Practice#

Practice exercise

Create a repository called recipes. Add a file tea.txt containing Boil water, and commit it. Then add a second line, Add tea leaves, commit that too, and print the history in one-line format.

Show hint

Use echo with > for the first line and >> to add the second.

Show solution
Terminal
mkdir recipes
cd recipes
git init -q
echo "Boil water" > tea.txt
git add tea.txt
git commit -q -m "Add tea recipe"
echo "Add tea leaves" >> tea.txt
git add tea.txt
git commit -q -m "Add second step"
git log --oneline
Output
f7083ec Add second step
4ee4f8c Add tea recipe

-q (quiet) hides the usual messages. It is used here only to keep the output short.

Key takeaway#

Next: The Staging Area.

Finished this lesson?