Git 101 Tutorial

Git 101 Tutorial

Git Introduction

Git is a powerful version control system used for managing and maintaining source code. Git allows multiple programmers to work on the same copy of code without stomping on each other work. Git can also be used to not only manage code but can also be used to manage other documents such as word, excel etc. This blog post will provide a quick introduction to get started with Git. Git can be easily installed without requiring any pre-requisites. For this blog post I simply installed Windows 10 Enterprise evaluation copy and then installed Git on it. Git installation file are available on Git.

During the installation process I simply hit next to everything. Once the install finishes you can open a command prompt and quickly test if the install was successful by issuing the git version command as shown below.

git version

Figure1: Git Version

Next, we need to provide Git with some basic information about us, Git need this to keep track of who performed the changes to the code. *Note: This information is not sent back to Git but used by the local installation of Git.

git config --global user.email "browninfosecguy"
git config --global user.name "browninfosecguy"

Git Repository

Git is based on the concept of repository. Repository is like a container which hold your code. The repository also contains meta data about the repository. This meta data contain information on what changes were made, who made those changes, when the changes were made etc.

We create a repository by first creating a new folder and then use Git to initialize a new repository. The folder just acts as a place holder to keep all our files and folders that we might want to add to our repository.

 New-Item -Type Directory -Path ".\MyCode"
 cd .\MyCode\
 git init
 git status

Figure2: Initialize new Git repository

If we create any file or folders in the MyCode folder and check the status, Git will let us know about changes we need to commit to the repository as shown in figure below.

Figure3: Git warning about untracked file

Git Add & Commit

In order to add files and folders to our repository we need to first add then to our staging area and then commit them to the repository. It’s a two-step process as shown below, here we are first adding our PowerShell script named ‘SysInfo.ps1’ to the staging area and then committing it to our repository.

git add .\SysInfo.ps1
git commit .\SysInfo.ps1 -m "Committing my first change"

Figure4: Initial commit

Any changes me make to our script can be viewed by issuing ‘git log’ command as shown below. The ‘git log –oneline’ command will get rid of extraneous information and truncates the hash. The hash value is important as it acts like a marker for Git to keep track of all the commits made to the repository.

git log

Figure5: Commit history

Git Reset

There might be a situation where you would like to revert back your changes and go back to a previous version of the repository. We can easily accomplish this with the help of ‘git reset’ command. Let’s look at an example, we added two files named file1.txt and file2.txt as shown in figure below to our repository.

Figure6: Initial commit

After the initial commit we make more changes to ‘text1.txt’ and commit new changes as shown in figure below.

Figure7: Commit new changes

In order to revert back these new changes, we issue the ‘git reset’ command as shown below.

git log --oneline
git reset --hard <truncated hash>

Figure8: Revert back the changes

Git Branch

The next important concept to understand is branch. Essentially, we start working in the Master branch. We can verify this by running following command.

git branch

The figure below shows that we are working in the Master branch (* represent the current branch).

Figure9: Commit history of Master branch

The Master branch is like the main code branch, in large development shops the developers usually create a new branch of the Master branch and then work on this new branch in their local environment. That way the developers can work on new features locally without introducing any stability issues or bugs in the Master branch. When the feature is ready the changes can easily be merged back to the Master branch.

Figure10: Git Branches

We can easily create a new branch from the Master branch by using the git branch command as shown below

git branch <name of new branch>

In order to start working in the new branch we need to switch from Master branch to this new branch. We can do that by using git checkout command as shown below

git checkout <name of new branch>

Figure11: Creating a new branch

Let’s create a new file and commit that as shown in figure below.

Figure12: Commit a new file to newly created branch

When we are ready to merge the new branch to the master branch, we switch back to the Master branch and issue the ‘git merge <name of new branch>.

git checkout master
git merge <name of new branch>

Figure13: Merge the changes back to Master branch

To summarize:

Git Operation Git Command
Configure user git config –global user.email “browninfosecguy”
Configure email git config –global user.name “browninfosecguy”
Initialize a new Repository git init
Status of the branch git status
Add to the Repository git add <name of file>
Commit to the Repository git commit <name of file> -m “MESSAGE”
View commit history git log or git log –oneline
Create a new branch git branch <name of new branch>
Move Between branches git checkout <name of branch>
View current branch git branch
Merge a child branch to Master (Should be run from Master branch) git merge <name of child branch>
Revert back to an earlier commit git reset –hard <truncated hash>

I hope you find this post helpful. Please feel free to reach out if you have any comments or suggestions. ~Sonny

Share: Twitter Facebook
Sonny's Picture

About Sonny

Sonny is a CyberSecurity enthusiast who currently resides in Halifax, Nova Scotia. Apart from CyberSecurity Sonny is pasionate about PowerShell.

Nova Scoita, Canada https://browninfosecguy.com