DEV Community

Danyson
Danyson

Posted on • Edited on

8 categories of Git

You can classify Git version control mechanism into 8 categories.

  1. Create
  2. Track
  3. Revert
  4. Update
  5. Publish
  6. View
  7. Branch
  8. Conflict

Below i have given classification of git mechanism along with their important commands.

Create

When it comes to files
git init
git add .
git add <filename>

When it comes to a repository
git clone ~/folder_A ~/folder_B
git clone git://<url>
git clone ssh://<url>

Track

Track is when we want to track our files for commit, update or delete operations.
git add <files> // adds files ready to be commited
git mv <prev_place> <target_place> //moves or renames a file, a directory, or a symlink
git rm <files> //removes files from working directory but adds it to the staging index
git rm --cached <files> //stops tracking but keeps files in working directory

Revert

We use revert when we want to do a new
commit that undoes previous commits.

git reset --hard
The --hard mode is not recoverable so if you want to recover your files after a git reset --hard, follow these Steps
Other modes of reset ->--soft, --mixed, --merge, --recurse-submodules

git revert branch
git commit -a --amend //replaces previous commit
git checkout <commit_id>

Update

Update is when we want to bring changes to our files.
git fetch //from the original repo that you have forked
git fetch remote
git pull //fetch & merge
git am //Apply a series of patches from a mailbox
git apply //Apply a patch to files and/or to the index

Publish

Publish is when we want to establish what we done with our files.
git commit -a //adds all changed files & commits all changes
git format-patch origin //create set of diffs
git push remote //push to origin or remote
git tag //mark current version

View

View is when we want to see the informations about our files.
git status
git diff <old_id> <new_id>
git log -p file|dir
git blame <file>
git show id //meta data & diff
git show < ID | tag | branch_name>
git branch //can list all branches both local and remote
git tag -l //shows list

Branch

The "branch" helps you to create, delete, and list branches.
git checkout branch //switch working dir to branch
git merge branch //merge into current branch
git branch branch_name //branch current
git checkout -b new_branch another_branch //branch new branch from another branch and switch to new branch

Conflict

Conflict arises when two files of same name have different contents so we need to check the difference and maintain our code base without conflicts.
git diff --base //compare working tree with base version
git diff --ours //compare working tree with our branch
git diff --theirs //compare working tree with their branch
git diff --staged //compare with staged changes
git diff HEAD
git diff <commit_1-sha> <commit_2-sha>
git diff <branch_1> < branch_2>
git log
gitk --merge

Reference

git documentation

Support Us on Buy Me a Coffee

Explore us on Doge Algo

Top comments (0)