JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Git >

Adding files in Git

Author:JIYIK Last Updated:2025/03/27 Views:

This article will discuss different ways to add files to our repository on Git.


In Git, use git addthe command to add all files

We can git addadd all the files without exception using the command as shown below.

git add -A

-AThe -p parameter instructs Git to add all files present in the index. Git will stage untracked, modified, and deleted files for commit.

Alternatively, you can run git addthe command, which instructs Git to stage the files in the 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 above example, we have an index with the following files.

  1. Modified Sample.txfile.
  2. Deleted README.mdfiles.
  3. There are two untracked files.

We run git add -Athe command to stage all the files for commit as shown below.

git add -A

Let's run git statusthe command to check if Git has 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 documents are ready for submission.


Adding files by extension in Git

Sometimes you may need a stage-specific file format. For example, .jsa .txt file or .txta .txt file.

We use git addthe command to enter wildcards and file extensions. Below is an example.

We have a JavaScript file that we want to stage in our directory for commit.

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 command in the following context git add.

$ git add *.js

Let's run git statusthe command to check if Git stages the 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:   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.jsfile is ready for submission.


Add only deleted and modified files in Git

We use the command with -uthe -p parameter git addto instruct Git to add only deleted and modified files to 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

If we want to stage our two files, we run the following command.

$ Git add -u

We will run git statusthe command to check whether Git has staged these 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 to commit.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Git synchronizes branches with Master

Publish Date:2025/03/27 Views:61 Category:Git

Git is a well-organized distributed version control system that can be very effective in managing source code in a distributed team of developers. When different developers are working on different product features, once they have completed

Setting Upstream in Git

Publish Date:2025/03/26 Views:152 Category:Git

In this article, we will learn how to use in Git upstream . When using Git by cloning and creating new repositories in branches, we must set up upstream branches for future commits and fetches. But first, we should understand what is upstre

Add Git to PATH on Windows

Publish Date:2025/03/26 Views:92 Category:Git

Git is a free and open source version control system designed to work with projects quickly and efficiently. You can use it on Windows, Mac, and Linux operating systems. This article explains how to add git to your Windows PATH environment

Open a file in Git Bash

Publish Date:2025/03/26 Views:68 Category:Git

We cannot use open in Git Bash. If you try to open a file using open on Git Bash, you will get the error bash: open: command not found . This short article explains how to open a file on Git Bash for Windows. Open a file in Git Bash We can

Creating Git patches from uncommitted changes

Publish Date:2025/03/26 Views:91 Category:Git

This article explains how we can create a Git patch from uncommitted changes in our working directory. This is handy when we want to create a patch without creating a commit. Creating Git patches from uncommitted changes We will use an exam

How to uninstall git in Windows

Publish Date:2025/03/26 Views:187 Category:Git

Git for Windows is a well-known and user-friendly software that provides a smooth platform to easily use Git in a team-friendly environment. Sometimes we want to get rid of some software because of space reasons, or the software is not comp

How to check the Git version

Publish Date:2025/03/26 Views:130 Category:Git

Git is primarily known as a unique free source code distributed version control system that can easily store all kinds of projects, big or small. Any new developer or someone who is new to it can understand it quickly and easily. We can ins

How to rename a local branch in Git

Publish Date:2025/03/26 Views:66 Category:Git

Git is a widely known, in-demand, and popular version control system (VCS) that is commonly used by software developers to collaborate on code development in small or large teams. It helps us coordinate our work and helps us track the chang

Rename files and directories in a Git repository

Publish Date:2025/03/26 Views:199 Category:Git

In this article, we will discuss the rename process in git. We use Git Rename to change the name of files and folders in the working directory. The renaming process involves the git mv command. This command accepts two parameters: target an

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial