Course Tonight

Course Tonight

Did You Know?

We design Docy for the readers, optimizing not for page views or engagement

Chapter 4: Staging

Estimated reading: 3 minutes 6 views

Section 4.1: Staging All Changes to Files

To stage all changes to files in the current directory and its subdirectories, you can use either of the following commands:

git add -A

or

git add .

In Git version 2.x, both commands will stage all changes. However, in version 1.x, git add . will only stage new and modified files, not deleted files. To stage all changes in any version of Git, it is recommended to use git add -A or git add --all.

Section 4.2: Unstage a File that Contains Changes

To unstage a file that contains changes, you can use the following command:

git reset <filePath>

Replace <filePath> with the path to the file you want to unstage.

Section 4.3: Add Changes by Hunk

You can interactively select and stage changes by hunk using the following command:

git add -p

or

git add --patch

This command opens an interactive prompt that displays the diffs and allows you to choose whether to include each hunk or not. You can selectively stage changes, split hunks, or even manually edit the diff. This is helpful for reviewing and staging specific changes.

You can also access this interactive mode by using the command git add --interactive and selecting the “p” option.

Section 4.4: Interactive Add

The git add -i or git add --interactive command provides an interactive interface where you can edit the index to prepare the changes you want to include in the next commit. It allows you to add and remove changes to files, add untracked files, remove files from being tracked, and select specific sections of changes to stage. This feature is useful for separating changes into separate commits or splitting a large commit during an interactive rebase.

Section 4.5: Show Staged Changes

To display the hunks that are staged for commit, you can use the following command:

git diff --cached

This command shows the differences between the staged changes and the previous commit.

Section 4.6: Staging a Single File

To stage a single file for committing, use the following command:

git add <filename>

Replace <filename> with the name of the file you want to stage.

Section 4.7: Stage Deleted Files

To stage a file deletion, you can use the following command:

git rm <filename>

This command removes the file from the Git repository and stages the deletion. If you want to keep the file on disk and only remove it from Git, you can use the --cached flag:

git rm --cached <filename>

Leave a Comment

Share this Doc

Chapter 4: Staging

Or copy link

CONTENTS