Deep Dive Into Git(Part II)
In my previous blog, I discussed “what is Git?”. In this blog, I am going to share how you can get started with git commands. Before deep dive into git, firstly, we need to understand what is GitHub. So GitHub is a code hosting platform for collaboration and version control. There are many platforms same as GitHub like GitLab, Bitbucket etc.
Before getting started, we need to install git. To install git just go to the link and download git for your system. If you are using windows then you have to install git bash. If you are using Linux or Mac then the default terminal is fine for you. After that, you need to go to GitHub and create an account. In this article, we are mainly focusing on git commands. Now we can start our journey with basic git commands.
First, we need to understand some of the words that are often used in git.
- repository: A git repository is a .git folder inside a folder.
- sha/hash: It is a hexadecimal number which is used to identify different commits.
- directory: It is same as a folder. But when we navigate through terminal we use the term directory.
- master/ main: It is the name of the default branch in git.
Now, just open git bash if you are in Windows and terminal if you are in Mac or Linux. Every git command started with git keyword. So to check that git is installed properly in your system just run the following command:
git — version
It will show you the version of git you installed in your system. After that, you have to configure your name and mail at git bash. To do that run the command:
git config — global user.name “your name”
git config — global user.email “your email”
Now you are ready to go. First, you have to create a directory. To create a directory using terminal use the following command:
mkdir folder-name
To enter inside a directory write the following command:
cd folder-name
After entering into the directory, now you have to initialize git to access all the features of it. use the following command:
git init
It initializes an empty git repository. So now if you turn on hidden files and folder, you can find a .git folder inside that folder. Or you can use
ls -a
command and you can find one .git folder inside it. It is the place where git controls all of its features.
Now you have to insert some contents inside the directory. So just open that directory in your favourite editor(Ex: VScode, Atom, Sublime etc.) and write some codes inside it.
After saving all the files. Now you need to stage all the changes inside the files. Because now the files are inside the directory but version control does not work now. Only the last saved version was shown now. To stage all the changes use:
git add file-name (to stage changes only inside that particular file) or,
git add . (to stage all changes inside the current directory)
Now after staging all the files or changes inside the project you need to commit those changes. If you don’t commit those changes then only the last version is saved. To commit all the changes that you made use the following command:
git commit -m “commit message”
You can also add and commit both at the same time by using the command:
git commit -a -m “commit message”
After committing all the changes a sha or hash is generated for that particular commit. Now if you made some more changes then you can compare those changes with any committed versions by using the command:
git diff sha-of-one-commit sha-of-another-commit -p
One of the most helpful git command is status. It gives you all the information about the current situation of your repository.
git status
What is HEAD?
HEAD is a pointer which always points to the last committed version. So you can also use the HEAD in your commands. Ex:
git diff HEAD HEAD~1 -p
To list all the commits details use the following command:
git log
Another useful command is show command. It is like a merge of git diff and git log. By using show you get all the details of a particular commit like author, date and so much more.
git show sha-of-the-commit
To see the commits in a particular file use:
git annotate file-name
If you are using VScode then you can also you an extension named as git lens to get the same output in VScode.
Now if make some changes but not add them to go back to the last committed version use:
git checkout — filename
If you add those changes and now want to go back, just use reset command:
git reset HEAD filename
Now if you run the checkout command it takes you to the last committed version.
If you are in a stage when you commit those changes and find that you are doing something wrong then just go to a previous version write
git checkout sha-of-the-commit-you-want-to-go file-name
These are the basic commands of git. After learning about all these commands you are ready to go with GitHub and other complex commands of git. Just check out any cheatsheet of git for learning more.