
Git Good with Git Version Control
Basic Tutorial of Git to help you Get Good with Git
The Need for Git
One of the most frustrating feelings when coding is when you have working code, then through unfortunate circumstances the code no longer functions. There can be a variety of reasons for this. When working with others, they may change the code in a way that you are unfamiliar with. When you try something new in the code, you make unknowingly break part of the functional code. Without a proper version control tool, this can lead to hours of trying to recreate the code that you already had.
Git and Version Control
Git can help with this situation. Git is one of the most, if not the most, popular version control tools. Version control is exactly what it sounds like, a tool to control the different versions of your code. There are a few main aspects of Git that I will cover in this post. The main aspects are initializing the Git repository, add files to the staging area, committing the files, and switching the head. This post will deal with how to perform these steps on a Mac device.
Initializing the Git Repository
The first step, initializing the Git repository, is a fairly simple step, but a very important one. The first thing you need to do is ensure that you are in the directory you want to initialize as the Git repository. After maneuvering to the correct folder in the Terminal, type the following command: “git init”.

This will initialize the folder as a repository. To verify the folder was correctly initialized, type “git status”. If the terminal has a fatal error, then the folder was not initialized.

If the terminal says “on branch main” with some additional information, then the repository was properly initialized.

Staging Area
After the Git repository is initialized, you can begin to work with the files in the repository. The process that files go through with Git are the following:
- Saved
- Added to Staging Area
- Committed to Git
The first step, saved, is saving the file like you would any other file on your computer. After you save the file, add it to the staging area. This area is designed so that you can add and remove files before committing them as a version of your code you can go back to. This is nice in case you add a file that you did not mean to add; you can remove the file before it is committed. The staging area also allows you to commit many files as once. To add a specific file, use the command “git add {filename}”.

To add all the files in a repository, use the command “git add .”. To remove a file from the staging area, use the command “git rm {filename} –cached”.

This will keep the file saved but will remove the file from the staging area. To remove all the files from the staging area, use the command “git rm . –cached -r”.

Git Commit
Once you have all the files you want in the staging area, the next step is to commit the files. Once you commit the files, you will be able to go to the repository at the specific point in time you committed it. No more losing hours of your life recoding what you have already done!
Committing is very easy, in the case of Git, at least. Once all of the desired files are in the staging area, type the following command “git commit -m “{commit message}””.

The commit message is a short statement about what you did in the commit. Usually commit messages will start with a verb and will deal with a single topic, like fixing a specific bug.
Switching the Head
Once you have several commits, you may want to backtrack to a specific commit in the past. This can be done by switching the head. The head is the term that denotes which commit your repository is currently at. Switching the head can be done by using “git checkout”. However, you need to be able to reference which commit you want to make the new head. The commit ID is how you can accomplish this. To get the ID associated with each commit, type “git log” or “git log –oneline” for a more condensed version of the log. The number on the top above the author (when using git log)

or left of the commit message (when using git log –oneline)

is the number you will need. When you have the number, you can then type “git checkout {ID}”.

Checking out to a previous commit will put you in “detached head” state. There is limited capability in this mode, but all the files will appear as if you were in that commit. There are other intricacies with working in “detached head” state. However, those will not be discussed in this article. It is suggested that you continue research to learn more about the finer points of “detached head” state. To maneuver back to the latest commit, simply type “git switch main”.

What now?
While learning about Git is very important, like any other coding principle, the most important thing to do is to practice. Practice getting good with Git, practice adding files, removing files, and committing files, so that you will not run into the issue of having functional code ruined. Continue to become more advanced in Git so that you can use this incredible tool to its full potential.
For additional information about Git commands, find the documentation at Git’s Website.
Title Photo
Photo by Mohammad Rahmani on Unsplash