Basics

working with local repositories

Getting and Creating Projects

git-init

Create an empty Git repository or reinitialize an existing one

git init [-q | --quiet] [--bare] [--template=<template-directory>]
	  [--separate-git-dir <git-dir>] [--object-format=<format>]
	  [--ref-format=<format>]
	  [-b <branch-name> | --initial-branch=<branch-name>]
	  [--shared[=<permissions>]] [<directory>]

git-clone

Clone a repository into a new directory

git clone [--template=<template-directory>]
	  [-l] [-s] [--no-hardlinks] [-q] [-n] [--bare] [--mirror]
	  [-o <name>] [-b <name>] [-u <upload-pack>] [--reference <repository>]
	  [--dissociate] [--separate-git-dir <git-dir>]
	  [--depth <depth>] [--[no-]single-branch] [--no-tags]
	  [--recurse-submodules[=<pathspec>]] [--[no-]shallow-submodules]
	  [--[no-]remote-submodules] [--jobs <n>] [--sparse] [--[no-]reject-shallow]
	  [--filter=<filter> [--also-filter-submodules]] [--] <repository>
	  [<directory>]

Snapshotting

git-add

Add file contents to the index

git add [--verbose | -v] [--dry-run | -n] [--force | -f] [--interactive | -i] [--patch | -p]
	  [--edit | -e] [--[no-]all | -A | --[no-]ignore-removal | [--update | -u]] [--sparse]
	  [--intent-to-add | -N] [--refresh] [--ignore-errors] [--ignore-missing] [--renormalize]
	  [--chmod=(+|-)x] [--pathspec-from-file=<file> [--pathspec-file-nul]]
	  [--] [<pathspec>ā€¦ā€‹]

git-status

Show the working tree status

git status [<options>] [--] [<pathspec>ā€¦ā€‹]

git-diff

Show changes between commits, commit and working tree, etc

git diff [<options>] [<commit>] [--] [<path>ā€¦ā€‹]
git diff [<options>] --cached [--merge-base] [<commit>] [--] [<path>ā€¦ā€‹]
git diff [<options>] [--merge-base] <commit> [<commit>ā€¦ā€‹] <commit> [--] [<path>ā€¦ā€‹]
git diff [<options>] <commit>ā€¦ā€‹<commit> [--] [<path>ā€¦ā€‹]
git diff [<options>] <blob> <blob>
git diff [<options>] --no-index [--] <path> <path>

git-commit

Record changes to the repository

git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]
	   [--dry-run] [(-c | -C | --squash) <commit> | --fixup [(amend|reword):]<commit>)]
	   [-F <file> | -m <msg>] [--reset-author] [--allow-empty]
	   [--allow-empty-message] [--no-verify] [-e] [--author=<author>]
	   [--date=<date>] [--cleanup=<mode>] [--[no-]status]
	   [-i | -o] [--pathspec-from-file=<file> [--pathspec-file-nul]]
	   [(--trailer <token>[(=|:)<value>])ā€¦ā€‹] [-S[<keyid>]]
	   [--] [<pathspec>ā€¦ā€‹]
  • To trigger a commit and kickoff workflow without having to make any change.

git commit --allow empty -m '<your_message>'

git-notes

Add or inspect object notes

git notes [list [<object>]]
git notes add [-f] [--allow-empty] [--[no-]separator | --separator=<paragraph-break>] [--[no-]stripspace] [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]
git notes copy [-f] ( --stdin | <from-object> [<to-object>] )
git notes append [--allow-empty] [--[no-]separator | --separator=<paragraph-break>] [--[no-]stripspace] [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]
git notes edit [--allow-empty] [<object>] [--[no-]stripspace]
git notes show [<object>]
git notes merge [-v | -q] [-s <strategy> ] <notes-ref>
git notes merge --commit [-v | -q]
git notes merge --abort [-v | -q]
git notes remove [--ignore-missing] [--stdin] [<object>ā€¦ā€‹]
git notes prune [-n] [-v]
git notes get-ref

git-restore

Restore working tree files

git restore [<options>] [--source=<tree>] [--staged] [--worktree] [--] <pathspec>ā€¦ā€‹
git restore [<options>] [--source=<tree>] [--staged] [--worktree] --pathspec-from-file=<file> [--pathspec-file-nul]
git restore (-p|--patch) [<options>] [--source=<tree>] [--staged] [--worktree] [--] [<pathspec>ā€¦ā€‹]

git-reset

Reset current HEAD to the specified state

  • --soft - move changes back to staging(index) area.

    This is helpful if you want to take a group of commits and squash them into a single larger commit.

  • --mixed (default) - move changes back to local working directory.

    this is helpful if you want to take a group of small commits and combine some of the changes to make larger commits, and you can also use it to make additional changes to the files and then re-create the commit history.

  • --hard - moves changes to the trash.

git reset [-q] [<tree-ish>] [--] <pathspec>ā€¦ā€‹
git reset [-q] [--pathspec-from-file=<file> [--pathspec-file-nul]] [<tree-ish>]
git reset (--patch | -p) [<tree-ish>] [--] [<pathspec>ā€¦ā€‹]
git reset [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [<commit>]

git-rm

Remove files from the working tree and from the index

git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch]
	  [--quiet] [--pathspec-from-file=<file> [--pathspec-file-nul]]
	  [--] [<pathspec>ā€¦ā€‹]
Snippets
  • to untrack the file and delete it from the repository.

git rm <file_name>
  • to untrack the file from git but keeps the file in the repository.

git rm --cached <file_name>

git-mv

Move or rename a file, a directory, or a symlink

git mv [<options>] <source>ā€¦ā€‹ <destination>

Branching and Merging

  • branch - List, create, or delete branches

  • checkout - Switch branches or restore working tree files

  • switch - Switch branches

  • merge - Join two or more development histories together

  • mergetool - Run merge conflict resolution tools to resolve merge conflicts

  • log -

  • stash - Stash the changes in a dirty working directory away

  • tag - Create, list, delete or verify a tag object signed with GPG

  • worktree - Manage multiple working trees

git-branch

git-log

Show commit logs

Snippets

This makes the log more readable and colorful.

git log --pretty=online --graph --decorate -all

=========================================================================

git branch --merged master | grep -v 'master' | xargs -n 1 git branch -d

Recursive Merge A recursive merge occurs when your feature branch doesnā€™t have the latest version of code in the branch youā€™re trying to merge into.

Fast-Forward Merge A fast-forward merge occurs when there have been no new commits, other than the ones youā€™re trying to merge, on the original branch since you created your feature branch from it.

Branches

Rename Branch

  • If you want to rename your current HEAD branch,

git branch -m <new_branch_name>
  • If you want to rename a different local branch that is not currently checked out,

git branch -n <old_branch_name> <new_branch_name>

Switch Branch

git checkout <branch_name>
git switch <branch_name>

Track Branch

  • when we want to track a remote branch,

git branch --track <new_branch> origin/<base_branch>
  • when we want the same name for the local branch as the remote branch then,

git checkout --track origin/<base_branch>

Rebase Branch

  • first switch to the branch that should receive changes

git switch <feature_branch>
  • then execute the "rebase" command with the name of the branch that contains the desired changes

git rebase <main_branch>

Compare Branch

  • to compare two branches,

git log <branchA>..<branchB>
  • to compare local and remote branch,

git log <local_branch>..origin/<remote_branch>
  • to see the actual changes that make up those differences between branches,

git diff <branchA>..<branchB>

Last updated