changed 2 years ago
Published Linked with GitHub

Git 基本操作

資訊之芽 2022 Python 語法班
Author: Sean 韋詠祥

Note:
日期:2022-05-08


課程錄影


安裝 Git 工具


macOS 安裝方式

先開啟 iTerm2 或 Terminal 終端機

輸入 git 指令,內建的 Xcode 將會跳出安裝資訊

git --version

Windows 安裝方式

搜尋 Git for Windows


依指示安裝

基本上都下一步,編輯器這邊可以改成自己慣用的


Linux 安裝方式

相信你知道怎麼做的 :D

# Debian / Ubuntu
apt install git
# Fedora
dnf install git
# Arch Linux
pacman -S git
# Alpine
apk add git

註冊 GitHub 帳號

網站:https://github.com/

填寫帳號資料

驗證碼:選出螺旋星系


收驗證信


個人分析資料

可點擊下方 Skip 跳過


基本 Bash 指令

  • 切換目錄
  • 顯示檔案內容
  • 查看目前狀態
  • 以下有 3 大部分,請跟著實作一次

先大概理解就好,也可以上網查「Bash XXX 用法」


Part 1

ls      # 列出目前目錄下的檔案
ls -a   # 包含隱藏檔案
ls -l   # 顯示詳細資訊
ls -al  # 我全都要!

touch myfile  # 建立空檔案
mkdir mycode  # 建立資料夾

mv myfile file1  # 移動檔案
cp file1 file2   # 複製檔案

Part 2

pwd        # 顯示當前目錄
cd mycode  # 進入資料夾
cd ..      # 回到前一層目錄
cd         # 不加參數,代表回到家目錄

echo Hey
echo "Hello World"

Part 3

cat /etc/passwd   # 印出檔案內容
less /etc/passwd  # 慢慢查看檔案內容

rm file1      # 刪除檔案
rmdir mycode  # 刪除空資料夾

開始上課!

資訊之芽 2022 Python 語法班
Author: Sean 韋詠祥


你有沒有看過


什麼是版本控制?


傳統中央控管型


分散式版本控制系統


核心使用概念


開始使用 Git


環境設定

git config --global user.name "Sean"  # 你的暱稱
git config --global user.email me@sean.taipei
git config --global color.ui true
git config --global init.defaultBranch master
git config --global alias.lg "log --color --graph --all --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --"

環境設定(續)

git config --global core.editor vim
git config --global merge.tool vimdiff
git config --global core.excludesFile = ~/.gitignore
git config --global gc.auto 0
git config --global pull.ff only
git config --global rerere.enable true
git config --global rebase.autosquash true
git config --global checkout.defaultRemote origin
git config --global url."git@github.com:".insteadOf "https://github.com/"

建立第一個本地 Repo

Git Repository

# 建立資料夾並初始化 git
mkdir py-project
cd py-project
git init
# 建立 main.py 檔案
vim main.py
git add main.py
git commit
# 在編輯器中輸入 commit 訊息
# 完成!

稍微改點東西

先用編輯器打開 main.py 檔案

讓他輸出 0 到 4 這五個數字,每次換行

完成後用 git 保存版本紀錄

git status
git add main.py
git commit

需求變更

輸入:一個正整數 N

輸出:從 0 到 N-1 的數字,每次換行

git diff
git add -p main.py
git commit

再次修改

輸入:一個正整數 N

輸出:從 1 到 N 的數字,每次換行

git add -p .
git commit -m '###'
git show
git show HEAD~1

建立 GitHub Repo


設定遠端 Repo 網址

git remote add origin https://github.com/#####/######.git
git remote -v
git push -u origin master

加入說明檔案

touch README.md  # 去編輯 README.md 檔案
git add README.md
git commit
git push

LICENSE 授權條款

git fetch --all
git pull
git log

完成!

讓我們進到 Git 進階用法


Git-flow


Git Branch

小練習:
輸入前詢問「Input N: 」
輸出 0 到 N-1

git switch
git switch -c feat-prompt-input
git reset
git status
git restore
git clean -x -i -d

Git Stash

git stash
git stash pop

Git Rebase

git rebase -i master

Thanks

投影片連結:https://hackmd.io/@Sean64/git-sprout2022


CC-BY

這份投影片以 創用 CC - 姓名標示 授權公眾使用,原始碼及講稿請見 此連結
Select a repo