Git and Github 筆記

Git fast version control

  • 一些基本概念

  • 版本管理和跟蹤

  • 版本控制系統是軟件工具

  • 可以幫助團隊管理源代碼

Status of Version 版本狀態

  • Untracked 未追蹤
  • Tracked 已追蹤
  • Staged 已暫存
  • Committed 已提交

HEAD -> master / branch

  • HEAD : 目前進度,最新狀態
  • Master : 主線

git 追蹤的是檔案的變化,不是檔案本身

  • 刪除檔案也需要"add" 和 “commit”

需要忽略的版本管理的文件

  • 在projec 目錄下 新增 .gitignore
  • .gitignore 文件中寫入不需要進行版本管理控制的文件後綴或者文件名

Github 是平台 / 相當於Server

  • Git 工具將文檔或者代碼 託管到github 平台 -

Install 安裝

Mac os

  • Mac OS 自帶 git 輸入 git -- version 就可以看到git 版本

Windows

Ubuntu /Debian

  • 根據下方命令進行安裝
$ sudo apt-get update
$ sudo apt-get install git
$ git --version

Use 使用

基礎配置

使用以下命令配置git 用戶名和電子郵件

$ git config --global user.name "Your name"
$ git config --global user.email "your_email@example.com"

基礎使用

# 加入 Git
$ git add app.py
$ git add init.py main.py
$ git status
$ git commit -m "初始化,建立 project"

# 查看提交記錄
$ git log
$ git log --oneline

# 比較及還原檔案
$ git diff fd0b510 -- app.py
$ git checkout fd0b510 -- app.py

# 重設版本;此操作不可逆,請先備份
$ git reset --hard 55f7a26

將代碼或文檔備份到github

$ git remote add origin https://github.com/dukehug/{repository}/{project_name.git}
$ git branch -M main
$ git push -u origin main
$ git push

多人協作

  • 在github 邀請協作者
  • 協作者clone project 到本地 (clone)
  • 主作者 pull 協作者的更新到本地 (pull)
  • 分支管理
$ git clone https://github.com/dukehug/{repository}/{project_name.git}
$ git checkout -b branch2
$ git add .
$ git commit -m "新增和更改文件"
$ git push origin branch2
  • 分支回出現2個
  • 協作者在他負責的分支下提交"Pull request"
  • 協作者請求將這些修改和變更合併到主分支
  • 主作者可以同意或者不同意,或者request change
  • 同意請求 , 將會執行"Merge pull request"
  • 合併完成之後 branch2 分支就會被deleted
  • 每次更新遠端的代碼或者檔案之後, 都需要執行 pull

Reference:

https://youtu.be/FKXRiAiQFiY?si=QP6wm_S9diztgIWm - 程式與網頁開發者必備技能!Git 和 GitHub 零基礎快速上手,輕鬆掌握版本控制的要訣! - papaya電腦教室

https://www.atlassian.com/zh/git/tutorials/atlassian-git-cheatsheet - Git cheat sheet

https://git-scm.com/docs - Complete list of all commands

https://education.github.com/git-cheat-sheet-education.pdf - Github GIT CHEAT SHEET - Github education