Git & GitHub Tutorial - Part 3 - uses of git init, git add, git commit and git log

git init

To use git with a project we need to create a git repository. To create a git repository open git bash to the project directory (in my case empty directory named Test) and run the command 

git init

This will enable git for your project and create a .git folder. You have to initialize git only once for a project. If you can not see the .git folder or dot file folder hidden in your system you can simply run the ls command with -a flat to see the hidden files and folders.

ls -a

Now if you run 

git status

you can see some messages on branch main and commit status, which means you initialized git properly. If you delete the .git folder that will remove all git history. Git will watch everything in your directory (in my case Test) including subfolders and files. Like if I create a folder inside my Test folder named styles and go-to styles directory and run the command git status this will show a kinda similar message to the previous one about branch name and commit status. That means we are in a git repository. So we do not need to initialize git to any folder inside a git repository.

git add

To add all changes to our git history we need to add/select the changes. Like if I create a file named index.html in the root directory (inside the Test folder) and create another file inside the styles folder named style.css. If I delete files or folders or edit the style.css file we need to track all these changes to add the git repository as a history. After the above changes if I run the command git status this will show the untracked changes (files and folders) we made. So git notice all changes we made inside our project. To add the changes to the staging point we need to use the git add command with the file names. We can add single file or multiple files at a time by the spacing between the file names. Like

git add index.html


git add index.html styles/style.css

If we run the git status command now this will show us changes to be committed file names with green text. We can also use the

git add --all


git add .

to add all the changes we made for the commit.

git commit

Previously we add our changes to the staging point by adding the git add command. Git commit used to save changes to the local git repository, and a specific message has to provide with it about the changes. This message tells about the changes and commits so a meaningful message will be helpful. To commit and set message at the same time we will use the following command

git commit -m "Created index.html and style.css files"

After the -m flag you can write your message inside quotation marks. If you do not use -m flag this will open your default editor for your bash this might make confusion at the beginning point. So use the above command with -m flag. Now if we run the git status command this will show a message like nothing to commit, working directory clean. That means everything is up to date no new changes are available to add a commit.

git log

To see the commits with information you can run the following command.

git log

git log will show the commits with author, date, commit message with hash.

If you miss the previous content you can read it from here.

Read: 26
Login to comment

Recent Comments