ProDeveloperTutorial.com

Tutorials and Programming Solutions
Menu
  • Shell Scripting
  • System Design
  • Linux System Programming
  • 4g LTE
  • Coding questions
  • C
  • C++
  • DSA
  • GIT
  • 450 DSA Cracker
  • 5G NR
  • O-RAN

Chapter 3: GIT basics Part 1

prodevelopertutorial August 3, 2020
In the last chapter we learned about “git init” “git status” git config” commands and their importance.
In this chapter we shall learn about following commands:
git add
git commit
git ls-files
git reset
git rm
git mv
First create a simple .c file as below and name it as “firstProgram.c” :
vim firstProgram.c

#include<stdio.h>

int main()
{
printf("Hello World\n");

return 0;
}

git add

After creating the file, run “git status” command. The output will be as below:
git add
It says that “firstProgram.c” file is untracked. To add “firstProgram.c” file to staging area, we use “git add” command.
Run “git add firstProgram.c”
Then run “git status”, it says that there are files that are needed to be committed.
git add
Note:
1. To add a single file use “git add <file_names>”
2. To add whole directory use “git add <directory_name>”
3. To add all the files and directories use “git add . “

git commit

To commit use “git commit”.
Once we run this command, GIT will open a file in the default text editor to enter a commit message.
Enter the commit message and exit from the editor.
Then the changes will be committed.
git commit
Again we run status command to know the current status.
git_status
Note:
1. To add a commit message directly use “git commit -m “<commit_message>” “.
2. To add and commit directly use “git commit -am “add and commit at same time” “
Here it will only commit the files those are being tracked. To know the files that are being tracked by GIT use “git ls-files” command.

git ls-files:

1. We use this command to get the list of the files that are being tracked by git.
2. A file that has been already added to git repository or a  file in the staging area, GIT will be tracking that file.
3. For any new file that is being created, it needs to be added to the staging area by using “git add” command.
4. From that instance, GIT will start tracking that file.
Example:
1. Below we have 2 files “firstProgram.c  untrackedFile.txt”
2. But “git ls-files” will only give the file that has been added by “git add” command i.e “firstProgram.c” file.
git ls-files
3. To add “untrackedFile.txt” to tracking use “git add”.
4. Again if we check for “git ls-files” we get the list of files that are being tracked.
git ls-files
5. Then we can use “git -am “commit messge” ” to commit a file directly.

git rm

1. Once you do “git add”, all the files will be tracked.
2. Now the question arises, how to un-track the file?
3. We use “git rm” command to un-track a file.
Note:
“git rm” will delete the file from the file system also.
Example:
1. “git ls-files” to know the list of files being tracked.
git rm
2. Now we do “gir rm untrackedFile.txt” and after that we check the status. Now the “untrackedFile.txt” file is untracked.
git rm

git mv

1. “git mv” is used to move and/or rename a file.
2. It is useful because, if the files have already been staged, by doing “git mv”, the renamed file will also be in staged area.
3. If you use linux “mv” command then, you need to remove previously tracked file from git and add the new renamed file to git.
Example:
1. I have a 2 .c files that are tracked as shown below:
git mv
2. Now rename the “test.c” file to “newName.c” and then check if the files are being tracked by using “git ls-files” command.
git mv
Share
Email
Tweet
Linkedin
Reddit
Stumble
Pinterest
Prev Article
Next Article

About The Author

prodevelopertutorial

Follow this blog to learn more about C, C++, Linux, Competitive Programming concepts, Data Structures.

ProDeveloperTutorial.com

Tutorials and Programming Solutions
Copyright © 2022 ProDeveloperTutorial.com