{Git/Github}

2022/11/30

git init
git status 
git add [filename] # Add file to staging area
git commit -m "Commit message"
# PRESENTING CODE

Git 基础

  • 为什么要使用 Git:版本管理 多人协作
  • Git 初始化:git init 做了什么
  • Git 添加文件:Staging area 的概念
  • Git 提交变化:git commit
  • Git 和 Github:并不是同一个东西
git show
git log
git diff
.gitignore
# PRESENTING CODE

Git 基础

  • 查看提交的历史
  • 查看差分
  • 不要跟踪特定文件:.gitignore
# PRESENTING CODE

Git 基础 - config

[core]
  preloadindex = true
[user]
  name = your_name
  email = your_email@domain.com
[alias]
	co = checkout
	br = branch
	ci = commit
	st = status
	graph = log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
	fullgraph = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n''          %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all
	up = rebase
[gitreview]
	remote = origin
  • 注意用户名和邮箱,这会记入你的commit:不同机器上的配置要统一起来

  • 添加常用缩略词以减少打字负担

  • 高阶命令合成:log graph 等功能

  • 简单的 config 可以用 git config 命令行完成

git branch
git switch <branch>
git merge <branch>
# PRESENTING CODE

Git 基础

  • 分支的概念:为什么需要分支?

  • main 分支的概念

  • 切换分支

  • 合并分支

git clone [url]
git remote
git push <remote> <branch>
# PRESENTING CODE

Git 基础

  • 远程 repo 的概念

  • 克隆一个远程 repo

  • origin 的概念

  • 从远程 repo 抓取

  • 推送修改

主仓

Ailing 的 Fork

Garbiel 的 Fork

本地

我的 Fork

Remote

push

fetch

fetch

fetch

# PRESENTING CODE

Git 基础 - fetch vs. pull

本地

Github

fetch:仅下载远程 repo 

pull:下载远程 repo 并合并 

git fetch # fetch all branches
git fetch <remote> <branch> # fetch one branch
git pull
# PRESENTING CODE

Git 基础 - fetch vs. pull

git pull 的问题:merge 的行为是比较复杂的

本地

Github

这里 merge 会尝试合并分支并创建一个新的提交

Bottom line:fetch + rebase 是一个比 pull 更保险的操作

# PRESENTING CODE

Git 基础 - fetch vs. pull

git pull 的新 warning (from 2.31)如果本地和 remote 发生了 divergence

# PRESENTING CODE

Git 基础 - fetch vs. pull

# PRESENTING CODE

Git 基础 - rebase vs. merge

main
my_feature
main
my_feature
main

初始状态

Merge

Rebase

my_feature
# PRESENTING CODE

Git 基础 - rebase vs. merge

main
my_feature

 

  • merge 的瑕疵在于会加入很多 merge commit,这些 commit 的意义何在?
    • 如果只是想保持自己的 branch 和主仓一致,rebase 使工作历史更加干净
    • merge 的好处是完全没有触碰 merge 前所有的历史
    • rebase 多多少少修改了历史,不符合实际发生的事情
    • rebase 需要解决过去历史中的 conflict 才可成功进行
  • 黄金准则:不要 rebase 一个和别人共同工作的 branch
git init
git add 
git commit // staging area,commit
git remote // remote 和 local 的概念,可以有两个 remote
git branch // 演示一下简单的分叉图,不要污染 master 的概念
git checkout
git push // 讲一下 local 和 remote
git log
git diff
git show
.gitignore

放到 intermediate
git pull  // 其实是一个比较复杂的 command;可以不放 basics; 触发 rebase 触发 merge 的情况 例子
# PRESENTING CODE

Git Basics 2

Learn the basics of Git :

https://github.com/twtrubiks/Git-Tutorials

https://git-scm.com/book/en/v2

other recommended resources?

git fetch
git merge 
git pull = git fetch + merge
git rebase 
[core]
  preloadindex = true
[user]
  name = ailzhang
  email = xxx@xxx.com
[alias]
	co = checkout
	br = branch
	ci = commit
	st = status
[push]
	default = simple
[credential]
	helper = osxkeychain
[alias]
	graph = log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
	fullgraph = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n''          %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all
	up = rebase
[gitreview]
	remote = origin
# PRESENTING CODE

Github config

# PRESENTING CODE

一起来试试吧!

# PRESENTING CODE

Steps

  1. 从远端拉取taichi 代码,设置自己的fork
  2. 本地开发,开PR
  3. Code review
  4. Continuous Integration
  5. 合并!
# PRESENTING CODE

Github开发建议

  1. 不要在master 分支直接进行操作,先开个branch!
  2. 一个PR尽量只包含一个commit,清楚的写明干了啥(一件事
  3. 提交PR的人应该优先考虑如何让review变得简单且快速
    1. 为什么要提这个PR, 这个PR干了什么,怎么干的,还剩什么没干
# PRESENTING CODE
  • ​Git 并不聪明,所以让它做的事情越简单越好

Github开发建议

  • 所以我会经常fetch & rebase, 让我的commit history 长下面这样:
1.  本地的master, fork的master,origin的master是啥关系?
2.  在开发branch1的过程中需要切换到branch2
3.  别人交了一个PR, 我想拉到本地测试
4.  这个commit很有用,我想拉到我的branch上来
5.  这个commit不太对我想把它踢掉看看
6.  刚commit的东西不太对,我想改改
7.  如何处理文件冲突
# PRESENTING CODE

Git 开发常见问题和解决办法

git bisect 
git reflog
git rebase -i 
# PRESENTING CODE

Git 进阶

Code

By ailzhang

Code

  • 161