LEARNGIT.IO
Home/Fundamentals of Git/The Commit Workflow
Fundamental Operations

The Commit Workflow

Finally, your project files are being by Git and you're ready to put in some serious coding work. In this lesson, you'll learn the fundamental Git workflow that allows you to record code changes to your repository. We'll review how to move files between the states (modified, committed, staged), and how to periodically save snapshots of your work.

The Commit Workflow

As you work on your project, you'll inevitably edit existing files, create new files, and delete old ones. These actions may seem obvious, but when using version control, these modifications are precisely the ones you want to track.

With Git, rather than worrying about each individual action, you simply work on your project as normal: start developing features, fixing bugs, etc., but when you get to a stopping point (for example: you fixed a bug, or you finished the feature) your work to the Git repository for safe keeping.

1. Check File Status

As you edit, create and delete files, Git keeps track in the background. Use the git status command to get a list of all your changes:

git status
Output
On branch main
Changes not staged for commit:
modified: homepage.html
deleted: styles.css
Untracked files:
about_me.html
profile_pic.jpg

In the output, you'll notice Git knows which files have been modified, created and deleted. In this example, I've been working on a website and I've added about_me.html and profile_pic.jpg, modified homepage.html, and deleted styles.css.

Recall from the previous lesson that new files are not yet tracked by Git, thus about_me.html and profile_pic.jpg are marked as Untracked files. This makes sense; without a previous version of the file for reference, Git would not know if a file has been modified or deleted, it simply knows this is the first time it's seeing the file.

2. Stage files

Now that you've gotten to a good stopping point in your project, you can start the process of recording your changes to your Git repository. Start by any files you'd like to include in your snapshot with the . This action moves the files from the working directory to the :

git add homepage.html styles.css about_me.html profile_pic.jpg

The action of moving files to the staging area is called staging; thus, we would say that we've "staged the files." After staging your file changes, you can run git status again to confirm:

git status
Output
On branch main
Changes to be committed:
new file: about_me.html
new file: profile_pic.jpg
deleted: styles.css
modified: homepage.html

You can see there is now a section titled "Changes to be committed," which tells you which files are in your staging area.

Note:

Stage multiple files at once

Instead of listing each file individually, stage ALL untracked/modified files in the working directory using the . wildcard:

git add .

Make a staging mistake?

If you accidentally added a file to the staging area and want to move it back into your working directory (i.e. unstage the file), simply use the . For example, to unstage profile_pic.jpg, run:

git restore --staged profile_pic.jpg

3. Commit

Now that the changes are moved to the staging area, tell Git to take a snapshot of that staging area, bundle those file changes in a , and save it to the repository. To do this, use the git commit command.

git commit -m 'Added an About page'

Remember to pass a descriptive commit message for future posterity using the -m flag. Commit messages (along with other metadata, like author and date) are attached to each commit. This extra information makes it easy to discern which changes were introduced by the commit. Git-based cloud hosting providers like GitHub also use this information to display project history.

Was this lesson helpful?