|
1 | 1 | ---
|
2 |
| -Title: 'Add' |
3 |
| -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.' |
| 2 | +Title: 'add' |
| 3 | +Description: 'Stages changes from the working directory to the staging area for the next commit' |
4 | 4 | Subjects:
|
5 | 5 | - 'Bash/Shell'
|
| 6 | + - 'Code Foundations' |
6 | 7 | - 'Developer Tools'
|
7 | 8 | Tags:
|
| 9 | + - 'Command Line' |
| 10 | + - 'Files' |
8 | 11 | - 'Git'
|
9 | 12 | - 'Version Control'
|
10 | 13 | CatalogContent:
|
11 |
| - - 'learn-the-command-line' |
12 | 14 | - 'learn-git'
|
| 15 | + - 'learn-the-command-line' |
13 | 16 | ---
|
14 | 17 |
|
15 |
| -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. |
| 18 | +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. |
| 19 | + |
| 20 | +## Syntax of `git add` |
| 21 | + |
| 22 | +```pseudo |
| 23 | +git add [options] [pathspec] |
| 24 | +``` |
| 25 | + |
| 26 | +### Common Syntax Variations |
| 27 | + |
| 28 | +```pseudo |
| 29 | +git add <filename> |
| 30 | +git add . |
| 31 | +git add -A |
| 32 | +git add --all |
| 33 | +``` |
| 34 | + |
| 35 | +**Parameters:** |
| 36 | + |
| 37 | +- `<filename>`: Specifies the exact file to stage for the next commit |
| 38 | +- `.`: Stages all changes in the current directory and its subdirectories |
| 39 | +- `-A` or `--all`: Stages all changes in the entire repository, including new, modified, and deleted files |
| 40 | +- `-u` or `--update`: Stages only modified and deleted files (excludes new files) |
| 41 | +- `-p` or `--patch`: Begins interactive staging mode to selectively stage parts of files |
| 42 | +- `-i` or `--interactive`: Opens interactive mode for choosing which changes to stage |
| 43 | +- `-f` or `--force`: Forces staging of ignored files |
| 44 | +- `-n` or `--dry-run`: Shows what would be staged without actually staging anything |
| 45 | + |
| 46 | +**Return value:** |
| 47 | + |
| 48 | +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. |
16 | 49 |
|
17 |
| -## Example |
| 50 | +## Example 1: Basic File Staging using `git add` |
18 | 51 |
|
19 |
| -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: |
| 52 | +This example demonstrates how to stage individual files for the next commit: |
20 | 53 |
|
21 | 54 | ```shell
|
22 |
| -$ git status |
| 55 | +# Create a new file in the project |
| 56 | +echo "Hello, Git!" > welcome.txt |
| 57 | + |
| 58 | +# Check the current status |
| 59 | +git status |
| 60 | + |
| 61 | +# Stage the new file |
| 62 | +git add welcome.txt |
| 63 | + |
| 64 | +# Verify the file is staged |
| 65 | +git status |
| 66 | +``` |
23 | 67 |
|
24 |
| -On branch main |
| 68 | +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. |
25 | 69 |
|
26 |
| -No commits yet |
| 70 | +## Example 2: Using `git add` for Team Collaboration Workflow |
27 | 71 |
|
28 |
| -Untracked files: |
29 |
| - (use "git add <file>..." to include in what will be committed) |
30 |
| - message.txt |
| 72 | +This example shows how to stage multiple files when working on a feature with a team: |
31 | 73 |
|
32 |
| -nothing added to commit but untracked files present (use "git add" to track) |
| 74 | +```shell |
| 75 | +# Working on a new feature - multiple files modified |
| 76 | +# Modified files: app.js, style.css, README.md |
| 77 | +# New file: config.json |
| 78 | + |
| 79 | +# Stage specific files for a focused commit |
| 80 | +git add app.js config.json |
| 81 | + |
| 82 | +# Check what's staged vs unstaged |
| 83 | +git status |
| 84 | + |
| 85 | +# Stage remaining files for a documentation update |
| 86 | +git add style.css README.md |
| 87 | + |
| 88 | +# Commit the changes with descriptive messages |
| 89 | +git commit -m "Add user authentication feature" |
| 90 | + |
| 91 | +# Push to share with the team |
| 92 | +git push origin feature-branch |
33 | 93 | ```
|
34 | 94 |
|
35 |
| -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: |
| 95 | +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. |
| 96 | + |
| 97 | +### Example 3: Staging All Changes With `git add --all` |
| 98 | + |
| 99 | +This example demonstrates how to stage all changes in the repository at once using `git add --all`: |
36 | 100 |
|
37 | 101 | ```shell
|
38 |
| -$ git add message.txt |
39 |
| -$ git status |
| 102 | +# Working directory with multiple changes: |
| 103 | +# Modified: index.html, app.js |
| 104 | +# New files: contact.html, footer.css |
| 105 | +# Deleted: old-style.css |
40 | 106 |
|
41 |
| -On branch main |
| 107 | +# Check current repository status |
| 108 | +git status |
42 | 109 |
|
43 |
| -No commits yet |
| 110 | +# Stage all changes at once (new, modified, deleted) |
| 111 | +git add --all |
44 | 112 |
|
45 |
| -Changes to be committed: |
46 |
| - (use "git rm --cached <file>..." to unstage) |
47 |
| - new file: message.txt |
| 113 | +# Verify all changes are staged |
| 114 | +git status |
| 115 | + |
| 116 | +# Alternative shorter syntax (same result) |
| 117 | +git add -A |
| 118 | + |
| 119 | +# Commit all staged changes |
| 120 | +git commit -m "Complete website redesign with new contact page" |
48 | 121 | ```
|
49 | 122 |
|
50 |
| -The file now appears in green when running `git status`, indicating that it is staged and will be saved with the next commit. |
| 123 | +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 the next commit. |
| 124 | + |
| 125 | +## Frequently Asked Questions |
| 126 | + |
| 127 | +### 1. How do I add files to git? |
| 128 | + |
| 129 | +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. |
| 130 | + |
| 131 | +### 2. What to use instead of git add? |
| 132 | + |
| 133 | +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). |
51 | 134 |
|
52 |
| -There are many extensions to the `git add` command such as: |
| 135 | +### 3. Does git add include deleted files? |
53 | 136 |
|
54 |
| -- `-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. |
55 |
| -- `-f`: Which stages all files including those which are ignored. |
56 |
| -- `.` : Which stages all the files in the current directory. On the command line, it references the current directory. |
57 |
| -- `-A`: Which stages all the files in the current directory as well as subdirectories. |
| 137 | +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. |
0 commit comments