Ultimate Guide for Git and Github/Gitlab

Aug 22, 2022

#git


Prerequisite

  • Install git in your machine
  • Add ssh key and gpg singing key in your github/gitlab account Github doc

Start with git

# clone a repo
git clone https://github.com/torvalds/linux.git
cd linux
# or make a local
mkdir <repo_name>
cd <remp_name>
git init .

# change some files and add to git
git add <file_name>  # or git add .
# commit the changes
git commit -m "your message"
# or add and commit at a same time
git commit -am "your message"

# push to remote
git push origin main  # origin is the remote repo

Git pre-commit hook

This helps to git user to run certain command (like formatting, type checking etc.) before commiting.
Install pre-commit from pip package manager pip install pre-commit and run

pre-commit install

Create a file .pre-commit-config.yaml in your git root directory and add these lines

repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v2.3.0
    hooks:
    -   id: check-yaml
    -   id: end-of-file-fixer
    -   id: trailing-whitespace

See offical website for more info Link

Run first pre-commit to all files

pre-commit run --all-files

after that git commit will automatically run pre-commit before every commit. Commit fails when changes are not passed according to pre-commit configuration file. It will throw the errors, fix the error and recommit again.

Structure template

Git repo structure template

main
    --> <new_branch>
        --> <new_features>
# update the latest changes
git pull origin main
# create new branch
git checkout -b <new_branch>
# work on this branch and push to origin
git push origin <new_branch>

# change to new_branch and then switch to main branch where you want to merge with
git switch main
# merge the head branch into the base branch
git merge <new_branch>
# push the changes to origin main
git push -u origin main

Delete branch

git push -d <remote_name> <branch_name>
git branch -d <branch_name>

Handling branch

Hande Branch merging in recommended way

# Rename
git branch -m <old_branch> <new_branch>
branch -r # list branch in remote
git branch -l  # list branch in local
# push new branch
git push origin -u <new_branch>
# push new branch
git push origin --delete <old_branch>

Git Tag

git tag -a 1.0.0 -m "This is the first version  of this package"
# show all the tags
git tag
# gathering information of a specific tag
git show 1.0.0
# push the tag to remote origin
git push origin 1.0.0

Git submodule

Sometimes you need to add a subrepo to your main repo for development purpose.
For more details follow this link

git submodule add https://github.com/<user_name>/<sub_repo>.git
git status
git commit -am 'add submodule'
git status
git push origin main

Update submoudle

cd sub_repo
git fetch
git merge origin/main
# or
git submodule update --remote sub_repo
# cd ../
git commit -am 'Updated sub_repo with remote repo'
git push origin main

Github actions

A workflow can be automate (e.g testing, building process etc.) from github action when

  • you make changes in your repo
  • someone make pull request

Create a directory .github/workflows and make a test.yml file

name: GitHub Actions Test
on: [push]
jobs:
  Explore-GitHub-Actions:
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository code
        uses: actions/checkout@v3
      - run: echo "Installting dependencies"
      - run:
          pip install -r requirements.txt
      - name: Test
        run:
          pytest
      - run: echo "This job's status is ${{ job.status }}."

Contributing

# Adding upstream url
git remote add upstream git@github.com:<user_name>/<upstream_repo>.git
# upstream --> <upstream_repo> repository
# orign --> personal fork
# pull the latest changes
git checkout master
git pull upstream master
# create new branch
git checkout -b <your_branch_name>
# work on your branch
git push origin <your_branch_name>
# go to github or gitlab to pull request to specific branch

Git server

Bare repo

Repo without working directory

mkdir <repo_name>
cd <repo_name>
git init --bare

Follow this link