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.
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 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
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.
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"
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.
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.
After the initial commit we make more changes to ‘text1.txt’ and commit new changes as shown in figure below.
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>
The next important concept to understand is branch. Essentially, we start working in the Master branch. We can verify this by running following command.
The figure below shows that we are working in the Master branch (* represent the current 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.
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>
Let’s create a new file and commit that as shown in figure below.
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>
|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