---
description: working with local repositories
---

# Basics

## Getting and Creating Projects

### git-init

> Create an empty Git repository or reinitialize an existing one

```git
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
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
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
git status [<options>] [--] [<pathspec>…​]
```

### git-diff

> Show changes between commits, commit and working tree, etc

```git
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
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
git commit --allow empty -m '<your_message>'
```

### git-notes

> Add or inspect object notes

```git
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>…​]
```

<details>

<summary>Snippets</summary>

* 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>
```

</details>

### 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` -&#x20;
* `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

<details>

<summary>Snippets</summary>

This makes the log more readable and colorful.

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

</details>

\=========================================================================

```
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.

![](https://1151231797-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQAaqgCwmb83T8T0x2j%2Fuploads%2FLXv9X3iERVHU9blkeVpR%2Fimage.png?alt=media\&token=e9fb1ce0-10ff-4614-89ee-4ba12beb08be)

**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.

\


![](https://1151231797-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQAaqgCwmb83T8T0x2j%2Fuploads%2FieDUhV5xUY87hQCt20Jl%2Fimage.png?alt=media\&token=cf3f7aa0-f8e4-404a-8a82-63d8b6c9cb7b)



## 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&#x20;

```
git rebase <main_branch>
```

### Compare Branch

* to compare two branches,

```
git log <branchA>..<branchB>
```

* to compare local and remote branch,

```git
git log <local_branch>..origin/<remote_branch>
```

* to see the actual changes that make up those differences between branches,

```
git diff <branchA>..<branchB>
```
