Skip to content

3. Basic Git Workflow

Git is designed to work with multiple areas, each serving a specific purpose in the version control process. Understanding these areas will give you deeper insight into what happens when you execute Git commands.

Press the button below to watch a change travel from your working directory all the way to the remote:

1 / 4
Step 1 of 4

You edited app.js. It sits in your working directory, untracked by the next commit.

Working Directory
📄 app.js
Staging Area
Local Repository
Remote Repository
Press enter or space to select a node. You can then use the arrow keys to move the node around. Press delete to remove it and escape to cancel.
Press enter or space to select an edge. You can then press delete to remove it or escape to cancel.

Here’s the same workflow, command by command.

The working directory contains all the files in your project, both tracked and untracked. Tracked files are those already under Git’s version control, while untracked files are newly created files that Git does not yet manage.

Any file that exists in the working directory but has not been added to Git is considered untracked. This allows you to control which files should be included in version control.

To check the status of your working directory, use:

Terminal window
git status

Before committing changes, you must first move them to the staging area. This step allows you to carefully prepare what will be included in your next commit. It helps in organizing changes logically rather than committing everything at once.

To add files to the staging area, use:

Terminal window
git add <file1> <file2>

Or to stage everything at once:

Terminal window
git add .

Reviewing changes before you stage (git diff)

Section titled “Reviewing changes before you stage (git diff)”

It’s worth looking at exactly what changed before you stage it. git diff shows your unstaged changes (working directory vs. staging area), while git diff --staged shows what you’ve already staged, which is precisely what the next commit will contain:

Terminal window
git diff # changes you have not staged yet
git diff --staged # changes already staged for the next commit

For finer control, git add -p walks you through each change “hunk” and asks whether to stage it. This is how you split a messy working directory into small, logical commits:

Terminal window
git add -p

Once files are staged, they are ready to be committed to your local repository. A commit is a snapshot of your project at a specific point in time, along with a meaningful message that describes the changes.

To commit staged files, use:

Terminal window
git commit -m "Your commit message"

At this stage, the changes are recorded in the local repository but are not yet shared with others.

The remote repository is where your project is stored online, making it accessible to other team members. It typically resides on platforms like GitHub, GitLab, or Bitbucket.

To upload your commits from the local repository to the remote repository, use:

Terminal window
git push origin <branch-name>

If you want to fetch the latest changes from the remote repository without merging them automatically, use:

Terminal window
git fetch origin <branch-name>

To fetch and merge the latest changes in one step, use:

Terminal window
git pull origin <branch-name>

We dedicate all of Chapter 5 to working with remotes.

As you work with Git, you may need to review past commits to track changes, find specific modifications, or simply understand the evolution of a project.

To view the commit history, use:

Terminal window
git log

This is the actual result of this command:

commit 85c035458122ca9f90a56fc2fa167bb61d22580b (HEAD -> main)
Author: Initial Commit <Example@domain.com>
Date: Mon Feb 23 10:39:12 2022 -0500
Added sample text to README.txt
commit cd918fd09e0014eefbcf9516a6ad99c431315838
Author: Initial Commit <Example@domain.com>
Date: Mon Feb 23 10:27:34 2022 -0500
Initial commit

The git log command supports various options to customize the output and help you quickly find the information you need.

If you want a more concise view, you can display each commit as a single line:

Terminal window
git log --oneline

The result would be:

e98d9c8 Fix typo on <file3>
8f0cd01 Add more content on <file2>
299a903 Update <file2>
8aca6fa Add a new file
Terminal window
git log --author="Bob"
Terminal window
git log --since="2 weeks ago"

Or

Terminal window
git log --since="2024-01-01" --until="2024-02-01"

To see the differences introduced in each commit:

Terminal window
git log -p

There are many other log options that you can find in the official documentation here: More on Git logs.