Git

Introduction

Version control systems like Git are tools used to track changes to source code (or other collections of files and folders). As the name implies, these tools help maintain a history of changes; furthermore, they facilitate collaboration. Source : Missing-Semester/Git
Documentation

Introduction to git slides

Flow

Working dir --add--> Staging data --commit--> Local repo --push--> Remote Repository

Status

    

$ git status

Init

    

$ git init

Staging

Add changes to staging. '-p' for pruning through changes.
    

$ git add .
$ git add -p
$ git checkout -- .

Diff

Use `--staged` for changes in staging. Can also diff against different branches.
    

$ git diff
$ git diff --staged
$ git diff origin/master

Commit

Use `--amend -m` to update previous commit message

$ git commit -m "First Commit"
$ git commit --amend -m "An updated commit message"

Log

    

$ git log
$ git log --all --graph --decorate

Branches

Use '-b' tag to create a new branch
    

$ git checkout <commit-id>
$ git checkout -b <branch>

Merging

To merge branch A into B

$ git checkout <branch-B>
$ git merge <branch-A>

Revert

Use `-m` to revert a merge.

$ git revert <commit-hash>>

Reset

Go back in time
    

$ git reflog
$ git reset HEAD@{<id>}

$ git reset --hard HEAD~<number-of-commits-to-undo>
$ git push origin HEAD -f


Terminology

In case of merge conflict, resolve conflict manually / using VSCode / other tools.