Fundamentals of Git
Git is a distributed version control system (DVCS) that allows software developers to track changes in their source code over time. GitHub is a web-based platform that provides a hosting service for Git repositories and a web interface for multiple developers to interact with repositories, view commit histories, create and manage branches, and participate in code reviews.
In this article, I’m excited to give you a hands-on look at the core fundamentals of Git. By walking through a practical example, I’ll help you grasp the basics of this powerful version control system. This is essential to set you up to dive into the awesome features of the GitHub platform we’ll discuss later.
Why is Git so important for software development?
As a junior software developer, I’ve been there before — not using version control like Git and just managing all my code in separate directory versions. It was the worst when I was testing something that was not working, and I had no idea which old version was causing the issue.
Let me walk you through how that typically happens: You’ve got this codebase with hundreds of files you’ve been building up over months or even a year. Then one day, you run a test and uh-oh, you find something’s broken. You know there’s a critical mistake somewhere in those previous versions. You say hey, let me trace back and fix it. So you have to go through the old versions one by one, trying to hunt down the root cause. And it’s not even isolated to just a single file or code snippet — you’ve got to trace through tons of different files across all those versions to get to the bottom of it. Total nightmare!
You can imagine this process is extremely time-consuming and is a terrible experience. It’s so much easier when you’ve got version control like Git to lean on. Then you can easily track changes, revert to previous working versions, and pinpoint exactly where things went sideways. It saves you so much time and a headache.
0. Install Git
Let’s see how to create a Git storage on your local computer first, then we will go further to the GitHub teamwork environment. To create Git storage (we call it Git repository) in your local computer, you should first download and install Git from the official website.
1. Starting — Create a repository
After installation of Git, you can open Visual Studio Code and open a terminal. In the terminal window, you can get into the project directory and type
$ git init
This command will make a Git repository for your project.
After a new repository is created, you can browse the details of this repository by typing
$ git status
The terminal should tell you that your code is now on branch “master” and there are “no commits yet”.
From now on, if you make any changes in the files of this directory, you can add some files inside this directory and try the status command again. Git will show the files you just added as untracked files.
To make Git track your new files, you should commit the change by typing “git add .” and clicking Enter. This helps you to include all the changes in the staging area of git. If you only want to pick some of the changes in the staging area, you can use the command below to specify the parts you want to commit.
$ git add <directory/file name>
After the changes get on the staging area, you should type “git commit -m [message]”, which commits the change with a customized message to describe what you have changed in the code with your wording.
For my demonstration here, I add all files and commit the changes.
So after that, you can commit when you make some changes in your code. When you enter
$ git log
It shows the history of commits in the terminal window clearly.
Here the terminal shows a list of commits, each commit should include the commit ID (shown as yellow in VS Code PowerShell), the author, the commit time, and the commit message. The HEAD label indicates the commit checkpoint you are located at, and the master is your branch.
Usually, the default branch name is master. For the details of the branch handling, let’s discuss it in another article. You can quit the view of history by clicking “q” on the keyboard.
If you’ve used a Git GUI tool before, you might already know that the Git log history looks like a railway route diagram, with all these different stops along the way that have descriptions.
2. Git ignore file
Sometimes you are working on your repository, you have some files you do not want to be included and tracked by Git. If your repository contains tons of files and you don’t want to add them one by one. Instead, you can use an ignore file to exclude unwanted files. You can add a file called “.gitignore” in your repository and type in the excluded directory/file names, the format is
$ /<directory name/file name>
then commit this change and Git will skip tracking those files.
Suppose my new commit includes a directory called env containing a password I don’t want to disclose to others and a testing file called testing.html for my internal usage, I should create a .gitignore file that includes those files or directories. Then I do a commit step to complete the process.
3. Revert Commit
Throughout the development, in case you find something wrong in your code at the latest single commit and want to correct it, you can use the revert command shown below
$ git revert HEAD
Suppose I add a text file in my main branch repository. But later on, I recognize it is the wrong file to add. I can roll back this single commit by a revert command
Then you can see the file testing.txt was deleted in Visual Studio Code Explorer.
4. Resetting vs. reverting
One thing should be kept in mind, the Git revert command does not “revert” back to the previous state of a project by removing all subsequent commits. If you want to have a new start from a certain commit point, there is another option. To go back to a certain commit checkpoint in your local repository, try typing
$ git reset --hard <commit-id>
The commit-id could be the first 8 characters of your commit ID, which can be found in the commit history by command git log.
Suppose I need to go back to the commit of cross point and start my new approach, I could enter the below command to reset the code back to that commit:
$ git reset --hard 5957ca03
Then make a new branch called newStart by entering the command
$ git branch newStart
After that, you can check there is a new branch called newStart at the same commit point cross point as the original master branch
I started adding a new file called newStart1.txt and committed the change. Then review the log and see below
Summary
We’ve gone through Git’s basic commands, including how to make a new local repository, update it, undo changes, and check the history. I’ve put that info in the table below. Hopefully, it’s helpful for you.
For the next step, we’ll dive into branch control in Git and working with GitHub. Thanks for reading, and hope you have a great day!