Making your first commit

With the explanation and background context covered in the previous pages, these next few sections will walk you through working with a Git repository.

First, make sure you have git. You can do this by running git --versionin your terminal. If you get an error message, you don't have it, or it's not set up properly and you should get it from here. As with any tool you install, don't forget to add it to your PATH.

To start, make a new directory and run git initinside of it

$ mkdir repo
$ cd repo
$ git init

Next, let's add a simple Hello World in Python

hello.py
print("Hello, World!")

Git strongly prefers that your files end with a blank new line. The code samples here won't show them, but in your IDE, be sure to do so, Otherwise, you might see subtle complaints from Git in various places.

Now let's see what Git has to say

$ git status
# On branch master
# 
# No commits yet
# Untracked files: 
#   (use "git add <file>..." to include in what will be committed)
#     hello.py
# 
# nothing added to commit but untracked files present (use "git add" to track) 

Let's break this down:

On branch master

Git will always start by reminding you what branch you're on. In this case, that's master, but convention is actually to use main . Let's create that:

$ git switch --create main
# Switched to a new branch 'main'

Moving on:

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

Git won't touch any files it doesn't already know about (called tracked files), so let's add our new file:

$ git add hello.py
$ git status
# On branch main
# 
# No commits yet
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#         new file:   hello.py

Now we're on the mainbranch, and we no longer have a warning about untracked files. Now Git is ready to commit, so let's make a new commit with a message:

$ git commit --message "Added Hello World"
# [main (root-commit) e96ab34] Added Hello World
#  1 file changed, 1 insertion(+)
#  create mode 100644 hello.py

The output here confirms your branch, commit ID (also called the commit hash), message, and what changed. Running git statusconfirms that our commit went through and that we're clear to start making more changes:

$ git status
# On branch main
# nothing to commit, working tree clean

Now that you've set up your repository and branch, the process will be the same for future changes: make changes, git addthe files, then git commitwith a message. Let's review that last commit

$ git show
# commit e96ab344b7b1a8499fcb01f9f39f943c31dc111e (HEAD -> main)
# Author: Levi Lesches <levilesches@gmail.com>
# Date:   Fri Apr 4 02:46:55 2025 -0400
# 
#     Added Hello World
# 
# diff --git a/hello.py b/hello.py
# new file mode 100644
# index 0000000..7df869a
# --- /dev/null
# +++ b/hello.py
# @@ -0,0 +1 @@
# +print("Hello, World!")

This has a lot of output, but you can clearly see the commit ID, author, date, message, which files were edited and even the diff for each file.

Last updated

Was this helpful?