CSST 程式設計討論會 2023
Author: Sean 韋詠祥
基本上都下一步,編輯器這邊可以改成自己慣用的
相信你知道怎麼做的 :D
# Debian / Ubuntu
apt install git
# Fedora
dnf install git
# Arch Linux
pacman -S git
# Alpine
apk add git
先大概理解就好,也可以上網查「Bash XXX 用法」
ls # 列出目前目錄下的檔案
ls -a # 包含隱藏檔案
ls -l # 顯示詳細資訊
ls -al # 我全都要!
pwd # 顯示當前目錄名稱
cd Documents # 進入資料夾
cd .. # 回到前一層目錄
cd # 不加參數,代表回到家目錄
mkdir mycode # 建立資料夾
touch myfile # 產生空檔案
mv myfile file1 # 移動檔案
cp file1 file2 # 複製檔案
echo Hey
echo "Hello World" > file3
cat /etc/passwd # 印出檔案內容
less /etc/passwd # 慢慢查看檔案內容
rm file1 # 刪除檔案
rmdir mycode # 刪除空資料夾
git config --global user.name "Sean" # 你的暱稱
git config --global user.email me@sean.taipei
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 gc.auto 0 # Disable garbage collection
git config --global pull.ff only # Fast-forward only
git config --global core.excludesFile = ~/.gitignore
git config --global rerere.enable true # Reuse Recorded Resolution
git config --global rebase.autosquash true
git config --global checkout.defaultRemote origin
Git Repository
cd ~/Documents
mkdir my-project # 建立資料夾
cd my-project
git init # 初始化 git
touch main.py # 建立 main.py 或 main.cpp 檔案
git add main.py # 把檔案從 working area 加入 staging area
git commit
# 在編輯器中輸入 commit 訊息:init
# 完成!
先用編輯器打開 main.py
檔案
explorer . # Windows: 用檔案總管開啟資料夾
open . # macOS: 開啟資料夾
EDIT main.py # Linux: 直接編輯檔案
用 for
跟 range()
讓他輸出 0 到 4 這五個數字,每次換行
完成後用 git 保存版本紀錄
git status # 有誰被改到了
git add main.py
git commit
輸入:一個正整數 N
輸出:從 0 到 N-1 的數字,每次換行
git diff # 我們改了什麼
git add -p main.py
git commit
Hint:用 int(input())
接收輸入
輸入:一個正整數 N
輸出:從 1 到 N 的數字,每次換行
git add -p .
git commit -m '<commit message>'
試著查看歷史紀錄
git show # 預設為 HEAD
git show HEAD~1 # 往前 1 筆
別把我的黑歷史記下來 (/ω\)
當我們有個不必要的檔案
echo __pycache__/ >> .gitignore
echo config.py >> .gitignore
cat .gitignore
git add .gitignore
假如我們要從 git 歷史中
完整刪除 pkg/
底下所有 .deb
檔案
git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch pkg/*.deb' \
--prune-empty --tag-name-filter cat
參閱:凍仁的筆記(2014)
git remote add origin https://github.com/<User>/<Repo>.git
git remote -v
git push -u origin master
touch README.md # 去編輯 README.md 檔案
git add README.md
git commit
git push
git fetch --all
git pull
git log
讓我們進到 Git 進階用法
小練習:
輸入前詢問「Input N: 」
輸出 0 到 N-1
git switch -h # --help
git switch -c feat-prompt-input # --create
git push
git push -f # --force
git push --force-with-lease
git restore
git clean -x -i -d # no-ignore, interactive, directory
git stash
git stash pop
git rebase -i master