What is Git and why has it become a must in the current software development world?
Git is an open-source, distributed version control system created initially by Linus Torvalds, creator of Linux (& Git). You might be wondering what is a Version Control (or Source Control) System. Well, in the world of software development today, versioning the software has become essential as it helps developers to keep track of new changes being done to the software and allows developers to collaborate on it. Also if something goes wrong, they can roll-back to the last stable version.
Knowing the purpose of Git, we can move on to how to use Git to build awesome pieces of software. The official installation guide is here. To use Git, first of all, we have to open a terminal and change our present working directory(aka pwd) to the directory where we want to start using Git. In Unix/Linux based systems, command cd
will do what we want.
cd ~/path/to/directory
Once we're inside of target directory, we have to initialize the Git repo. We can do so by running the following in the terminal.
git init
Here is output of git init
on successful execution:
Initialized empty Git repository in ~/path/to/directory
We discussed earlier that Git keeps track of changes in files of our project. From "tracking", there are two states of files inside a Git initialized directory. One is tracked by Git and the other, as you might've guessed, is untracked. To understand the concept of tracked and untracked files, we must have some files first. If the directory is empty, we can create a file and add some text to it directly by using echo
command on Unix/Linux-based systems.
echo "1\. Learning Git and Github" > ./learnings.md
The >
creates a new file named learnings.me
and puts the output of echo
in that file. Now as soon as we create this file, we can execute git status
in the terminal to know the status of our tracked/untracked files.
Ignoring all other information, when we set an eye on the section of Untracked Files, we see that there is our file named learnings.md
. I.e. Git recognized the fact that the file is new and there has never been a version of this file stored inside of Git and that makes the file untracked.
Command: Git Add
Once the file is on the list of Untracked Files, we much move the files to the Git state of Staged Files, where Git lists files that we changed the content of to update our software. In our case, we created a new file (learnings.md
), which will be added to the list of Staged Files.
git add learnings.md
As discussed earlier, to know the status of Git tracked/untracked files, we run git status
. Here is what Git will show us now once we've run git add learnings.md
.
Command: Git Commit
Now we can see that our file is listed under the Changes to be committed(aka staged file-state) section. Once files are staged(ready to be committed), we can tell Git to take a snapshot of the current state of files with git commit
. In general, we provide a meaningful message(aka Commit-Message) with every commit, so that we can look back at commits later and recognize the changes done in a specific commit. We provide our Commit-Message with an -m
flag with git commit
.
git commit -m "added list of techs in learnings.md"
Git has taken a snapshot of current files in the directory and since this is the first commit, it is labeled as "root-commit". We discussed earlier that the main application of Git is to keep track of changes in our files. So now it's time to change the content of the file we created earlier. In Unix/Linux systems, we can use >>
with echo
to append the content at the end of a file making (more info on >>
here) a change to the file and that's where Git kicks in.
echo "2\. ReactJS and VueJS" >> learnings.md
We can use cat
command to print the content of a file in the terminal in Unix/Linux systems. And we see that the file's been updated with a new line containing "2. ReactJS and VueJS".
Command: Git Diff
Now that we have an updated file that we once committed into Git earlier, we can ask Git to show us differences between the earlier committed(saved) version and the current updated file with the command git diff
.
Git diff shows us a green line with a "+" at the start. It stands for the fact that the content that was added to the file is new. Once again we can commit these changes to make a snapshot of the current state of git. Running git add learnings.md
and git commit -m "added ReactJS and VueJS to the learnings list"
subsequently would stage and commit our new change in the file. We can see the list of commits by executing git log
.
If we wish to be informed about the Commit-Messages only, we can use --oneline
with git log
.
Quick Revision Of Topics Discussed Till Now:
- initializing git in a directory with
git init
- staging files with
git add <File Name>
- committing them with
git commit -m "meaningful-commit-message"
- see the difference between the current state of files and the last committed version of files with
git diff
- see the information about the commits with
git log
orgit log --oneline
(for compact info)
Hurray 👏 👏 👏 , now you know how git operations that developers do 1000s of time in a typical year are done!!! 🥳
Next post in this series is 02 Git Remotes and GitHub where we go deep into pushing repos to GitHub and manage remotes to local git repo.
This post is originally written on my blog.
Top comments (0)