Cancel

Git and GitHub Guide

It is a Version Control System (VCS) where you can track all the changes. It enables multiple engineers to simultaneously work on a single project.

Architecture Diagram

Prerequisites

Most of the Operating Systems have git installed but if you don’t have one install it, its easy process.

Linux (Debian, Ubuntu, Mint)

1
sudo apt install git

MacOSx (don’t forget to install Homebrew if you don’t have one)

1
brew install git

Windows (don’t forget to install Chocolatey if you don’t have one)

1
choco install git

Centralized System vs Distributed System

The concept of a centralized system is that it works on a Client-Server relationship. The repository is located at one place and provides access to many users to change files on Github directly. Whereas, in a Distributed System, every user has a local copy of the repository in addition to the central repo on the server side.

Working Directory vs Staging Index

  • Working Directory: files that you are currently working on.
  • HEAD: Last commit or snapshot.
  • Staging Index is a single, large, binary file of your changes. It’s a next commit or snapshot.

Image

Tracked vs Untracked

  • Tracked files means files that were in the last snapshot, they can be unmodified, modified, or staged.
  • Untracked files means any files in your working directory that were not in your last snapshot and are not in your staging index.

Useful Cheatsheets

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
git init # initialize git project.
git status # check status that how many (un)tracked files are detected by git.
git add # add pending changes from the working directory to the staging area.
git commit # move files from the staging area to a snapshot of the changes made.
git push # upload local files to a remote repository.

git rm --cached .

# for staging index untracked/tracked files
git restore --staged .

# for tracked files
git restore .

# for untracked files
git clean -dfx .

# get to know the latest tag or commit-hash
git describe --tags --abbrev=0 --always

CRUD Operations via Github

Get more information from Github’s Reference REST API

  1. In Github CRUD stands for Create/POST, Read/GET, Update/PATCH and Delete/DELETE.
  2. GIT Cheatsheets can be found here (https://deletify.app/tech-world/learn-real-power-of-curl-command-with-cheatsheets.md/)
1
2
GITHUB_TOKEN=<YOUR_TOKEN>
GITHUB_API_URL=https://api.github.com

GET

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# return all items
curl \
  -sG -X GET \
  -H "Authorization: token $GITHUB_TOKEN" \
  -H 'Content-Type: application/json' \
  $GITHUB_API_URL/user/repos \
  -d per_page=2000

# return single item only
curl \
  -X GET \
  -H "Authorization: token $GITHUB_TOKEN" \
  -H 'Content-Type: application/json' \
  $GITHUB_API_URL/repos/{username}/{repo_name}

POST

1
2
3
4
5
6
7
8
9
10
11
12
13
14
curl \
  -X POST \
  -H "Authorization: token $GITHUB_TOKEN" \
  -H 'Content-Type: application/json' \
  $GITHUB_API_URL/user/repos \
  -d '{
    "name": "NAME",
    "description": "DESCRIPTION",
    "private": true,
    "has_issues": false,
    "has_projects": false,
    "has_wiki": false,
    "delete_branch_on_merge": true
  }'

PATCH

1
2
3
4
5
6
curl \
  -X PATCH \
  -H "Authorization: token $GITHUB_TOKEN" \
  -H 'Content-Type: application/json' \
  $GITHUB_API_URL/repos/{username}/{repo_name} \
  -d '{"private": false}'

DELETE

1
2
3
4
5
curl \
  -X DELETE \
  -H "Authorization: token $GITHUB_TOKEN" \
  -H 'Content-Type: application/json' \
  $GITHUB_API_URL/repos/{username}/{repo_name}

Clone all public & private repos from Github API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# clone public & private repos
curl \
  -sG -X GET \
  -H "Authorization: token $GITHUB_TOKEN" \
  -H 'Content-Type: application/json' \
  $GITHUB_API_URL/user/repos \
  -d per_page=2000 \
  | grep ssh_url \
  | awk -F '"' '{print $4}' \
  | xargs -n 1 git clone

# clone public repos only
curl \
  -sG -X GET \
  -H 'Content-Type: application/json' \
  $GITHUB_API_URL/users/{username}/repos \
  -d per_page=2000 \
  | grep ssh_url \
  | awk -F '"' '{print $4}' \
  | xargs -n 1 git clone

Reference