---
# System prepended metadata

title: Git Tutorial Overview
tags: [tutorials]

---

# Git Tutorial Overview

###### tags: `tutorials`

> For those who only know `git add` `git commit` `git push`

## Git config setup



## Basic concept

### `git log` usage
```bash=
$ git log --author="acebenson0704"
$ git log --grep="add" # commit message contains "add"
$ git log -S "content" # commit file content conains "content"
$ git log --since="9am" --until="12am"
$ git log --oneline 
$ git log --graph
$ git log --all
```

### Rename, delete, edit files
#### `git mv <file>`
```bash=
$ mv <file>
$ git add <file>
```
#### `git rm <file>`
```bash=
$ rm <file>
$ git add <file>
```
#### `git rm --cached <file>`

Only remove `<file>` from git, the file still retains in folder
```bash=
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    deleted:  <file>

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    <file>
```

#### `git restore <file>`
Discard changes in working directory, old version use `checkout`
```bash=
$ git checkout <file>
$ git checkout -- <file>
```

:::info
why checkout work?
```bash=
git checkout <commit> <file>
```
update current `<file>` with `<commit>` version (checkout that version to here)
:::

#### `git restore --staged <file>`
Unstage (staging area -> working directory), or using `reset`
```bash=
$ git reset HEAD -- <file>
```
:::info
why reset work?
* --mixed(default): retain in working directory
* --soft: retain in staging area
* --hard: discard

`reset` is more like **go to**
:::

:::success
The difference between `git checkout` and `git reset`
> `checkout` only change HEAD 
> `reset` change HEAD and branch
:::
        
## Commit
### Edit last commit
revise message
```bash=
$ git commit --amend -m "New Message"
```
add file to last commit
```bash=
$ git add <file>
$ git commit --amend --no-edit # no edit message
```
> Create a new commit object (new SHA value)


## Files in `.git` folder
### Objects: Blob, Tree, Commit Tag

> example folder structure
```
/
│   README.md
│   index.html    
└───public
    │   image1.png
    └───image2.png
```

```graphviz
digraph gitfiles {
    Blob1 [label="Blob1|image1.png" ,fillcolor=brown1, fontcolor=White, fontsize=24, shape=Mrecord, style=filled]
    Blob2 [label="Blob2|index.css" ,fillcolor=brown1, fontcolor=White, fontsize=24, shape=Mrecord, style=filled]
    Blob3 [label="Blob3|index.js" ,fillcolor=brown1, fontcolor=White, fontsize=24, shape=Mrecord, style=filled]
    Tree1 [label="Tree1|public/" ,fillcolor=darkgoldenrod1, fontcolor=White, fontsize=24, shape=Mrecord, style=filled]
    Tree2 [label="Tree2|/" ,fillcolor=darkgoldenrod1, fontcolor=White, fontsize=24, shape=Mrecord, style=filled]
    Tree3 [label="Tree3|/" ,fillcolor=darkgoldenrod1, fontcolor=White, fontsize=24, shape=Mrecord, style=filled]
    Commit1 [label="Commit1" ,fillcolor=darkgreen, fontcolor=White, fontsize=24, shape=Mrecord, style=filled]
    Commit2 [label="Commit2" ,fillcolor=darkgreen, fontcolor=White, fontsize=24, shape=Mrecord, style=filled]
    
    Commit2->{Tree3}

    Commit1->Tree2
    Tree1->Blob1
    Tree1->Blob2
    Tree2->Tree1
    Tree2->Blob3
    
    {rank=same; Commit2 Commit1}
    {rank=same; Blob1 Blob2 Blob3}
}
```
