Staging Changes for Commit
Before you can commit your changes to the Git repository, you need to stage them. The staging area is a crucial concept in Git, as it allows you to selectively choose which changes you want to include in the next commit.
Tracking Changes
To track the changes in your working directory, you can use the git status
command. This will show you which files have been modified, added, or deleted.
$ git status
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
modified: src/main.py
Untracked files:
(use "git add <file>..." to include in what will be committed)
new_file.txt
Adding Changes to the Staging Area
To add changes to the staging area, use the git add
command. You can stage individual files, directories, or use the .
wildcard to stage all changes in the current directory.
$ git add README.md
$ git add src/main.py
$ git add .
You can verify the changes in the staging area using git status
again:
$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: README.md
modified: src/main.py
new file: new_file.txt
Removing Changes from the Staging Area
If you've accidentally added a file to the staging area or want to remove a change, you can use the git restore
command to unstage it.
$ git restore --staged new_file.txt
$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: README.md
modified: src/main.py
Untracked files:
(use "git add <file>..." to include in what will be committed)
new_file.txt
Understanding the staging area is crucial for managing your Git commits effectively. In the next section, we'll explore how to amend staged commits.