> ## Documentation Index
> Fetch the complete documentation index at: https://docs.americ.io.vn/llms.txt
> Use this file to discover all available pages before exploring further.

# Essential Git

> Description of your new file.

> 🗎 Refs: \
> [📜Official Document](https://git-scm.com/doc) \
> [Atlassian Git Tutorial](https://www.atlassian.com/git/tutorials) \
> [Learn Git Branching](https://learngitbranching.js.org/) \
> [Visual Git](https://ndpsoftware.com/git-cheatsheet.html#loc=index;)

## **Configuring user information**

### Set global name

```shell theme={null}
git config --global user.name "Americio Hoag"
```

### Set global email

```shell theme={null}
git config --global user.email "mediocreDev@americio.dev"
```

### Enable automatic command line coloring

```shell theme={null}
git config --global color.ui auto
```

## **INIT/CLONE REPO**

### Initialize a Git repository

```shell theme={null}
git init
```

### Clone remote repository

```shell theme={null}
git clone https://github.com/OliverKain/obsidian-vault
```

## **STASH**

### Stash changes

```shell theme={null}
git stash
```

### List stash

```shell theme={null}
git stash list
```

### Pop stash

```shell theme={null}
git stash pop
```

### Drop the top of stash

```shell theme={null}
git stash drop
```

## **STAGE & COMMIT**

### Show modified files in the working directory

```shell theme={null}
git status
```

### Stage file(s)

```shell theme={null}
git add path/to/file1.md [path/to/file2.md path/to/file3.md]
```

### Un-stage file(s)

```shell theme={null}
# use git-restore
git restore --staged path/to/file1.md [path/to/file2.md path/to/file3.md]

# use git-reset
git reset path/to/file1.md [path/to/file2.md path/to/file3.md]
```

<Info>
  More advanced usage for git-reset [here](https://git-scm.com/docs/git-reset)
</Info>

### Show changed, not staged differences

```shell theme={null}
git diff
```

### Show staged, not committed differences

```shell theme={null}
git diff --staged
```

### Commit staged changes

```shell theme={null}
git commit -m "Add new .md files"
```

## **BRANCH & MERGE**

### List branches

```shell theme={null}
git branch # * indicates current branch
```

### Branching at the current commit

```shell theme={null}
git branch new-branch-name
```

### Checkout a branch

```shell theme={null}
git checkout existing-branch
```

### Merge a branch into the current branch

```shell theme={null}
git merge need-to-merge-branch
```

## **REMOTE INTERACTION**

Retrieving updates from another repository:

* **`git remote add [alias] [url]`**: Add a Git URL as an alias.
* **`git fetch [alias]`**: Fetch down all branches from that Git remote.
* **`git merge [alias]/[branch]`**: Merge a remote branch into your current branch to bring it up to date.
* **`git push [alias] [branch]`**: Transmit local branch commits to the remote repository branch.
* **`git pull`**: Fetch and merge any commits from the tracking remote branch.

## **(WIP) TRACKING PATH CHANGES**

Versioning file removals and path changes:

* **`git rm [file]`**: Delete the file from the project and stage the removal for commit.
* **`git mv [existing-path] [new-path]`**: Change an existing file path and stage the move.

## **(WIP) REWRITE HISTORY**

Rewriting branches and updating commits:

* **`git rebase [branch]`**: Apply any commits of the current branch ahead of specified one.
* **`git reset --hard [commit]`**: Clear staging area, rewrite working tree from specified commit.

## **(WIP) INSPECT & COMPARE**

Examining logs, diffs, and object information:

* **`git log`**: Show the commit history for the currently active branch.
* **`git log branchB..branchA`**: Show commits on branchA that are not on branchB.
* **`git diff branchB...branchA`** : Show differences in content between two branches.

## **(WIP) IGNORING PATTERNS**

Preventing unintentional staging or committing of files:

* Use `.gitignore`: Save patterns as strings or wildcards to ignore specific files or directories.

This cheat sheet serves as an essential guide for both beginners and experienced users to navigate common Git commands effectively.

### Signing Git commits

```shellscript GPG init & assign theme={null}
# Generate key
gpg --full-generate-key

# List keys, focus this line
#sec   .../[KEY_ID] yyyy-MM-dd [expires: yyyy-MM-dd]
gpg --list-secret-keys --keyid-format=long

# Public key, copy all output -> Github/Need verified platform
gpg --armor --export KEY_ID

# (Optional) Set gpg for Git
where gpg
# C:\_work\tools\GnuPG\bin\gpg.exe
git config --global gpg.program "/c/_work/tools/GnuPG/bin/gpg.exe"

# Clear Git config (global)
git config --global --unset gpg.format

# Set Git config (global)
git config --global user.signingkey KEY_ID

# Turn on signing required on commit (or tag)
git config --global commit.gpgsign true
# (or tag)
#git config --global tag.gpgSign true
```
