3. Basic Git Workflow
Understand Git areas
Section titled “Understand Git areas”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:
You edited app.js. It sits in your working directory, untracked by the next commit.
Here’s the same workflow, command by command.
The working directory
Section titled “The working directory”Checking repository status (git status)
Section titled “Checking repository status (git status)”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:
git statusThe staging area
Section titled “The staging area”Staging changes (git add)
Section titled “Staging changes (git add)”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:
git add <file1> <file2>Or to stage everything at once:
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:
git diff # changes you have not staged yetgit diff --staged # changes already staged for the next commitFor 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:
git add -pThe local repository
Section titled “The local repository”Committing changes (git commit)
Section titled “Committing changes (git commit)”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:
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
Section titled “The remote repository”Pushing changes (git push)
Section titled “Pushing changes (git push)”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:
git push origin <branch-name>If you want to fetch the latest changes from the remote repository without merging them automatically, use:
git fetch origin <branch-name>To fetch and merge the latest changes in one step, use:
git pull origin <branch-name>We dedicate all of Chapter 5 to working with remotes.
Viewing commit history (git log)
Section titled “Viewing commit history (git log)”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:
git logThis 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 cd918fd09e0014eefbcf9516a6ad99c431315838Author: Initial Commit <Example@domain.com>Date: Mon Feb 23 10:27:34 2022 -0500
Initial commitEnhancing git log with useful options
Section titled “Enhancing git log with useful options”The git log command supports various options to customize the output and help you quickly find the information you need.
One-line summary
Section titled “One-line summary”If you want a more concise view, you can display each commit as a single line:
git log --onelineThe result would be:
e98d9c8 Fix typo on <file3>8f0cd01 Add more content on <file2>299a903 Update <file2>8aca6fa Add a new fileFiltering by author
Section titled “Filtering by author”git log --author="Bob"Filtering by date
Section titled “Filtering by date”git log --since="2 weeks ago"Or
git log --since="2024-01-01" --until="2024-02-01"Checking the changes
Section titled “Checking the changes”To see the differences introduced in each commit:
git log -pThere are many other log options that you can find in the official documentation here: More on Git logs.