git


ECHO $(WHOAMI)


№Ball

太空三


Todays slide


Before that

git --version

What is git?

Advanced git


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?

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

mkdir gitPractice # Make an new directory
cd gitPractice # Go to the directory

Working Directory

Creating one

git init

Result

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

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

OFTEN USED COMMAND

git status
Results
  1. What branch are you on
  2. Untracked (Unstaged) files
  3. Staged files

Staging Area

OFTEN USED COMMAND

git add .
  • . stands for everything
  • you are able to only add certain files
git add NCU

Staging Area

OFTEN USED COMMAND

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

<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

<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?

OFTEN USED COMMAND

git commit

RESULT

[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

OFTEN USED COMMAND

git status

RESULT

On branch master
nothing to commit, working tree clean

What if we have a typo?

Go ahead and do something else to your file

echo "Nobody Cares U" > NCU  # This will overwrite `NCU` with content "Nobody Cares U"

Let's check status again

OFTEN USED COMMAND

git status

RESULT

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

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!

git log

RESULT

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?

git log --oneline

More Usage

RESULT

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

You will never ever put an unworkable program on master

Let's add pretend we are another user.

git config user.name "jason"
git config user.email "jason@g.ncu.edu.tw"

SAVIOR - branches


SAVIOR - branches

OFTEN USED COMMAND

git branch jason
git branch dev
  • jason -> your branch name
  • dev -> short of develope

ALWAYS REMEMBER TO CHECK WHICH BRANCH YOU ARE ON

OFTEN USED COMMAND

git status

SAVIOR - branches

List all the branches you have (LOCAL)

OFTEN USED COMMAND

git branch

RESULT

  dev
  jason
* master

You can see that we have three branches.


SAVIOR - branches

Switch through branches

OFTEN USED COMMAND

git checkout jason
  • jason -> the target branch you want to switch to

Result

Switched to branch 'jason'

Remember to check if it is successed by status


Pretending someone

Make more changes!

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

OFTEN USED COMMAND

git status

Have U done it right?


Quick review

How to check your git movements?


REFLOG!

git reflog

RESULT

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
git config user.name
git config user.email

If you are correct

git log --graph --all
  • graph -> The graph view
  • all -> not only the current branch

What are these

network


Time to add (merge) them together!


What is merge?

Making two branches together.


How to merge?

you have to be on master branch!

OFTEN USED COMMAND

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

git log --graph --all

Wow Looks real cool

git merge dev

Let's merge dev and see what happens!


With some prettfier

*   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

OFTEN USED COMMAND

git status

RESULT

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

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

*   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

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?

git blame AUTHOR
  • AUTHOR -> the target file to ask

Expected result

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

OFTEN USED COMMAND

git checkout .
  • same as add, . for everything or you can type the filename you want to restore

QUICK TEST

echo "OUO" > test
cat test # prints out the content inside test
git checkout test
cat test

I don't like

Staging Area

git restore --staged .
  • same as add, . for everything or you can type the filename you want to restore

QUICK TEST

echo "OUO" > test
git add .
git status
git restore --staged test
git status

I don't like

REPO

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

echo "wrong commit" > test
git add .
git commit -m "OOPS: wrong commit"
git log
cat test
echo "right commit" > test
git reset --soft xxxxx
  • xxxxx is the commit id
git log
cat test
git status

QUICK TEST HARD

echo "original commit" > test
git add .
git commit -m "OOPS: wrong commit"
git log && cat test
echo "new commit" > test
git reset --hard xxxxx
  • xxxxx is the commit id
git log && cat test
git status

I don't like

COMMIT

git commit --amend

QUICK TEST

echo "test" > test
git add .
git commit -m "ADD: discription of T3ST"
git log
git commit --amend -m "ADD: description for TEST"
git log

REMOTE


LET'S REMOTE


REGISTRATION

register a gitlab account!


GET REMOTE REPO

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


Lets create a repository


Go to Create a new repository


fill out the forms


You made it!

Select a repo