Introduction

git is a version control system that is widely used in software development. It is a distributed version control system that allows developers to work on the same project at the same time. It is also a powerful tool that can be used to track changes in files and revert to previous versions.

Git Workflow

Git Workflow

Git Stages

Git Stages Staging area is a concept that is often used in git. It is a temporary area where files are stored before they are committed to the repository.

Usage

Create a new repository

# create a new directory
$ mkdir myproject
# change directory
$ cd myproject
# initialize the directory as a git repository
$ git init

Clone an existing repository

# clone the repository
$ git clone https://github.com/ian729/silver-umbrella.git

Add files to the staging area

# add all files in the current directory to the staging area
$ git add -A
# add a specific file to the staging area
$ git add README.md

Commit changes

# commit all changes in the staging area
$ git commit -m "commit message"

Push changes to remote repository

# push changes to remote repository
$ git push

Mixed reset(default)

# reset all changes in the staging area
$ git reset
$ git reset --mixed
# reset a specific file in the staging area
$ git reset README.md

Hard reset

# hard reset all changes in the staging area
$ git reset --hard
# hard reset a specific file in the staging area
$ git reset --hard README.md

Soft reset

# soft reset all changes in the staging area
$ git reset --soft
# soft reset a specific file in the staging area
$ git reset --soft README.md

Difference between three reset types

| Type | Staging Area | Working Directory | | — | — | — | | Mixed | Reset | Not Reset | | Hard | Reset | Reset | | Soft | Not Reset | Not Reset |

Git Reset

Revert changes

# revert all changes in the staging area
$ git checkout -- .
# revert a specific file in the staging area
$ git checkout -- README.md

Squash commits

# squash the last 3 commits into one
$ git rebase -i HEAD~3

Pull changes from remote repository

# pull changes from remote repository
$ git pull
# pull changes while rebasing local commits
$ git pull --rebase

Rebase

References

  1. Git Documentation
  2. Git Tutorial
  3. Git Cheat Sheet
  4. Git Cheat Sheet
  5. Git Cheat Sheet