Git has to be one of the most useful tools I have begun using since moving from a click-ops-focused systems administration job to a cloud-focused role dealing with code. I wish I had started using it years ago when I was working on my projects. I have been using it for quite a few months now and I am still learning new things about it. I have found that it is a very powerful tool and there are many ways to use it. So, what is git?

Git is a version control system that allows you to track changes to files and folders. It is a distributed version control system which means that everyone has a copy of the repository on their local machine. This allows for offline contributions and the ability to work on multiple tasks at the same time. Git is a very powerful tool and there are many ways to use it. Below we will cover the basics of getting started with git.

Getting Started

❗ Commands below were tested on Linux and Mac. Windows users might have to adjust some commands.

This will initialize the current folder as a git repository. Once the repository is initialized, you can use git commands to track changes to files and folders. The -m flag is used to set the default branch name to main.

mkdir git_tutorial
cd git_tutorial
git init -m main
git status

Next, we will add a file and see what git is doing after checking out the main branch (sometimes called current, master or latest)

git checkout -b main  
touch README.MD  
git status  

Be sure to read that status output. It is telling us that we have a new file that is not being tracked. We can add it to the staging area and commit it. The staging area is where we can add files and folders that we want git to track. We can also remove files and folders from the staging area. Once we have added files to the staging area, we can commit them. A commit is a snapshot of the files and folders that are in the staging area. We can add a message to the commit to describe what we did. We can also add multiple files and folders to the staging area and commit them all at once. Stage and commit often. It is a good habit to get into and each commit is a snapshot of your work. so if you need to go back to a previous version, you can.

git add README.MD  
git status  
git commit -m 'added empty README.MD'  
git status  

Notice the changes and there is nothing to commit with the second git status. This is because we have already committed the file. We can also add files and folders to the staging area and commit them all at once.

Now let’s create a branch:

git branch content  
git checkout content  

By creating a new branch, we are creating a new line of changes. We can make changes to the files and folders in this branch without affecting the main branch. This can allow you to experiment with changes without affecting the main branch. Keep your main branch clean and only commit changes that are ready to be merged into the main branch. Use your other branches to experiment with changes and test things out. Once you are ready to merge your changes into the main branch, you can do so. This is a very powerful feature of git and it allows you to work on multiple tasks at the same time. Use your branches to fail fast and fail often. Even when things get to a FUBAR state, you can always go back to a previous commit and start over.

Now let’s simulate some work. We will create a file and add some content to it (the hostname of your machine).

hostname >> ./hostname.txt  
cat ./hostname.txt  

Let’s see what git has to say about branches

git branch  

Go head and show the status of the repository


```bash
git status  

Stage all file changes and commit

git add .  
git status  
git commit -m 'hostname.txt file added'  
git status

Ok, you now have the perfect hostname.txt file. Let’s merge it into the main branch. Chekout the main branch:

```bash
git checkout main  

Now we are going to pull the changes into master

git merge content  

You are now ready to rule the world. You have a git repository with a main branch and a content branch. You have added a file to the content branch and merged it into the main branch. You are now ready to start using git to track changes to your code, documents, or pretty much anything else you can think of. You can also use git to collaborate with others. You can create a repository on github and share it with others. You can also clone a repository from github and work on it locally. You can also push your changes back to github. There are many ways to use git and it is a very powerful tool. I hope this tutorial has helped you get started with git, but before we go, I have one last command for you to run. The ‘git log’ command will give you a summary of the activity in the repository. It will show you the commits that have been made and the changes that were made in each commit.

git log