Chathumi Kumarapeli
Posted on October 28, 2021
In this tutorial we are going to cover the areas like Staging, Removing, Moving, and Ignoring Files with Git.
Staging Files
Let's add two files to our directory.
echo hello > file1.txt
echo hello > file2.txt
Now let's check the status of the working directory and the staging area using the command git status
.
Then you will be able to see something like this.
Two files are indicated in red as they are not yet added to the staging area. To add them what you have to do is simply using one of the following commands.
git add file1.txt file2.txt
Or
git add *.txt
to add all the text files available in the current directory.
Or
git add .
to add all the in the directory. But beware when using this command because there might be files which you need not to be added to the repository.
Once again let's check the git status and if you have done your work correctly it should display like this;
Let's modify out file1.txt.
echo world >> file1.txt
and let's check the git status
.
You can see the modified file in red color, as now the directory has the modified version of the file1 while staging area has the initial version of it. Hence you should once again add file1 to the staging area using git add file1.txt
.
Let's commit our changes.
git commit -m "Initial Commit"
Don't forget to add a meaningful commit message as it is a good habit. Now you should see something like this;
When you are committing code changes mind about following facts.
Make sure that your commits are not too large or too small: you need not to commit each time you do a very small update, or you should not wait till last moment to save all your updates.
Commit Often: Normally a developer commit 5-10 times a day making each commit should contain a separate changes set.
Meaningful commit messages: your commit message should describe what kind of changes you have made. If it is a bug fix, you can name the commit as "Bug fix" and explain it a little bit more in the description part. This helps to other members of the team to track the history of the changes.
Skipping staging area
It is not a necessity to stage the changes before committing. But you should do that only if you are 100% sure that you need not to review your changes later. To skip the staging all you need to do is committing without adding the files to the staging area. Look at the example below.
echo everyone >> file2.txt
git commit -am "Changed file2"
Now the change you did to the file2 is committed skipping the staging area.
Removing files
What should you do if there is a file that you need to remove? The command you should use is as follows.
rm file2.txt
Now when you check the git status
it should be like this;
How can you know whether this file is removed from the staging area? You can use git ls-files
to see the files available in staging area.
As you can see in the above image, the two files are yet in the staging area. Hence to remove the file2.txt from it you should add the change.
git add file2.txt
Yes, even though you are removing file2, it should be 'add' key word you should use in the command as you are 'adding' a change to the staging area.
Now if you check the staging area you'll be able to see that there is no file2.txt in it. Now you can do a commit.
Renaming/Moving files
Currently we only have file1.txt in our working directory. Let's rename this into main.js using following command.
mv file1.txt main.js
As shown above now you have two un-staged changes. One is deleting the file1.txt, second is having main.js as an untracked file. Git does not automatically track new files. Hence whenever you have a new file you should add it to the staged area for Git to track it.
Run git add file1.txt
to stage the deletion of file1.txt
and run git add main.js
to stage creation of main.js
.
However, than using above mentioned several steps, you can directly using the below command to rename a file.
git mv main.js file1.js
.
Ignoring files
Sometimes you might have certain files to ignore without adding to the repository. For example files like log files or binary files do not need to add to the repository.
Let's create a directory to store a log file.
mkdir logs
echo logInfo > logs/logFile.log
Now let's see how we can ignore adding this file into repository. For this you have to create a file called .gitignore
which is a file with no name, only an extention. Hence let's run the following command.
echo logs/ > .gitignore
You can check what is included in the file by opening the VScode using code .gitignore
.
If you run git status
it does not says that we have a new directory named logs because Git ignores it. It only says we have a new file named .gitignore
. Now do not forget to stage .gitignore
.
What should you do if you want to remove a directory from the staged area? Simple. You can use the command,
git rm --cashed -r directoryName/
This will remove the entire directory from the staging area.
That's all for this tutorial. For more head over to the tutorial 'About Git #03'. 😀
Posted on October 28, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.