How to Add Files in Git
-
Add All Files Using the
git add
Command in Git - Add Files by Extension in Git
- Add Deleted and Modified Files Only in Git
This article will discuss the different methods to add files to our repository on Git.
Add All Files Using the git add
Command in Git
We can add all files without exception with the git add
command as shown below.
git add -A
The -A
argument instructs Git to add all Files present on your index. Git will stage untracked, modified, and deleted files for commit.
Alternatively, you can run the git add
command, which instructs Git to stage the files in your current directory.
Example:
pc@JOHN MINGW64 ~/Git/Delftscopetech (main)
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: README.md
modified: Sample.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
Sample.txt.bak
Tutorial.txt
no changes added to commit (use "git add" and/or "git commit -a")
In the example above, we have an index with the following files.
- A modified
Sample.tx
file. - A deleted
README.md
file. - And two untracked files.
We run the git add -A
command to stage all the files for commit, as shown below.
git add -A
Let us run the git status
command to check if Git staged our files.
pc@JOHN MINGW64 ~/Git/Delftscopetech (main)
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: README.md
modified: Sample.txt
new file: Sample.txt.bak
new file: Tutorial.txt
Our files are ready to commit.
Add Files by Extension in Git
At times, you may need stage-specific file formats. For example a .js
file or .txt
file.
We use the git add
command to throw in a wildcard and the file extension. Below is an example.
We have a JavaScript file we want to stage for commit in our directory.
pc@JOHN MINGW64 ~/Git/Delftscopetech (main)
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: README.md
modified: Sample.txt
new file: Sample.txt.bak
new file: Tutorial.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
New Text Document.txt
New.js
We can run the git add
command in the following context.
$ git add *.js
Let us run the git status
command to check if Git staged the file.
pc@JOHN MINGW64 ~/Git/Delftscopetech (main)
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: New.js
deleted: README.md
modified: Sample.txt
new file: Sample.txt.bak
new file: Tutorial.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
New Text Document.txt
As you can see, our New.js
file was staged for commit.
Add Deleted and Modified Files Only in Git
We use the git add
command with the -u
argument to instruct Git to add only the deleted and modified files for commit.
Example:
pc@JOHN MINGW64 ~/Git/Delftscopetech (main)
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: New.js
deleted: README.md
modified: Sample.txt
new file: Sample.txt.bak
new file: Tutorial.txt
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: Sample.txt
deleted: Sample.txt.bak
We run the command below if we want to stage our two files.
$ Git add -u
We’ll run the git status
command to check if Git staged the two files.
pc@JOHN MINGW64 ~/Git/Delftscopetech (main)
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: New.js
deleted: README.md
modified: Sample.txt
new file: Tutorial.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
New Text Document.txt
We have added the deleted and modified files for commit.
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedIn