In this tutorial we are going to cover the areas like Staging, Removing, Moving, and Ignoring Files with Git.
Let's add two files to our directory.
echo hello > file1.txt
echo hello > file2.txt
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
git add *.txt to add all the text files available in the current directory.
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;
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.
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.
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.
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.
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.
git add file1.txt to stage the deletion of
file1.txt and run
git add main.js to stage creation of
However, than using above mentioned several steps, you can directly using the below command to rename a file.
git mv main.js file1.js.
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.
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
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
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'. 😀