DEV Community

Danyson
Danyson

Posted 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>
Enter fullscreen mode Exit fullscreen mode

When it comes to a repository

git clone ~/folder_A ~/folder_B
git clone git://<url>
git clone ssh://<url>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Revert

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

git reset --hard
Enter fullscreen mode Exit fullscreen mode

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

git revert branch

git commit -a --amend //replaces previous commit

git checkout <commit_id>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Reference

git documentation

Personal blog@danyson.github.io

Discussion (0)