What you need before you start

Git records changes to files over time. A repository is a project folder where Git keeps that history. You can learn the basic workflow entirely on your computer; you do not need a hosting account or an internet connection to make a local commit.

For the commands in this guide, use Git 2.23 or newer, a terminal, and a text editor. Open your terminal and enter git --version to check your installation. The examples assume your terminal is already open in the folder you want to track. If you are working on an existing project, choose its main folder rather than a folder that contains several unrelated projects.

Initialize the repository

In your project folder, run git init. Git creates a hidden .git directory there to store repository information. Your project files remain where they are. The folder is now a repository, but initializing it does not automatically save a version of your files.

Next, run git status. This command reports which files Git has not started tracking and which changes are ready to commit. At this point, a new project may have no files to report. Use your text editor to create a file named README.md in the project folder and add a short description, such as “A small project for learning Git.” Run git status again. Git should list README.md as an untracked file.

You might see a branch name such as main or master in the status output. The initial branch name depends on your Git configuration, so you do not need to change it to follow this guide. A branch is a line of project history; for now, you only need to know that your first commit will be saved on the current branch.

Stage the file you want to save

Staging lets you choose what goes into the next commit. Run git add README.md, then run git status again. README.md should now appear under changes to be committed. Staging is not the same as committing: it prepares a snapshot of the file, but it has not yet added that snapshot to the project history.

Before committing, review what you staged with git diff --cached. For this new file, Git displays its contents as an addition. If you staged README.md by mistake, run git restore --staged README.md to remove it from the staging area without deleting your file. You can edit it and stage it again whenever you are ready.

It is useful to name files explicitly while learning. The command git add . stages changes throughout the current directory and its subdirectories, which may include files you did not mean to save. Check git status and review staged changes before each commit, especially if your project contains local settings, generated files, or credentials.

Make and check your first commit

A commit saves the staged snapshot with a message explaining the change. First, Git may need a name and email address to attach to your commits. If it asks for them, set them for this repository with git config --local user.name "Your Name" and git config --local user.email "you@example.com". Replace those examples with the identity you want recorded. These settings identify the commit author; they do not sign you in to a hosting service.

Run git commit -m "Add project introduction" to commit the staged README.md file. A useful commit message describes what changed, so you can understand the history later. If Git says there is nothing to commit, run git status and confirm that a file is staged. If you edited README.md after staging it, run git add README.md again to include your latest edits in the commit.

Finally, run git status. If you have made no further changes, it should report a clean working tree. Run git log -1 --oneline to see your newest commit summarized with a short identifier and its message. You have now completed the core local workflow: change a file, stage the version you want to save, and commit it. Repeat those steps as your project grows.

Back to category ↗