Skip to content

First Commit Solution


git add .
git commit -m "first commit"

Explanation

Working Directory

The explanation below assumes that your working directory is the root of the repo. In other words, if you run

bill@gates:~$ pwd

it should return

bill@gates:~$ pwd
/path/to/yolo

Otherwise, you need to

bill@gates:~$ cd /path/to/yolo

Status

Before we stage and commit the changes, let's review the state of our git repo using git status.

bill@gates:~$ git status
On branch main

No commits yet

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

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

git status tells us that we have an untracked file - foo.txt.

Untracked Files

Untracked files are files that have been created within your repo's working directory but have not yet been added to the repository's tracking index using git add.

Stage

Before we can commit our changes, we need to stage them with git add.

bill@gates:~$ git add foo.txt
bill@gates:~$ git add .

Let's run git status again..

bill@gates:~$ git status
On branch main

No commits yet

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

Now, instead of "Untracked files:" we have "Changes to be committed:".

Commit

To commit our staged changes, we can use git commit.

bill@gates:~$ git commit -m "first commit" # (1)!
[main (root-commit) 88d8887] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 foo.txt
  1. -m is an alias for the --message argument to git commit. It should be a meaningful message regarding your changes in the commit.

What if I don't want to specify a message?

Shame! 🔔 You should always provide a commit message!

Log

Finally, let's check our commit history with git log.

bill@gates:~$ git log
commit 88d8887beaad2cfd37d04fb00f38c05077581057 (HEAD -> main)
Author: bill123 <billgates@gmail.com>
Date:   Sun Sep 11 13:09:50 2022 -0500

    first commit