DEV Community

Cover image for GIT & GITHUB AS A BEGINNER
Amos Kiarie
Amos Kiarie

Posted on

GIT & GITHUB AS A BEGINNER

DEFINATION OF TERMS - GIT

GIT - This is a free and opensource distribute version controll system. It runs locally on a developers machine but may also be hosted centrally or remotely in a specified server within an organization to aid in version control.

its never a good idea to explain terms with more jargon. While reading the explanation above, I have realized that I did not mention what a version control system is.

Version Control - In technology, this is the practice of keeping track of a software system's many versions and configurations over time.
This task is normally carried with the aid of a version controll system.

AN EXAMPLE

The best way to explain how git works is with a simple python example.
On my first day in a data engineering class we created a small python file and named hello world. inside the file was this line of code.
print(" Hello World")

Towards the end of the lesson we chnaged the line of code to

'print(" Hello World, My name is AMOS and I am a data engineer.")

The lecturer gave us an assignement to modify the print statement above and make it print in two lines. After completeing this I had the following line of code.
Print(" Hello World! \n My name is AMOS and I am a data engineer.")
After this exercise, I realized I needed to keep track of my small hello world project how it started and how it had evolved. I ended up creating 3 folders;

  • hello_world_1
  • hello_world_2
  • hello world_3

Hello World Project

In each of the folders, I saved a file named hello_world.py with the sample lines of code written above.

This seemed like a great idea at the time, after all if all you have is a hammer, everything looks like a nail.

What I had created here was a version control system of sorts, this alowed me to keep track track of chnages made to my python project. There are many problems with this approach;

  1. The entire project is hosted on one machine (my laptop).
  2. Single point of failure (losing the laptop means losing all the code).
  3. Collaboration with other developers is very challenging (share files as email attachments).
  4. Its hectic to manage especially with big projects.
HELLO WORLD IN GIT

GIT solves all these problems and provides us with many more features. To experience the power of GIT we have to download and install this software in our computer.

Installing git.
  1. Visit the git-scm site and download the latest release for your operating system. I am on windows and so I got version Git-2.52.0-64-bit. GIT Webpage
  2. Double click on the downloaded file to open it. Being an executable, opening the file will run the installer for GIT.
  3. Your computer will ask you if you really want to allow the file that you have just openned to make chnages to your computer. click allow to agree.
  4. Read the GNU License agreement and click next if you agree to comply with the terms. GNU License
  5. On the next screen, the installer will ask you to select a folder in which to install GIT. leave it as default and click next. Selecting folders
  6. On the next screen, the installer will ask you to select the git components that you wish to install. leave it as default and click next. Select Components
  7. On this screen, the installer will ask where it should place GIT's shortcut, leave it as default and click next. Git Shortcuts
  8. On the next screen, the installer asks which code editor you would like to use for GIT, choose an appropriate one depending on your preference, I chose VS Code as depicted below and click next. Default Editor
  9. On the next screen, the installer asks how you would like to name your initial branch in git repositories, leave it as default and click next. Default Branch
  10. On the next screen, the installer asks how you would like to use Git from the command line. Leave it as default and click next. Git from the command line
  11. On the next screen, the installer will ask you which secure shell client you would like to use. Choose Use bundled OpenSSH and click next. Secure Shell
  12. On the next screen, the installer will ask you which SSL you would like to use, leave it as default and click next. GIT SSL preference
  13. On the next screen, the installer will ask you to choose a line ending conversions. For reasons will not get into here ensure you select checkout Windows-style, commit Unix-style and click next. Line Ending conversions
  14. On the next screen, the installer will ask which terminal emulator you would like to use,leave it as default and click next. Terminal Emulator
  15. On the next screen, the installer will ask you to choose the default behaviour of git pull choose rebase and click next. Git Pull - Rebase
  16. On the next screen, the installer will ask you to choose your credential manager of choice. leave it as default and click next. Credential Manager
  17. On the next screen, the installer will ask you to choose which features you would like to enable, leave it as default and click on the install button. Enable File System Caching
  18. The installer will run for about 30 seconds or more depending on the speed of your computer. Installer Running
  19. The installer will show you the screen below. uncheck view release notes, and click finish. Finish

Now that we have GIT installed we need to setup a few things;

  1. A username. Open git bash on your computer, on the prompt type the following command replacing myName with your actual name. git config --global user.name "myName" and press enter.
  2. An Email. On git bash type the folowing command replacing myemail@domain.com with your actual email address. git config --global user.email "myemail@domain.com"

With that done, we need to create a repository.

CREATING A REPO

Think of a repository as a project, a container that contains all the code we have written so far upto this point and any other code that will ever write.

  1. Open the hello world project. Right click on the file explorer and choose open git bash here. Open Git Bash here
  2. Type the command git init on git bash. You will get a message, initialized an empty Git repository ......... Create a new file named hello_world.py touch hello_world.py this file will not be within any of the folders we had created earlier. Edit the new file by typing nano hello_world.py, this will open up a simple text editor, write your code print('Hello World') here and click ctrl + O folowed by enter to save it. Click ctrl + x to exit the edit.
  3. Stage the new file by typing the command git add hello_world.py.
  4. Commit the file by typing the command git commit -m 'initial commit'.
  5. Make changes to the same file by folowing the steps 2 , 3 and 4 above updating the contents of the file to 'print(" Hello World, My name is AMOS and I am a data engineer.") the commit comand to git commit -m 'added my name'.
  6. Follow step 5 above updating the contents of the file to Print(" Hello World! \n My name is AMOS and I am a data engineer.") and the commit command to git commit -m 'added new line'.
  7. Type git log --oneline to see the history of the project. Git Log

DEFINATION OF TERMS - GITHUB

This is a web based cloud platform where developers and artists come together to create software. As the name implies, it hosts a tonne of git projects but also provides other features such as CI-CD rule based collaboration, 2 factor authentication etc.

Creating a Github Account

head over to https://github.com/ and create an account.

On your localmachine;

  1. Ensure SSH is running $ eval "$(ssh-agent -s)".
  2. Generate an SSH Key pair ssh-keygen -t rsa -b 4096 -C "myemail@domain.com".
  3. Add a passphrase for securing the SSH key pair.
  4. Add the SSH ID to the local SSH key manager. ssh-add /c/Users/user/.ssh/id_rsa.
  5. Sometimes this step will return an error, repeat step 1 above.
  6. Copy the public key on windows clip < /Users/user/.ssh/id_rsa.pub On Linux open the file via Nano and copy from there.
  7. Add the new key to the GitHub account and test the connection by executing $ ssh -T git@github.com on git bash.
  8. Git will ask for your passphrase and then return a message as shown below;
user@machine MINGW64 ~/Documents/Hello_world (master)
$ ssh -T git@github.com
Enter passphrase for key '/c/Users/amoh/.ssh/id_rsa':
Hi [github_user_name]! You've successfully authenticated, but GitHub does not provide shell access.

Enter fullscreen mode Exit fullscreen mode

Next article:

** Pushing your Local project to Github.**

Keep on keeping on.


Signed kiarieamos

Top comments (0)