---
# System prepended metadata

title: Git版本控制
tags: [git]

---

# Git版本控制

###### tags: `git`

## Git分散式版本控制
>Git是一套免費、開源、且有許多人使用的分散式版本控制軟體，普遍適合各種開發流程，受到多數人喜愛。

### Git != GitHub:
* Git：是一套免費、開源分散式的版本控制工具
* GitHub：以 Git 為核心技術基礎的「雲端版本控制服務平台」，類似的服務還有 GitLab、Bitbucket

### Git初始化:

```
# 第一次使用git需要設定
git config --global user.username "你的github使用者名稱"
git config --global user.email "你的信箱"

# 查看是否設定成功
git config --list
```

### Git主要步驟:
1. 初始化
```bash=
# 進到該檔案路徑下初始化git
git init
```
2. add索引
```bash=
# 加入檔案到stage area
git add file  # add某個檔
git add .     # add全部檔案

# undo add
git reset
```
3. commit
```bash=
# 把檔案從stage area移交至git repository，-m代表message
git commit -m "變更內容"
```
4. 設定遠端的github
```bash=
# 把遠端的 HTTPS 網址(github專案)註冊起來，並且將這個遠端主機命名為 origin
git remote add origin [your github repo]

# 確認origin 這個名稱指向了 GitHub 位址
git remote -v 

# 修改origin的url
git remote set-url origin [your github repo]
```
5. push推到github
```bash=
# 把本機的local repository推到雲端上的gihub repository，-u 是 --set-upstream 的縮寫，
# 一旦設定過 upstream，以後就不用每次都要打 origin master。
git push -u origin master
```
檢查git狀態
```bash=
# 隨時可以檢查目前git的狀態
git status
```
6. clone下來再push到自己的
```bash=
git clone <url>
git remote remove origin
git remote add origin https://github.com/github帳號/檔名.js
git push origin master
```

## Git合併

### fast-farward merge(快轉合併)

merge指令預設是fast-farward

![](https://i.imgur.com/DyRycLI.png)

### non-fast-farward merge

![](https://i.imgur.com/nUd1yr4.png)

使用`--no-ff`的flag代表不要fast-farward

```bash=
$ git checkout -b myfeature develop    #建立兩個分支並切換到myfeature分支上做功能開發
Switched to a new branch "myfeature"

$ git checkout develop      #開發完畢後，切換到develop分支
Switched to branch 'develop'

$ git merge --no-ff myfeature   #把myfeature的分支內容merge到當前分支(develop)
Updating ea1b82a..05e9557
(Summary of changes)
```

## 移除Git分支

Repos often have a main branch for the main codebase and developers create other branches to work on different features.

**Once work is completed on a feature, it is often recommended to delete the branch.**

```bash=
# delete branch locally
git branch -d localBranchName

# delete branch remotely
git push origin -d remoteBranchName
```

If you get the error below, it may mean that someone else has already deleted the branch.

```
error: unable to push to unqualified destination: remoteBranchName The destination refspec neither matches an existing ref on the remote nor begins with refs/, and we are unable to guess a prefix based on the source ref. error: failed to push some refs to 'git@repository_name'
```

Try to synchronize your branch list using:

```bash=
git fetch -p
```

**The `-p` flag means "prune". After fetching, branches which no longer exist on the remote will be deleted.**


## Git貼標籤(tag)

移除tag，再新增(local/remote都要做)

```bash=
git tag v0.1.0 -d
git tag v0.1.0
git push origin v0.1.0 -d
git push origin v0.1.0
```

## git-flow/github-flow/gitlab-flow

### Git-flow

![](https://i.imgur.com/BbJEktC.png)

### Github-flow




:fire: [git flow vs github flow vs gitlab flow [medium 沈一二]](https://medium.com/@lf2lf2111/%E4%B8%89%E7%A8%AE%E7%89%88%E6%8E%A7%E6%B5%81%E7%A8%8B-29c82f5d4469)
:fire: [Topic分支和integration分支的運用實例-猴子](https://backlog.com/git-tutorial/tw/stepup/stepup1_5.html)
:fire: [A successful Git branching model](https://nvie.com/posts/a-successful-git-branching-model/)


## 參考
* [Git與GitHub介紹，軟體版本控制基本教學](https://tw.alphacamp.co/blog/git-github-version-control-guide)