Git 入門
===
### Git / Git Server / Git flow
----
![](https://git-for-windows.github.io/img/git_logo.png)
https://hackmd.io/@calee/SJ_09kjf-
----
<img src="https://i.imgur.com/7px5IND.png" style="width:200px;height:200px;border-radius:50%"/>
### [李家安](https://www.facebook.com/calee0219)
##### 交大資工 大二
calee0219@cs.nctu.edu.tw
- 網站開發社
- 網路安全策進會
- [NCTU CS Course](https://hackmd.io/CYFgpgDArBEIwFpgDYBGwEgOyog1YqAnAgGZwDMIqATCAByr1RhA) hosting
---
## 軟體開發
- 版本控制
- 團隊開發
----
### 什麼是版本控制?
教授要刻一顆 CPU,有 Cache 功能加十分
於是,有了漫長的旅程...
----
![](https://denny.one/git-slide/img/code_smart_release-58.jpg)<!-- .slide: data-transition="fade-in" -->
----
![](https://denny.one/git-slide/img/code_smart_release-68.jpg)<!-- .slide: data-transition="fade-in" -->
---
## 攻城屍的救星
版本控制
----
## 安裝 Git
- `apt-get install git`
- [windows 下載](https://git-scm.com/download/win)
- [macOS 下載](https://git-scm.com/download/mac)
- brew install git
- [GitHub Desktop](https://desktop.github.com)
----
## 環境設定
```=
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
git config --global color.ui true
git config --global core.editor vim
git config --global alias.co commit
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 --"
```
----
## .gitconfig
- [config file](https://git-scm.com/docs/git-config)
- [ignore file](https://github.com/github/gitignore)
- [large storage](https://git-lfs.github.com)
- [我的設定](https://github.com/calee0219/.dotfiles/blob/master/gitconfig)
----
- man git
- git help <command>
- stackflow
----
## 原理
git 會儲存每次的修改
藉由 hash 維護修改紀錄
hash value 會往上疊加 (前一個 hash + 修改)
---
## 開新專案
git init
----
![](https://image.slidesharecdn.com/git-worfklow-drupal-core-dev-140302065209-phpapp01/95/a-git-workflow-for-drupal-core-development-14-638.jpg?cb=1393743243)
[reference](https://git-scm.com/about/staging-area)
----
## 新改變後...
- git status
- git diff
- git add
- git commit (-m)
- git log
----
### 何時 commit?
### 如何 commit?
commit --amend
---
## Branch
- git branch
- git checkout -b
![](https://i.imgur.com/5ZxuOIZ.png)
----
## Checkout
- git checkout {branch name}
- git checkout {commit hash}
----
## Merge
- git merge (--no-ff)
- git rebase
- git cherry-pick
![](https://i.imgur.com/qGF99ju.png)
----
## submodule
- .gitmodule
- 紀錄 repo 位置
- 紀錄 commit
- git submodule sync
- git submodule init
- git submodule update
- git submodule update --init
----
## Other trick
- git stash (save / list / pop)
- git blame
- git tag
- git format-patch -2 -o ./output
- git am ./output
---
# Git Server
[GitHub](https://github.com) / [GitLab](https://about.gitlab.com) / [Gogs](https://gogs.io) / [Gitea](https://gitea.io/zh-TW/) / [BitBucket](https://bitbucket.org) / Etc...
----
## 本地端版控
![](https://denny.one/git-slide/img/18333fig0101-tn.png)
----
## 集中式
![](https://denny.one/git-slide/img/18333fig0102-tn.png)
----
## 分散式
![](https://denny.one/git-slide/img/18333fig0103-tn.png)
----
![](https://denny.one/git-slide/img/local-remote.png)
[Git Cheatsheet Cht](http://scars377.github.io/git-cheatsheet-cht/#loc=remote_repo)
----
## 以 GitHub 為例
申請 [GitHub](https://github.com) 帳號
新增 repo
- README.md ([Markdown](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet))
- [License](https://www.openfoundry.org/tw/licenses)
- [.gitignore](https://github.com/github/gitignore) ([Large file sortage](https://git-lfs.github.com))
- Public / Private
----
## Git Clone
- git clone (downstream / upstream)
- git remote add origin https://...
- git remote set-url origin git@://...
- git remote (add / show / -v)
- git fetch
- git pull (fetch + merge)
- git push (-u)
----
## SSH vs HTTPS
- git clone https://...
- git clone git@...
- ssh key (~/.ssh/id_rsa.pub)
---
# X,東西寫炸了!!
git clone https://github.com/calee0219/Frontier
----
## 找戰犯囉~
- git log ( --author / --since= / --until= / regex)
- git show
- git blame
----
## 尋找錯誤
- git diff
- git difftool
- git checkout
----
## 暫存
- git stash
- `git stash save "msg"`
- `git stash list / show / apply / drop / pop`
- [參考](https://git-scm.com/book/zh-tw/v1/Git-工具-儲藏-Stashing)
----
## 工作目錄
- 創造出另一個工作目錄 (通常建再版控以外的地方)
- `git worktree add -b branch /path/to/new/dir`
- [參考](https://ihower.tw/blog/archives/8740)
---
## 復原
- git reset (\-\-hard)
- git revert
- [參考差別](https://github.com/geeeeeeeeek/git-recipes/wiki/5.2-代码回滚:Reset、Checkout、Revert的选择)
- 只 reset 某一個檔案
- git checkout HEAD(commit) \-\- {file_name}
----
## 刪除 untrack
git clean (-f -d)
---
## Others
- fork
- submodule
- issue
- pull request / merge request
- CI (Continuous Integration)
- downstream / upstream
---
# Git Flow
- [git flow](http://nvie.com/posts/a-successful-git-branching-model/)
- [github flow](https://guides.github.com/introduction/flow/)
- [gitlab flow](https://about.gitlab.com/2014/09/29/gitlab-flow/)
----
### Git Flow
![](http://lanziani.com/slides/gitflow/images/gitflow.png)
----
### GitHub Flow
![](https://guides.github.com/activities/hello-world/branching.png)
----
### GitLab Flow
![](https://about.gitlab.com/images/git_flow/environment_branches.png =450x)
---
## How about free5GC
- submodule
- upstream first
- private repo
- [Git workflow](https://free5gc.atlassian.net/wiki/spaces/FREE5GC/pages/144637953/Git+workflow)
----
![](https://i.imgur.com/6lQYedJ.png =450x)
----
0. `git clone --recursive git@bitbucket.org:free5gc-team/free5gc.git`
1. Open task
2. Create feature branch on submodule (NF/lib)
3. work & commit on submodule
4. Merge feature branch back to develop branch (using PR)
5. Create branch on `free5gc` (main repo)
6. Add develop commit of your feature
7. Merge branch to develop (using PR)
---
## 參考資料
- [Git](https://git-scm.com)
- [完整學會Git GitHub Git Server的24堂課](http://www.books.com.tw/products/0010690010)
- [Danny git 介紹](https://denny.one/git-slide)
- [moztw 貢獻 快速指南](https://hackmd.io/EYRgnADAZgpgHAEwLRQEwgMZICxjMuYKOJAZlQRgQFZS4A2CU4IA?view)
- [Code Smart, Don't Code Hard](https://speakerdeck.com/crboy/code-smart-dont-code-hard)
- [為你自己學 Git](http://gitbook.tw/)
{"metaMigratedAt":"2023-06-14T13:03:28.870Z","metaMigratedFrom":"YAML","title":"Git 入門","breaks":true,"description":"WDC Git 入門","lang":"zh-tw","dir":"ltr","robots":"index, follow","GA":"UA-100433652-1","disqus":"calee","contributors":"[{\"id\":\"7e8541c8-f55f-499e-991d-1bfe90b9cbb8\",\"add\":1382,\"del\":34}]"}