Overview
Lets start with the question what is Git?? Git is an version control system. Okay...but what exactly is a version control system.
Version control systems are a category of tools that helps in tracking changes made to files by keeping a track of all the modifications done in the code. So git is one of the most popular and widely used version control systems.
Installation
You can install git from below link:
Configuration
After installing git, you need some configurations to be done. Please follow below link:
Using Git
We can get the git version installed in our system using git --version
command. And git help
will show all the commonly used git commands and their description.
git --version
git help
Initializing git
Change the path to the folder/directory where you want git to keep track of files and run git init
command
as shown below. This will initialize a new git repository and all the changes done in this directory will be tracked.
cd /users/demo_directory
git init
If we run ls -la
command we can see the hidden .git
file that is created in the directory.
$ ls -la
total 8
drwxr-xr-x 1 user 181220 0 Jul 24 14:21 ./
drwxr-xr-x 1 user 181220 0 Jul 24 14:20 ../
drwxr-xr-x 1 user 181220 0 Jul 24 14:21 .git/
Creating file and adding it to git repository
Create file and see the status
Now, create a text file with some text in the directory and run git status
command. The git status command is used to display the state of the repository and staging area. It allows us to see the tracked, untracked files and changes. This command will not show any commit records or information.
$ git status
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.txt
nothing added to commit but untracked files present (use "git add" to track)
Note: In the second line, we can see On branch main
this is because I have set main
as the default branch name instead of master in my system.
Adding files to stagging area
After running the git status command we can see that there is an untracked file. Here git automatically detects the files in our folders. To keep a track of modifications or changes in the file, we have to bring that changes to our staging area and the process of adding these changes or modifications in an area to keep a track of it is called staging.
We can add these files to stagging area using any of the below commands. Use dot when you want to add all the files in the directory and use file name to add specific files.
git add .
or
git add test.txt
The git add
command adds changes in the working directory to the staging area. It tells git that you want to include updates to a particular file in the next commit.
Once if we add and see git status
, we find that our file test.txt
is being tracked and the changes need to be committed.
$ git status
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: test.txt
Commit
Run below command to commit the changes.
$ git commit -m "Initial Commit"
Note: 'm' tag in above command is the message tag which shows a short description of changes that are being committed. This is very important and plays a significant role while identifying different commits.
Now you can edit the file again and run
git status
. We can see that the file is modified but changes are not staged.
$ git status
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
Proceed further adding these changes to the stagging area using git add
and commit those changes using git commit
.
Pushing above local repository to online.
Instead of using local repos we can have online repos as well and the most popular and widely used online repository store is GitHub.
Create your account in the Git Hub website github
Click
+
icon in the navigation bar then click onNew repository
and add new repositoryUse the following commands.
To create a branch use git branch
command. Set the remote origin using git remote add origin
command. Then push your code to the git hub repository using git push
command.
git branch -M main
git remote add origin https://github.com/username/repo_name.git
git push -u origin main
Thank you...That's all from my side on the topic Git.
Thanks for reading my blog, drop a like/comment if you find this article useful.
Happy Coding Journey😊.