git add
git add
: add files to the staging area.
The git add
command is used in Git to add changes in the workspace to the staging area. It tells Git that you want to include updates to particular file(s) in the next commit. However, git add
doesn't affect the repository until you commit the changes. You can add multiple files at once or use patterns to add all files of a certain type. You can also interactively add parts of a file to the staging area.
Staging is a step before the commit process in Git's version control. It allows you to select and prepare files for a commit, rather than committing all the changes you've made since the last commit. This is useful for organizing your changes into manageable groups before recording them in the project's history.
git add
is especially useful in scenarios where you've made changes to more than one file and want to commit them separately. By adding only specific files to the staging area, you can control exactly how your project's history will be documented in increments.
For example, if you added a new feature in one file and updated the styling in another, you can stage these changes separately to create two distinct commits. If later you want to revert the styling changes, you can do so without affecting the new feature.
Examples
Add a specific file to Git's staging area:
git add README.md
Add all changes in the current directory (including new files and changes to existing files). The .
character points to the current directory, meaning "all changes in the current directory":
git add .
Add all .txt
files in the current directory:
git add *.txt
Select specific pieces of changes to be added to the staging area interactively:
git add --patch
Or the shorter form:
git add -p
Unstage a file that was previously added to the staging area:
git restore --staged README.md
Hi! I'm Alex, creator of GitByBit.
And this is Gitopedia—a reference that summarizes knowledge in my Git course.
Gitopedia is designed to be a personal knowledge base. When you progress through the main Git course in VS Code, you unlock all of these topics, one by one, bit by bit. This gives you time to digest the information and gradually build the mental model of Git. Here's an example of how it works:
Learning directly in VS Code lets you practice Git exactly as you would in real life, doing real jobs, writing real code. In addition, the course has access to your actual terminal, so it can point out mistakes, suggest workarounds, etc.
The course is FREE, there are no Ads or other bullshit. There are optional premium add-ons you can purchase, mainly to support my work (regrettably, I have to eat every day), but that's totally up to you.