owned this note
owned this note
Published
Linked with GitHub
---
tags: tutorial, ss, ncu
author: N0-Ball
title: Git
slideOptions:
theme: solarized
transition: 'fade'
GA: UA-208228992-1
---
# git
---
# ECHO $(WHOAMI)
----
## №Ball
### 太空三
----
## Todays slide

----
## Before that
```sh
git --version
```
---
## What is git?
[Advanced git](https://github.com/doggy8088/Learn-Git-in-30-days)
----
### Let's assume that you have a program.
###### Called FinalProject
----
## How do you often store your data?
1. FinalProject_V1
2. FinalProject_V2
3. FinalProject_V3
4. FinalProject_V100000000
5. FinalProject_FINAL
----
## How do you often store your data?
1. FinalProject_V1
2. FinalProject_V2
3. FinalProject_V3
4. FinalProject_V100000000
5. FinalProject_FINAL
6. FinalProject_FINAL2
7. FinalProject_FINAL3
8. FinalProject_FINAL10000
----
### What if you are trying to cooperate with classmates?
1. FinalProject_V1_EVIL
2. FinalProject_V1_STAR
3. FinalProject_V1_TSAI
4. FinalProject_V1_MERGE
5. FinalProject_V1_MERGE2
6. FinalProject_V1_MERGE3
----
### Moreover
#### Do you even know the different between V4 and V1?
----
### Moreover
#### What if someone changed and somehow the program just doesn't work where CRTL+Z doesn't help you?
----
## What is git?
:::info
A version controller
:::
---
## How to git?
----
### As a master said before
> you can make great products from an application by only using 20% of its functions.
*You only need to know the basics*
----
### Basic `STAGES` of git

----
### Basic `STAGES` of git
- Working Directory -> Where you write your code
- Staging Area -> **Magic**
- Local Repo -> git on your computer
- Remote Repo -> git not on your computer
----
### What is a Repo?
`Repository` which you can see as a project file (often a directory)
----
## Review
What is the stage that you write your codes?
----
### Working Directory
```bash
mkdir gitPractice # Make an new directory
cd gitPractice # Go to the directory
```
----
### Working Directory
#### Creating one
```bash
git init
```
#### Result
```shell
Initialized empty Git repository in $(pwd)/.git #you can do "echo $(pwd)" to verify if the command is correct
```
----
### Go ahead an play make changes
```bash
echo "Never Care U" > NCU # This will create a file called `NCU` with content "Never Care U"
```
----
## Quick Review
After finishing a period, what stage will you want to enter
----
### Staging Area
:::success
OFTEN USED COMMAND
:::
```bash
git status
```
##### Results
1. What `branch` are you on
2. Untracked (Unstaged) files
3. Staged files
----
### Staging Area
:::success
OFTEN USED COMMAND
:::
```bash
git add .
```
- `.` stands for everything
- you are able to only add certain files
```bash
git add NCU
```
----
### Staging Area
:::success
OFTEN USED COMMAND
:::
```bash
git status
```
#### Any differents?
----
## Quick Review
After staging a file, what will you want to do?
----
### Local Repos
Which is `commit`.
This means to put all the thing from stage into your repo, and telling what have you done or why do you do this.
----
#### Before that
We need a **GOOD** convention of commit so that we know what you are writing
----
### A COMMIT SHOULD
```shell
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]
```
- `type` -> need to discuss before using, however it often contians and can be more of them
- `FIX`
- `ADD`
- `FEAT`
- `DOCS`
- `TEST`
----
### A COMMIT SHOULD
```shell
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]
```
- `scope` -> sometimes we don't need them, but when we are in a big project, it will give a better view of what the error is
- `description` -> the reason you make this commit
- `body` -> further descriptions if needed
- `footer` -> References
----
### A COMMIT SHOULD NOT

#### BE NONSENSE
Trust me, you will find out that this is hard.
----
### Let's try shell we?
:::success
OFTEN USED COMMAND
:::
```bash
git commit
```
#### RESULT
```shell
[master (root-commit) 6d13e9f] ADD: add NCU for demostrating how to commit.
1 file changed, 1 insertion(+)
create mode 100644 NCU
```
----
### Let's see if it works
:::success
OFTEN USED COMMAND
:::
```bash
git status
```
#### RESULT
```shell
On branch master
nothing to commit, working tree clean
```
----
### What if we have a typo?
Go ahead and do something else to your file
```bash
echo "Nobody Cares U" > NCU # This will overwrite `NCU` with content "Nobody Cares U"
```
----
### Let's check status again
:::success
OFTEN USED COMMAND
:::
```bash
git status
```
#### RESULT
```shell
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: NCU
no changes added to commit (use "git add" and/or "git commit -a")
```
----
### We just don't need that much descriptions
```bash
git commit -m "FIX: typo of NCU"
```
---
## Now we know how to create files to repo
How to see the changes and what happens is the most important thing.
----
### LOGS
- `git log` -> repo's history
- `git reflog` -> **whole** git history
----
## Give it a try!
```bash
git log
```
#### RESULT
```shell
commit 9b605fe216228416f4b999f34c17d05f33f33ef1
Author: N0Ball <cwj0325@g.ncu.edu.tw>
Date: Tue May 17 20:32:10 2022 +0000
FIX: typo of NCU
commit 6d13e9f8e8deab9f1724a63e6ce1845548207a75
Author: N0Ball <cwj0325@g.ncu.edu.tw>
Date: Tue May 17 20:23:41 2022 +0000
ADD: add NCU for demostrating how to commit.
It's simply just a nonsense file with nonsense words; if it is for
teaching it can be anything else. I suggest it to be a meme.
```
----
## Too long?
```bash
git log --oneline
```
[More Usage](https://devhints.io/git-log)
#### RESULT
```shell
9b605fe FIX: typo of NCU
6d13e9f ADD: add NCU for demostrating how to commit.
```
---
## What if
- You don't want the service down, but need the add modifications?
- Or you have to cooperate with someone.
----
## What if
:::danger
You will never ever put an unworkable program on master
:::
#### Let's add pretend we are another user.
```bash
git config user.name "jason"
git config user.email "jason@g.ncu.edu.tw"
```
----
## SAVIOR - branches

----
## SAVIOR - branches
:::success
OFTEN USED COMMAND
:::
```bash
git branch jason
git branch dev
```
- `jason` -> your branch name
- `dev` -> short of develope
:::danger
ALWAYS REMEMBER TO CHECK WHICH BRANCH YOU ARE ON
:::
:::success
OFTEN USED COMMAND
:::
```bash
git status
```
----
## SAVIOR - branches
List all the branches you have (**LOCAL**)
:::success
OFTEN USED COMMAND
:::
```bash
git branch
```
#### RESULT
```shell
dev
jason
* master
```
You can see that we have three branches.
----
## SAVIOR - branches
Switch through branches
:::success
OFTEN USED COMMAND
:::
```bash
git checkout jason
```
- `jason` -> the target branch you want to switch to
#### Result
```shell
Switched to branch 'jason'
```
Remember to check if it is successed by `status`
----
## Pretending someone
Make more changes!
```bash
echo "Nation Central University\!\!\!\!" >> NCU && echo "BAD" > AUTHOR # This appends "Nation Central University!!!!" to the file NCU and creates a file `AUTHOR` with content "BAD"
```
Append words to NCU is important for future usage.
#### Don't forget to commit it!
----
## Pretending someone
:::success
OFTEN USED COMMAND
:::
```bash
git status
```
Have U done it right?
----
## Quick review
How to check your git movements?
----
## REFLOG!
```bash
git reflog
```
#### RESULT
```shell
b50471b HEAD@{0}: commit: ADD: add comments to author
9b605fe HEAD@{1}: checkout: moving from master to jason
9b605fe HEAD@{2}: commit: FIX: typo of NCU
6d13e9f HEAD@{3}: commit (initial): ADD: add NCU for demostrating how to commit.
```
----
### Practice
1. change back to your name and email
2. make changes in your `dev` branch.
3. make changes in your `master` branch.
4. change to another person again.
5. make changes in `jason` branch.
6. Repeat `1.` once again.
#### NOTE
1. **DO NOT** change anything from old files.
2. you can check your config by
```bash
git config user.name
git config user.email
```
----
## If you are correct
```bash
git log --graph --all
```
- `graph` -> The graph view
- `all` -> not only the current branch
#### What are these
:::info
network
:::
---
## Time to add (`merge`) them together!
----
## What is merge?
Making two branches together.

----
## How to merge?
you have to be on `master` branch!
:::success
OFTEN USED COMMAND
:::
```bash
git merge jason
```
- `jason` -> your targeted branch
#### Note
branch `jason` will not change, that's reasonable, since you can only modify what's on your current branch.
----
## If you are correct
```bash
git log --graph --all
```
#### Wow Looks real cool
```bash
git merge dev
```
Let's merge dev and see what happens!
----
### With some prettfier
```shell
* N0Ball -> Merge branch 'dev' (HEAD -> master)
|\
| * N0Ball -> FIX: typo of filename (dev)
| * N0Ball -> ADD: add readme file
* | N0Ball -> Merge branch 'jason'
|\ \
| * | jason -> FIX: modify author's comment to make it more readable (jason)
| * | jason -> ADD: add comments to author
| |/
* | N0Ball -> ADD: add something more to make the graph better
* | N0Ball -> ADD: add something to make the log graph looks cool
|/
* N0Ball -> FIX: typo of JASON
* N0Ball -> ADD: add JASON for demostrating how to commit.
```
#### Try it yourself!
----
## Merges are not always fluent
1. change to `dev` branch
2. modify `NCU`
- remember that `jason` had append words to `NCU`
- since it was merged to `master` -> `NCU` had appended words
- both `dev` and `master` have its own appended words -> git doesn't know which to use -> **CONFLICT**
----
## MERGE CONFLICTS
:::success
OFTEN USED COMMAND
:::
```bash
git status
```
#### RESULT
```shell
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: JASON
no changes added to commit (use "git add" and/or "git commit -a")
```
----
## MERGE CONFLICTS
```shell=
FOREST SMELLS GOOD
<<<<<<< HEAD
National Central University!!!!
=======
Very long president duration
>>>>>>> dev
```
- `<<<<<< HEAD` -> current status
- `>>>>>> dev` -> incomming changes
You should choose. Keep both, leave one, delete both... whatever you like!
#### I chose to keep dev
----
## MERGE CONFLICTS
1. fix conflicts
2. stage changes
3. commit changes
4. **FINISH MERGE!**
----
## If everything works fine
```bash
* N0Ball -> Merge branch 'dev' ( (HEAD -> master))
|\
| * N0Ball -> ADD: add some words to JASON to make merge conflicts ( (dev))
* | N0Ball -> Merge branch 'dev' ()
|\|
| * N0Ball -> FIX: typo of filename ()
| * N0Ball -> ADD: add readme file ()
* | N0Ball -> Merge branch 'jason' ()
|\ \
| * | jason -> FIX: modify author's comment to make it more readable ( (jason))
| * | jason -> ADD: add comments to author ()
| |/
* | N0Ball -> ADD: add something more to make the graph better ()
* | N0Ball -> ADD: add something to make the log graph looks cool ()
|/
* N0Ball -> FIX: typo of JASON ()
* N0Ball -> ADD: add JASON for demostrating how to commit. ()
```
---
# GIT FLOW
----
## How to git

----
## IMPORTANT
:::danger
You will never ever put an unworkable program on master.
:::
- you should not modify anything on master.
- you should be very aware of merging to master.
----
## WHO?
```bash
git blame AUTHOR
```
- `AUTHOR` -> the target file to ask
#### Expected result
```shell
3add7ab5 (jason 2022-05-17 21:47:52 +0000 1) YOU ARE BAD
```
- `-L a, b` can be used for lines
---
## OOPS
----
## I don't like this
----
## Quick Reviews
`STAGES` of git
----
## Quick Review
- Working Directory
- Staging Area
- Repo
----
## How to get to the wanted version?
----
## I don't like
Working Directory
:::success
OFTEN USED COMMAND
:::
```bash
git checkout .
```
- same as `add`, `.` for everything or you can type the filename you want to restore
----
## QUICK TEST
```bash
echo "OUO" > test
```
```bash
cat test # prints out the content inside test
```
```bash
git checkout test
```
```bash
cat test
```
----
## I don't like
Staging Area
```bash
git restore --staged .
```
- same as `add`, `.` for everything or you can type the filename you want to restore
----
## QUICK TEST
```bash
echo "OUO" > test
git add .
```
```bash
git status
```
```bash
git restore --staged test
```
```bash
git status
```
----
## I don't like
REPO
```bash
git reset
```
#### OPTIONS
1. `--soft` -> only changing status
2. `--hard` -> changes everything back to the commit status
- Just like `git reset --soft` + `git checkout .`
----
## QUICK TEST SOFT
```bash
echo "wrong commit" > test
git add .
git commit -m "OOPS: wrong commit"
```
```bash
git log
cat test
```
```bash
echo "right commit" > test
git reset --soft xxxxx
```
- xxxxx is the commit id
```bash
git log
cat test
git status
```
----
## QUICK TEST HARD
```bash
echo "original commit" > test
git add .
git commit -m "OOPS: wrong commit"
```
```bash
git log && cat test
```
```bash
echo "new commit" > test
git reset --hard xxxxx
```
- xxxxx is the commit id
```bash
git log && cat test
git status
```
----
## I don't like
COMMIT
```bash
git commit --amend
```
----
## QUICK TEST
```bash
echo "test" > test
git add .
git commit -m "ADD: discription of T3ST"
```
```bash
git log
```
```bash
git commit --amend -m "ADD: description for TEST"
```
```bash
git log
```
---
# REMOTE
----
## LET'S REMOTE
----
## REGISTRATION
[register a gitlab account!](https://gitlab.dos.phy.ncu.edu.tw/users/sign_up)
----
## GET REMOTE REPO
:::success
OFTEN USED COMMAND
:::
`git clone <repo url>`
---
# GITHUB
---
## WHAT IS
> At a high level, GitHub is a website and cloud-based service that helps developers store and manage their code, as well as track and control changes to their code. [name=https://kinsta.com/application-hosting/]
----
Clone methods
- HTTPS: ask for username and password
- SSH: use ssh key, which doesn't require username and password
----
[Create a github](https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F&source=header-home)

---
## Lets create a repository
----
Go to Create a new repository

----
fill out the forms

----
You made it!
