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.

Leave a Reply Cancel Reply

You must be logged in to post a comment.

ProDeveloperTutorial.com

Tutorials and Programming Solutions
Copyright © 2023 ProDeveloperTutorial.com
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT