-
Notifications
You must be signed in to change notification settings - Fork 4.3k
[Edit] Git: Git add #7471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[Edit] Git: Git add #7471
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d37f25c
[Edit] Git: Git add
mamtawardhani bb0c3c1
Update content/git/concepts/add/add.md
avdhoottt 17f2eeb
Minor Codeblack change
avdhoottt d87043f
Update content/git/concepts/add/add.md
avdhoottt 2f1e715
Update content/git/concepts/add/add.md
avdhoottt 9f2ee33
Update content/git/concepts/add/add.md
avdhoottt 5416480
Update content/git/concepts/add/add.md
avdhoottt c4317bd
Update content/git/concepts/add/add.md
avdhoottt 156f86c
Merge branch 'main' into git-add
avdhoottt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,57 +1,137 @@ | ||
| --- | ||
| Title: 'Add' | ||
| Description: 'The git add command is used to add changes from the working tree to the staging area. This is where changes are saved for the next commit.' | ||
| Title: 'add' | ||
| Description: 'Stages changes from the working directory to the staging area for the next commit' | ||
| Subjects: | ||
| - 'Bash/Shell' | ||
| - 'Code Foundations' | ||
| - 'Developer Tools' | ||
| Tags: | ||
| - 'Command Line' | ||
| - 'Files' | ||
| - 'Git' | ||
| - 'Version Control' | ||
| CatalogContent: | ||
| - 'learn-the-command-line' | ||
| - 'learn-git' | ||
| - 'learn-the-command-line' | ||
| --- | ||
|
|
||
| In Git, adding is used to add changes from the working tree to the staging area. This is where changes are saved for the next commit. When the file name appears in green, it indicates that the file is tracked and will be staged for the next commit. | ||
| The **`git add`** command stages changes from the working directory to the staging area, preparing them to be included in the next commit. This essential Git command allows developers to selectively choose which modifications should be committed, providing precise control over version history and enabling organized, logical commits in any Git workflow. | ||
|
|
||
| ## Syntax of `git add` | ||
|
|
||
| ```pseudo | ||
| git add [options] [pathspec] | ||
| ``` | ||
|
|
||
| ### Common Syntax Variations | ||
|
|
||
| ```pseudo | ||
| git add <filename> | ||
| git add . | ||
| git add -A | ||
| git add --all | ||
| ``` | ||
|
|
||
| **Parameters:** | ||
|
|
||
| - `<filename>`: Specifies the exact file to stage for the next commit | ||
| - `.`: Stages all changes in the current directory and its subdirectories | ||
| - `-A` or `--all`: Stages all changes in the entire repository, including new, modified, and deleted files | ||
| - `-u` or `--update`: Stages only modified and deleted files (excludes new files) | ||
| - `-p` or `--patch`: Begins interactive staging mode to selectively stage parts of files | ||
| - `-i` or `--interactive`: Opens interactive mode for choosing which changes to stage | ||
| - `-f` or `--force`: Forces staging of ignored files | ||
| - `-n` or `--dry-run`: Shows what would be staged without actually staging anything | ||
|
|
||
| **Return value:** | ||
|
|
||
| The `git add` command returns no output on successful execution. If there are errors (such as attempting to stage non-existent files), Git displays appropriate error messages. Use [`git status`](https://www.codecademy.com/resources/docs/git/status) to verify which files have been successfully staged. | ||
|
|
||
| ## Example | ||
| ## Example 1: Basic File Staging using `git add` | ||
|
|
||
| Below is a short example of how the `git add` command works. Assuming a change was saved in a **message.txt** text file, running `git status` will show this: | ||
| This example demonstrates how to stage individual files for the next commit: | ||
|
|
||
| ```shell | ||
| $ git status | ||
| # Create a new file in your project | ||
| echo "Hello, Git!" > welcome.txt | ||
|
|
||
| # Check the current status | ||
| git status | ||
|
|
||
| # Stage the new file | ||
| git add welcome.txt | ||
|
|
||
| # Verify the file is staged | ||
| git status | ||
| ``` | ||
|
|
||
| On branch main | ||
| After running these commands, `welcome.txt` will be moved from "Untracked files" to "Changes to be committed" in the status output, confirming it's ready for the next commit. | ||
|
|
||
| No commits yet | ||
| ## Example 2: Using `git add` for Team Collaboration Workflow | ||
|
|
||
| Untracked files: | ||
| (use "git add <file>..." to include in what will be committed) | ||
| message.txt | ||
| This example shows how to stage multiple files when working on a feature with a team: | ||
|
|
||
| nothing added to commit but untracked files present (use "git add" to track) | ||
| ```shell | ||
| # Working on a new feature - multiple files modified | ||
| # Modified files: app.js, style.css, README.md | ||
| # New file: config.json | ||
|
|
||
| # Stage specific files for a focused commit | ||
| git add app.js config.json | ||
|
|
||
| # Check what's staged vs unstaged | ||
| git status | ||
|
|
||
| # Stage remaining files for a documentation update | ||
| git add style.css README.md | ||
|
|
||
| # Commit the changes with descriptive messages | ||
| git commit -m "Add user authentication feature" | ||
|
|
||
| # Push to share with the team | ||
| git push origin feature-branch | ||
| ``` | ||
|
|
||
| Since the file is untracked, `git add` can be used to stage it for the next commit. Afterwards, running `git status` again should yield the following: | ||
| This workflow demonstrates how `git add` enables developers to create logical, focused commits by staging related changes together, making code review and collaboration more effective. | ||
|
|
||
| ### Example 3: Staging All Changes With `git add --all` | ||
|
|
||
| This example demonstrates how to stage all changes in your repository at once using `git add --all`: | ||
avdhoottt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```shell | ||
| $ git add message.txt | ||
| $ git status | ||
| # Working directory with multiple changes: | ||
| # Modified: index.html, app.js | ||
| # New files: contact.html, footer.css | ||
| # Deleted: old-style.css | ||
|
|
||
| On branch main | ||
| # Check current repository status | ||
| git status | ||
|
|
||
| No commits yet | ||
| # Stage all changes at once (new, modified, deleted) | ||
| git add --all | ||
|
|
||
| Changes to be committed: | ||
| (use "git rm --cached <file>..." to unstage) | ||
| new file: message.txt | ||
| # Verify all changes are staged | ||
| git status | ||
|
|
||
| # Alternative shorter syntax (same result) | ||
| git add -A | ||
|
|
||
| # Commit all staged changes | ||
| git commit -m "Complete website redesign with new contact page" | ||
| ``` | ||
|
|
||
| The file now appears in green when running `git status`, indicating that it is staged and will be saved with the next commit. | ||
| After running `git add --all`, all modifications, new files, and deletions will be moved to the "Changes to be committed" section, making this command ideal for comprehensive updates when there is a need to include everything in your next commit. | ||
|
|
||
| ## Frequently Asked Questions | ||
|
|
||
| ### 1. How do I add files in git? | ||
|
|
||
| Use `git add <filename>` for single files, `git add .` for all changes in the current directory, or `git add -A` for all changes in the repository. Always verify with `git status` before committing. | ||
|
|
||
| ### 2. What to use instead of git add? | ||
|
|
||
| There's no direct replacement for `git add` since it's fundamental to Git's three-stage workflow. However, you can use `git commit -a` to automatically stage and commit tracked files (but this won't include new files). | ||
avdhoottt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| There are many extensions to the `git add` command such as: | ||
| ### 3. Does git add include deleted files? | ||
|
|
||
| - `-n`: Where the file will not be added to the staging area but will show whether or not the file exists and/or if it will be ignored. | ||
| - `-f`: Which stages all files including those which are ignored. | ||
| - `.` : Which stages all the files in the current directory. On the command line, it references the current directory. | ||
| - `-A`: Which stages all the files in the current directory as well as subdirectories. | ||
| The basic `git add .` command does include deleted files. Use `git add -A` to explicitly include all changes including deletions, or `git add -u` to stage only modifications and deletions while excluding new files. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.