Git 介紹 & 常用操作

Table of Contents

[TOC]

git 基本介紹

:::info Git 是一個分散式版本控制系统,最初由Linus Torvalds為了更好地管理Linux内核開發而創建。允許多人協作開發項目,追蹤文件的編輯紀錄,以及管理版本的變化。 :::

  1. 分散式版本控制系统: 每個使用Git的使用者都擁有完整的code base的副本。這也意旨即使伺服器發生故障,開發者仍然可以繼續工作。
  2. 版本控制: Git追蹤文件的編輯紀錄,允許開發者紀錄項目的不同版本。這使得可以隨時返回到先前的狀態,查看特定版本之間的差異。
  3. 分支(Branch): Git允許創建分支,即獨立的開發路徑。可以在不影響主線(Main)開發的情况下進行實驗性開發、修復bug等。分支可以合併回主線開發。
  4. 提交(Commit): 將文件的修改保存到Git的過程。每次提交都包含一個提交資訊,描述了當次修改的内容。
  5. 合併(Merge): 將一個分支的更改合併到另一個分支。當一個分支的開發完成後,可以將其合併回主線開發。
  6. Others: Clone、Pull、Push、Tag ...

image

Install

Git download: https://git-scm.com/downloads/

git bash

Config

● Initialize data

 git init

● Configure your personal information

 git config --global user.name "Your Name"
 git config --global user.name "your.email@example.com"

● Check the configured user information

git config --list

image

Git editing process

image

:::info Situation: 這裡有一份空白的國小數學考券 "paper.txt" (一) 填空題 (55%)

  1. 1 + 1 = __ (5 pts)
  2. 5 x 7 = __ (10 pts)
  3. 9 + 6 = __ (10 pts)
  4. 8 - 3 = __ (10 pts)
  5. 12 ÷ 4 = __ (10 pts)
  6. 10 x 10 = __ (10 pts)

(二) 簡答題 (45%)

  1. 你有10個蘋果,吃掉了2個,媽媽又給你6個,你還剩下幾個?__ (15 pts)
  2. 如果跑步比賽,你跑贏了第2名,請問你是第幾名?__ (15 pts)
  3. 10人搭公車7人下車,剩幾人?__ (15 pts) :::

Completed oneself

1. Create paper.txt

vi paper.txt

paper.txt:

(一) 填空題 (55%)
1. 1 + 1 = __ (5 pts)
2. 5 x 7 = __ (10 pts)
3. 9 + 6 = __ (10 pts)
4. 8 - 3 = __ (10 pts)
5. 12 ÷ 4 = __ (10 pts)
6. 10 x 10 = __ (10 pts)

(二) 簡答題 (45%)
1. 你有10個蘋果,吃掉了2個,媽媽又給你6個,你還剩下幾個?__ (15 pts)
2. 如果跑步比賽,你跑贏了第2名,請問你是第幾名?__ (15 pts)
3. 10人搭公車7人下車,剩幾人?__ (15 pts)
<esc>:wq! #store and quit

1.1 Changes request

git add .  //add changes in the current directory to the staging area
git status //overview of what changes you've made

image

git commit -m "add paper.txt" 

Create a new commit in the Git repository The commit message: "add paper.txt"

image

● Return to confirm the status

git status

image

It's empty. Cause the changes be committed

● Check git log

git log

image

image

2. Complete the first question

vim paper.txt

paper.txt:

(一) 填空題 (55%)
1. 1 + 1 = _2_ (5 pts)
2. 5 x 7 = __ (10 pts)
<esc>:wq! #store and quit

2.1 Changes request

git add paper.txt  //add changes in the paper.txt to the staging area
git status

image

git commit -m "Complete problem 1"

image

git log

image

Branch

:::info Situation: 已建完考券 且 完成第一題。 接著交給: ◆ Alice完成剩下的第一大題: (一) 填空題 (55%)

  1. 1 + 1 = 2 (5 pts)
  2. 5 x 7 = __ (10 pts)
  3. 9 + 6 = __ (10 pts)
  4. 8 - 3 = __ (10 pts)
  5. 12 ÷ 4 = __ (10 pts)
  6. 10 x 10 = __ (10 pts)

◆ Bob完成第二大題: (二) 簡答題 (45%)

  1. 你有10個蘋果,吃掉了2個,媽媽又給你6個,你還剩下幾個?__ (15 pts)
  2. 如果跑步比賽,你跑贏了第2名,請問你是第幾名?__ (15 pts)
  3. 10人搭公車7人下車,剩幾人?__ (15 pts) ::: image

1. Create & Switch Branch

● Create & Switch to alice

git checkout -b alice

image

● Create & Switch to bob

 git checkout master //back to master branch

back to master and add another branch image

git checkout bob

image

git status

image

2. Branch alice

2.1 Switch to alice

git checkout alice

image

2.2 alice editing

 vi paper.txt
(一) 填空題 (55%)
1. 1 + 1 = _2_ (5 pts)
2. 5 x 7 = _35_ (10 pts) 
3. 9 + 6 = _15_ (10 pts)
4. 8 - 3 = _5_ (10 pts)
5. 12 ÷ 4 = _3_ (10 pts)
6. 10 x 10 = _100_ (10 pts)

(二) 簡答題 (45%)
1. 你有10個蘋果,吃掉了2個,媽媽又給你6個,你還剩下幾個?_ (15 pts)
2. 如果跑步比賽,你跑贏了第2名,請問你是第幾名?_ (15 pts)
3. 10人搭公車7人下車,剩幾人?_ (15 pts)
<esc>:wq! #store and quit

2.3 Changes request

git add paper.txt
git status

image

git commit -m "Complete alice done"

image

3. Branch bob

3.1 Switch to bob

git checkout bob

image

:::danger Unable to see the commit history of other branches :::

● check paper.txt file & git log

cat paper.txt

image

It's a original file.

git log

image

No log with "Complete alice done" branch Master 的git log 也不會有 image

3.2 Bob editing

vi paper.txt
(一) 填空題 (55%)
1. 1 + 1 = __ (5 pts)
2. 5 x 7 = __ (10 pts) 
3. 9 + 6 = __ (10 pts)
4. 8 - 3 = __ (10 pts)
5. 12 ÷ 4 = __ (10 pts)
6. 10 x 10 = __ (10 pts)

(二) 簡答題 (45%)
1. 你有10個蘋果,吃掉了2個,媽媽又給你6個,你還剩下幾個?_14_ (15 pts)
2. 如果跑步比賽,你跑贏了第2名,請問你是第幾名?_2_ (15 pts)
3. 10人搭公車7人下車,剩幾人?_4_ (15 pts)
<esc>:wq! #store and quit

2.3 Changes request

 git add paper.txt
 git status

image

 git commit -m "Complete bob done"

image

 git log

image

There is "Complete bob done",but no alice. Because of different branches.

Merge

:::info Situation: 依照上述步驟: ◆ Alice 先完成第一大題 ◆ Bob 後完成第二大題

新情境: 雖然Alice先完成,但Bob先交卷 :::

● Return to Master and Merge

1. Merge Bob

git checkout master
git merge bob

image

git log

image

2. Merge Alice

git merge alice

image

 git log

image :::success "Successfully completed the branch task." :::

Connecting to GitHub: SSH

SSH key

1. Generate SSH key pairs for secure communication

ssh-keygen

image

Enter passphrase (empty for no passphrase): optional but recommended for additional security

2. Default location to save the key pair

cat ~/.ssh/id_rsa.pub

ssh-rsa: A type of cryptographic algorithm used in SSH (Secure Shell) for authentication and encryption.

Github: Settings >> SSH and GPG keys

https://github.com/settings/keys :::info GitHub supports two types of keys for secure communication and authentication: SSH keys and GPG (GNU Privacy Guard) keys. :::

image

Paste the id_rsa.pub

image

Verify

ssh -T git@github.com

image :::success Finally,Push the committed changes to the remote GitHub repository. ::: S__504643588

fork

git clone https://github.com/ntut-xuan/ConflictCTF.git

image

 git merge origin/alice

image

python3 script.py

image

Error

vscode debug script.py

成功

python3 script.py

image