Get to Know Git

What is git?

Git is version control system that keeps track of changes that you make to files and directories. Git is great at tacking the changes you make between version of the files as you save. This is especially important when you would like to track where, when, why and who introduced a bug or feature into the code. Here are some other forms of version control that you have most likely used:

  • File Naming (version_1.txt, version_2.txt)
  • Microsoft Word: Track Changes
  • Adobe Photoshop: History
  • Wikis
  • Undo: Ctrl + Z (Windows), Cmd + Z (Mac)

History Behind Git

Git wasn’t the first source code control system there have been others that influenced the Git we know today. It originally started with the Unix operating system which the first Source Code Control System (SCCS) came free with Unix. With each generation, people improved upon the idea of a source code control system. Although Git became a hit due to its distributed design, features, speed, size, and open-source ecosystem. Here is a timeline of the most influential version control to name a few and how we currently got to Git today:

Installing Git

You can install Git the git executable at the git downloads page. Once you have it installed you should be able to type in a command prompt git --version to see the version that you installed. After you have done this, you need to configure git on your system. There are two configurations that you need to apply, and you will want to match the username and email that you have on your GitHub. Also, if you wish to further customize your git configuration you can see the list here in the Git Documentation. In addition, you can view a list of default configurations by typing the command git config --list or append --help to view additional options in the manual.

git config --global user.name "Your GithubUserName"
git config --global user.email "[email protected]"

Intialize Your First Repository

The way you initialize a repository is by first having a project and going to that project directory/folder. Then you will want to use the git init command to initialize that repository. This command creates an invisible .git folder which you can see if you run the ls -al command which stands for list all files and directories.

Where Are Git Files Stored?

Git files are stored inside of that hidden. git directory and have the following folder and file structure. Although you may not have to deal with anything except for config it’s still great to understand the underlying system.

Folder or FileDescription
HEADYou can think of HEAD as the current branch. When you switch branches with git checkout, the HEAD revision changes to point to the tip of the new branch. You can see what HEAD points to by doing:
cat .git/HEAD
configFile that holds your project configurations which is different from global configuration like above.
descriptionThe description file is used only by the GitWeb program, so don’t worry about it.” As stated in the Git documentation.
hooksThe hooks folder contains scripts that execute when a certain event occurs. It’s like a trigger event in a database for reference.
infoThis contains a file called exclude which is similar to a .gitingore file. However, this is used for user specific applications like when different developers use a different code editor. For example, when you would want to ignore the .vscode folder.
objectsContains a key value data store. What this means is that you can insert any kind of content into a Git repository, for which Git will hand you back a unique key you can use later to retrieve that content.
refsGit refs holds pointers to your files and their changes.

Create Your First Commit

First you will need to have a file inside your project folder that has git initialized. You can create a file with the bash command touch first_file.txt | echo 'sometext' >> first_file.txt. What this does is creates a file and inserts the text ‘sometext’ into the file.
You can see this when I use the cat command to output the text of the file.

Now since you did make a change by adding a file to the repository. We now must stage our changes by using the command git add . Which means stage and add all the changes that we made in our current directory. Now if we would like to un-stage those changes before committing, we simply git reset <filename.txt>. If you would like to see the files that were added to staging, you can simply use the git status to view them. Now finally creating your first commit is as simple as git commit -m "Added First File.txt". The -m stands for message and it’s incredibly important to create clear and concise messages. Here are some best practices to keep in mind when creating a commit message:

  • Keep the message a short single-line summary (less than 50 characters).
  • Optionally it can be followed by a blank line and with a more complete description. You should keep these lines less than 72 characters in length.
  • Write commit messages in present tense, not past tense. (“Fix for a payment system bug”, “Fix a payment system bug”, not “Fixed a payment system bug”)
  • Bullet points are usually asterisk or hyphens
  • Add “tracking numbers” from bugs or support request

View Changes In Repository

To view a list of changes that have been made after committing you can view the git log. The git log enables you to search the entire repository for specific changes. This is handy when you would like to know when a specific feature or bug was introduced. As you can see in the git log each commit has a sha1sum hash, author, date, and the commit message. You can also see that the head points to the latest commit in the master branch.

Additionally, you can search appending –grep (Global Regular Expression Parser) for a specific commit. In the example below you can see that I searched for “first” and the only commit that was logged “add first file” since that’s the only commit with the word first in it. Therefore, it is especially important to write good commit messages.

git log --grep "first"

Connecting To GitHub

Now that I’ve covered most of the basics we can connect to GitHub. First you will login and create a repository and GitHub will provide you with a link to the repository. Once you have gotten your link to your GitHub repository you can enter the following commands in your local project.

git remote add origin https://github.com/githubusername/test.git 
git add .
git commit -m "Commit Message"
git push -u origin master

GitHub will prompt you with a single sign on message. Once you’re done with entering your GitHub credentials, you will have officially pushed your code to GitHub.

Wrapping UP

Now you should understand the basics of git as there is much more to learn and more advanced git commands. There is a free book on git called Git Pro with everything there is to know about git. Getting good at Git takes time and there is much more to learn which I will hopefully cover soon. You can also get started with contributing to open source and try to find a good first issue on GitHub.

Leave a Reply
Previous Post

Creative Ways to Hide Elements with CSS

Related Posts
Skip to content
Share via
Copy link
Powered by Social Snap
Close Bitnami banner
Bitnami