Once we have set up our Git and the repository, we can get started with the version control. We can now work on files and directories in the repository, make changes, and commit those changes.
Concepts of Staging
When we create or edit an existing file in a git repository, we will have to stage the changes. When you are working on a feature and you are done with the changes, you can simply stage the changes. By staging the changes, you can start working on additional changes in the same file or otherwise. Staging will also mark your changes to be included in the version control history.
Using Staging to your advantage
You might ask why would you need to stage the file multiple times before you commit those changes. While modern development workflows have done a good job of dividing work into simpler chunks, these chunks can be further broken down.
Let's consider an example where you want to build a dashboard tile in some kind of dashboard in a system. You might need to write API, create routes, and write front-end codes. You can do all those things, and test all those changes when you finish making changes. Or, you can just code the API, test the API, and stage the changes. Then you can move to setting up routes, stage those changes, and move on to the front end. The main advantage you will get by doing this is that you will create checkpoints of working codes. You can undo the unstaged changes with a simple command which is a million times better than Ctrl-Z
-ing your file until your last working changes. You also ensure that no accidental changes are made after you have tested and staged your changes because git will let you know that new changes have been made after staging.
File statuses in git
When working on a repository, we will have to deal with the states of the changes. A file can have the following states:
Untracked: The file is not tracked in the version control yet. A file can be untracked when you create a new file.
Unmodified: The file is tracked by the version control but there is no change in the file
Modified: When a tracked file is changed, the status of the file is changed to modified.
Staged: When a tracked file is changed and those changes have been staged.
Practically learning git staging
Now we will apply these concepts practically in Git. We will open the learning-git
directory in the terminal.
Setup
We will create a new file index.html
with the touch
command and track it with Git.
$ touch index.html
After creating the file run the git status
command which will give us the current status of the repository.
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
index.html
nothing added to commit but untracked files present (use "git add" to track)
Git lets us know that there is an untracked file index.html
and also hints to you on how you can track the file.
Staging change(s)
We will now stage the index.html
file by running the git add
command followed by git status
command as follows:
$ git add index.html
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: index.html
We can now see that the file has been staged for the commit. Also, you can stage multiple or all changes at once.
Let's consider you have two files index.html
and about-us.html
. If you want to stage both files, then you can run the git add
command with paths to the files.
$ git add about-us.html index.html
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: about-us.html
new file: index.html
You can also track the files in the repository with git add .
the command or all files within a certain directory such as all files inside img
directory with git add img/.
Unstaging change(s)
You can unstage a newly created file that has never been committed by running the following command.
$ git rm --cached about-us.html
rm 'about-us.html'
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: img/image1.jpg
new file: img/image2.jpg
new file: index.html
Untracked files:
(use "git add <file>..." to include in what will be committed)
about-us.html
If you are making a change in a file that is tracked by the repository, you can use git restore
the command as follows.
$ git restore about-us.html
You can also provide git rm --cached
and git restore
with relative file paths and wildcard(.) like we did on git staging commands.
In this article, we have learned about the concepts of git staging. In the next article, we will learn about committing the changes to the local git repository.