dinsdag 8 december 2020

Introduction to GIT

Introduction

Today, a quick introduction in Git. In this blogpost a couple of familiar Git commands will pass and I will demonstrate the commands in small demos.


Installation

But first download the software of Git to your local machine and install the software locally. Verify the installation by using the following command : "Git version". This will return the current version of Git. 



git version


In my case this results in the following version 2.28.0.



Demo

For this blogpost I created a folder with some files and I will experiment with these files : FileA, FileB and FileC.



And in every file I inserted the following phrase. This is and example for File A:


This is File A


Initialize the repository

When the file are there, the first statement that needs execution is Git init. This command creates an empty Git repository (basically a .git directory with subdirectories for objects, refs/heads, refs/tags, and template files. An initial HEAD file that references the HEAD of the master branch is also created.


Git init


A message appears when the initialization is ready. 



Running git init in an existing repository is safe. It will not overwrite things that are already there. The primary reason for rerunning git init is to pick up newly added templates.


Add the files to the repository

With git add you can add the files to the l(local) repository. It typically adds the current content of existing paths as a whole. This command updates the staging area (index) using the current content.


git add .


And now the files are added to the staging area. The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, and which haven’t, and which files aren’t being tracked by Git. The Git status output does not show you any information regarding the committed project history. For this, you need to use git log.


git status


Here the result of the git status. The files are in the staging area but they need to be committed.



The "commit" command is used to save your changes to the local repository. Creating a new commit containing the current contents of the index and the given log message describing the changes. The new commit is a direct child of HEAD, and the branch is updated to point to it,

git commit -m "new files"


Here the result of the commit of the files.



Now let's check the commit with "git status".

git status




So what have we done so far? We have added the files from the working directory to the staging area (aka index) with the "git add" command and with "git commit" we added the files from the staging area to repository.


Check this out with the "git log" command.

git log




Change the file

Now let's edit the file FileA with some changes.




Save it and check the status with "git status".

git status


Git status reports that FileA is changed and these changes are not added and commit to the repository.



Let's add the file to the staging area with "git add".

git add FileA.txt
git status


"Git status" reports that the file is changed and add to the staging are, now. But they need a commit to the repository.




So, let's commit the changed to the repository with the "git commit" command.

git commit -m "FileA is changed"
git status
git log


And here is the result of the "git log". There are now two commits and HEAD is poinitng to the master. 



The next step is changing fileC and add this file to the repository.



Execute the following commands and see the results. One command I haven't explained yet is git diff. From the Git site : "Show changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, changes resulting from a merge, changes between two blob objects, or changes between two files on disk".

git status
git diff
git add FileC.txt
git commit -m "FileC- Changed"
git log


And now there are three commits and changes



It is also possible to go back one (or more) version of the code. You can execute this with
"git checkout" command.

git checkout b81a42d23c48657dc976372d989a09e9cb6022ed


And now the original file is back.



git log




It's not possible to see the successors of the commits with "git log", now. Now let's go back to the last changes 

git checkout b81a42d23c48657dc976372d989a09e9cb6022ed


And now the last commit file is back



And with "git log" we see that we are back on the latest commit of the master branch.

git log








Used resources 

For this blogpost I've used the following resources :

Final Thoughts

This is a small introduction to git. 

Hennie

Geen opmerkingen:

Een reactie posten