There are circumstances when working with git that you may want to exclude certain files from being managed by git and for that purpose we generally create git ignore files, which is essentially a text-based file that developers can use to list specific files, file types and folders to be excluded from being added or managed by a git repository.
We will now learn how files are added to a git repository and understand what git does internally to track and manage these files.
Any files inside the working directory can be either known or unknown to Git. The files unknown to Git can be either untracked or ignored.
In how to create a git repository the
result of either a
git init or a
git clone results in the creation of a
.git folder, where git stores all the files it needs
to manage the repository. We also learned that we don't necessarily need to connect to a remote server to be able to work
with git, we are able to carry out all git based repository operations without the need to connect to a remote server. In most cases,
the only time you need to connect to remote repository is when you want to synchronise and share with other users.
We will now expand further on the work we did in how to create a git repository and we are going to add our first file to that repository.
Lets create our first file, which will be a simple readme providing some description and regarding our project. Initially we'll do this using terminal command, for brevity.
echo "# Geek.I.Am Git tutorial" > README.md
if we now examine the root of the working directory we will see the
README.md file has been created. At this point,
even though the file exists in your working directory it is still not been tracked by Git. We now need to notify git that
we would like to start tracking this file. To do this, we can simply use the following command to explicitly add the file.
git add README.md
This command adds the file to the Staging area, as we discussed in The Git Promotion Model. In the diagram below, you'll see that the git add command takes the file from the working directory and puts it into the staging area.
If we use the
git status command git will let us know that it is aware of a new file, but it has not been committed to
the local repository and also provide us with instructions on how we can remove the file if necessary.
We can go ahead and commit this file to our local repository and provided a message what we did. We discuss committing files in detail in How to commit files to a local git repository
git commit -m "Added initial read me file"
If we do a
git status after the commit Git will inform us that now there is nothing to commit and our working tree is
In Git 2.5 introduced the concept of a
Work Trees. The concept behind work trees is that you can have multiple, separate
working directories all connected to the same staging area and local repository. Your working directory in Git terminology
is the main working tree . You can now also create linked working trees with the
git worktree add.
Git stores the information about working trees in the Git directory area. The working tree information is stored in
.git/worktrees/<name of worktree>. A repository has one main working tree and zero or more linked working trees.
As a software developer, you'll typically be developing new functionality or working on bugs while creating, editing and deleting any number of files during an average day. You don't want to be having to remember all these files and adding them one by one when you're ready to add them to your staging area. Fortunately, Git has some nifty short cuts for you to use.
Lets run through a typical scenario, by simulating a developer starting a new Node.Js development project.
gitignore Node npm init -y echo "console.log('Hello World');" > index.js
During a simple process we've created 3 files. We can add all of these files to the staging area using a Git switch
-A, --all, --no-ignore-removal Update the index not only where the working tree has a file matching <pathspec> but also where the index already has an entry. This adds, modifies, and removes index entries to match the working tree. If no <pathspec> is given when -A option is used, all files in the entire working tree are updated (old versions of Git used to limit the update to the current directory and its subdirectories).
We can just go ahead to add all the files to our staging area as follows
git add -A
We could also use another Git shortcut
git add .
However, it is important to remember that this command stages new files and modifications, without deletions which our particular case is valid because we have not deleted any files.
It is important to remember that the
git add command only adds the content of the specified files at the time the add
command was run; if we continue to add and edit files after running the command then you will need to run the command
again to ensure the changes are included in the next commit.
If you want to check anytime what files have been included you can, as we did in the example previously, execute the
git status command. The
git status command can be used to obtain a summary of which files have changes
that are staged for the next commit.
git add command will not add ignored files by default. If any ignored files were explicitly specified on the
command line, git add will fail with a list of ignored files. Ignored files reached by directory recursion or
filename globbing performed by Git will be silently ignored. The
git add command can be used to add ignored files
-f (force) option.
Git add enables software developers with a variety of options. In some cases you may only want to stage modified
and deleted files, and not stage the newly created file. This use case can be actioned with the below command:
git add -u
git add *.js *.css
You can undo any
git add operation using the
git reset command. To undo adding file simply use the following command.
git reset <filename>
We explored the
git add command to understand how it fits into the git command workflow and it purpose in the chain.
We also learned that the
git add command does not add a file to a repository, it only adds to the staging area and in
order to ensure that a file makes it to the repository we need to run the
git commit command, which we will explore
further in How to commit files to a repository.