A brief introduction to Git
Tags: javascript, git
Published: 2020-09-14 03:29:24

As I'm sure you've been told repeatedly, programming is usually a team effort. Enter Git.
# Version Control Systems and Distributed Version Control Systems
To allow programmers to work as a team, some sort of version control system, or VCS is typically used. A VCS tracks any changes made to a project and it allows users to recover any version of a project that they wish to view. Additionally, you can also typically see any changes that were made, who made them, when they were made, and hopefully why they were made. Git is not only a VCS, its a distributed version control system, or DVCS. This means that Git users have complete access to every branch, file, and iteration of any project they're working on, as well as a total history of all changes made to that project. In addition to this, DVCSs don't require constant connection to a central repository, meaning developers are free to work on projects wherever they have access to a computer! In addition to the regular benefits of a DVCS, Git has the added bonus of being extremely lightweight, allowing for easy creation and changes to branches. These traits combine to make Git incredibly ubiquitous, so much so that over 70% of developers use Git!
# Git Basics
Git essentially creates whats known as a repository  and tracks it. A repository is a collection of all files and folders associated with a project. From there, the file history is tracked as a linked-list through commits, with each development line known as a branch. Since Git is a DVCS, anybody with a copy of the repo is able to access not only the entire codebase, but also its history.  There are three basic commands for updating your repository: add, commit, and push. Before performing an add, you can also use the status command to see any changes to your work. Checking the status on an unmodified repo should look something like this:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/tt56rgktf64eyj465l4v.png)
Note that all Git commands must be preceded by the phrase "git". After a change has been saved, it needs to be staged. The add command is used to stage changes: 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4g7xl8io1zho5dbuag7o.png)
You can add a single file like in my example, multiple files separated by a space, folders, the entire repo, or any combination in between! After staging a change using add, the commit command is used to actually save that snapshot to the project's history. Upon performing a commit, you will also typically be prompted to include a message along with your commit to describe your reason for performing the commit:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xruj2o6s1tepaibzcnfl.png)
Finally, the push command is used to update a remote repository with any commits that you have made locally!
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2uc9a8ay6dpqes6vdltl.png)
With those four commands you can begin editing a Git file!

# When trouble rears its Head....
Sometimes, things get off track. One of the first problems I encountered while using Git was the ominous "detached HEAD" state. This happens when using the checkout command. Checkout can be used to view a branch or a specific commit. Its the latter use-case that causes the detached HEAD warning. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/j8qinup7qknstt7am6of.png)
In Git, HEAD typically points to the revision that you're currently working on, and moves automatically with each new commit. However, this process is disrupted when checking out a commit hash. This happens because when you change to a commit, you are no longer pointing to a branch. This is the essence of a detached head: HEAD pointing directly to a commit instead of a branch. Because of this, changes that you make won't belong to any particular branch. This is a problem because you won't have an easy way to access any changes you make in this current environment. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/c5lv3emmh3hibu2t3e34.png)
From here, there are two scenarios: you've arrived here by choice and you want to keep any changes you make, or you've stumbled here by accident and want to get back to your main branch. If you're here by accident, its relatively simple to get back on track (assuming you just want to get back to the master). Simply checkout the master:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/e1vtoezdibf9tb5ht9zh.png)
From there, you should be free to continue your work. If you want to keep working on this new line and make it a new branch, thats also pretty straightforward. simply create a branch and run checkout on that new branch:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5qsd461rja457a06vsq1.png)
I hope this article can help you out the next time you encounter a detached head!