Practical introduction to git and GitHub/GitLab

What is a VCS and how it makes life easier?

What is a VCS and how it makes life easier?

VCSVersionControlSystem
a system that records changes to a file (or directory) over time so that you can recall specific versions later
You can version control all kind of work you do:
Poetry
Images
Music
Programming
...
Easily share your work with other team members

VCS Types

local
really, not used by companies
centralized
like subversion
decentralized (distributed)
like git

Local VCS

Centralized VCS

Distributed VCS

Installing and configuring git.

Installing and configuring git.

Installing Git
Note for Windows Users - install from gitforwindows.org which also brings a nice features like git bash, shell integration and more

Creating git repository

Creating git repository

What is a git repository?

We can tell git to track the changes and keeps a history of them for any folder of our computer.
The tracked changes per folder with all the history will be saved by git in .git folder, which is called a git repository.
If you delete your git repository, i.e. the .git folder, then you'll delete your project’s history

Initialize a Git repository

If we want git to track the changes in a folder, first we need to tell git to initialize in that folder a git repository:

				# make sure you are in your root project folder:
				pwd
				# the output should be the full path you are currently in

				# initialize git repo for that folder:
				git init
		

Setup your git identity

Before your first commit, you have to provide your user name and email.
This information will be stamped in each of you commits and other git activities you make.

			# per project - make sure you type next commands while you are in the root git folder
			git config user.email "your_mail@mail.com"
			git config user.name "User Name"

			# or if you wish to setup your git identity on global level, you can use the --global option:
			git config --global user.email "your_mail@mail.com"
			git config --global user.name "User Name"
		

Create a git commit

Create a git commit

The Three States in git

Stage changes

Also known as adding files content to the index


				# to stage changes only in certain file/path:
				git add path

				# to stage all changes (including deletion of files):
				git add -A
			
The staging area contains all the changes that will be added to the local repository after the commit

What is the difference between various git add options? check this stackoverflow answer
You can remove (unstage) files from the index, with:

					git rm file
				

Commit changes to local repository


				# commit files - the default system editor for git will be opened to write your Commit Message
				git commit

				# commit and write the Commit Message
				git commit -m'Your Commit Message'
			
If you want to make VSCode your default git editor:

				git config --global core.editor "code --wait"
			

All steps at a glance

Each time, you want to commit (save) your changes, you'll need to:

			# stage all changes:
			git add -A

			# commit and write the Commit Message
			git commit -m'Your Commit Message'
		

Get insights of your repo

Get insights of your repo

Git status and commit history

We often need to check a git repository status (if there are any non-commited changes and etc.)

				git status
			
Or to check the repository commits history

				git log
			

Working with branches.

Working with branches.

Reference: Git Branch
Live demo.

Using GitHub as a remote service

Using GitHub as a remote service

What is GitHub?

GitHub is a code hosting platform for version control and collaboration. It lets you and others work together on projects from anywhere.
GitHub is not just a remote for storing git repositories, it also provides features like:
Fork a repo - you can create a new project based off of another project that already exists.
Pull Requests - if you have worked on a forked repo, you can contribute your changes to that project by opening a Pull Request. The authors of the original repository can see your work, and then choose whether or not to accept it into the official project.
GitHub alterantives: GitLab, Bitbucket

Connect your local git repository to your GitHub repository

Create a local git repository (if you haven't done so)
Create a corresponding GitHub repo (preferably with the same name, as the root folder of your local repo).
Follow the instruction given in github page, in the section: "…or push an existing repository from the command line" section.
Or read the github docs: Adding an existing project to GitHub using the command line

Pushing changes to GitHub

Each time, you want to "store" your local repo changes to your remote GitHub repo, you'll need to push that changes

				# push your default local branch (master/main) to the corresponding remote (origin):
				git push
			

Resources

Recommended Videos

Git Tutorial for Beginners: Command-Line Fundamentals

Github Tutorial For Beginners - Github Basics for Mac or Windows & Source Control Basics

These slides are based on

customised version of

Hakimel's reveal.js

framework