We have already received an introduction to Git and learned how to install Git on multiple platforms. Now we’re ready to get started learning the basics of Git. In this article, we’re learning how to add files to Git. This means we’re telling Git which files we would like to monitor for changes.
We’re going to be using Git on the command line. As stated before, you can use one of the many popular Git applications available. These are easy to use and can provide you with a friendly interface to accomplish the tasks we’re going to learn. However, to ease the learning process, we are going to cover command line Git, which is universal across platforms. You will need to open up your Bash instance or favorite Terminal app to get started.
Configure Your Git User
You will first want to make sure that you have added a user name to your git installation. This is done on your local computer.
Type the first part of the command and replace what’s inside of the quotes with your desired user name.
git config --global user.name "Joe Example"
It is also important to add an email address to your Git configuration. Git will not allow you to make commits to your repository without an email address.
This will also make it easier for other members of your team to contact you. However, if you’re working alone, or you do not wish to share your email address, you can put anything you want as an email address.
git config --global user.email [email protected]
Initiate a New Git Repository
You can start using Git with any set of files on your computer. All you need to do is “initiate” a Git repository from within the directory that contains your files. All you need to is navigate to the directory in which your files reside and type the following command:
You will see a message indicating you have successfully initiated a new Git repository.
How to Add Files to the Staging Index
You will constantly find yourself adding files to the staging index in Git. You will be both adding new files for tracking and adding modifications to files that are already being tracked.
To add a new file for tracking, use the “add” command followed by the name of the file.
git add name-of-file.txt
There will likely be occasions in which you will want to add the full contents of a sub-directory to the staging index. For example, if you wanted to add the full contents of the “assets” directory to the staging index, you can add the directory followed by a forward slash and an asterick:
git add assets/*
You can also add the entire contents of your working directory to staging with the “add” command followed by the
git add -A
Remember that each time you make modifications to a file you will need to add those changes to the staging index in the same fashion that you added the file itself. If you have already added files to tracking and only want to record modifications without adding new files, you can use the “add” command followed by the
git add .
Often, there are files or file types you will want to always exclude from your staging index. In this case, you will want to use the .gitignore file. All you need to do is create a file called “.gitignore” and add various rules to it according to your needs.
Any file listed on its own line in this file will be ignored by git, even if you add a whole directory to staging. You can also use astericks as wildcards. For example, if you want to exclude a file called “notes.html” and all .txt files from your repository, you can use the following rule in the ignore file:
Checking the Status of Your Files
Often, you will want to check the status of the files in your projects including which files are tracked or untracked. In order to get this information you can use the status command.
Well done! You now know how to add files to Git for tracking, how to configure your git user, how to ignore files, and how to check the status of files.
Now, we will continue with how to make commits in Git.