Git Basic Tutorial
Tags: tutorial, git, beginners
Published: 2022-03-01 06:40:12

---
title: Git Basic Tutorial
tags: tutorial, git, beginner
author: Corey Lai
---
# Git Tutorial
### Git Command 影響範圍
![enter image description here](https://cdn-images-1.medium.com/max/1600/0*V58Cll7ybTILOc3s)

## Gitlab 專案初始化
**Command line instructions**

**Git global setup**
```bash
git config --global user.name "Corey Lai"
git config --global user.email "corey.lai@deanshoes.com"
```
**Create a new repository**
```bash
git clone http://gitlab/ws/wsrm-react-ide.git
cd wsrm-react-ide
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
```
**Existing folder**
```bash
cd existing_folder
git init
git remote add origin http://gitlab/ws/wsrm-react-ide.git
git add .
git commit -m "Initial commit"
git push -u origin master
```
**Existing Git repository**
```bash
cd existing_repo
git remote add origin http://gitlab/ws/wsrm-react-ide.git
git push -u origin --all
git push -u origin --tags
```
## 創建
**在local端建立一個**
```bash
$ git init
```
**從gitlab內載入已經建立的庫**
```bash
$ git clone http://gitlab/zk/deplm.git
```
## Local 端操作
**顯示目前已修改文件**
```bash
$ git status
```
**顯示與上次提交版本不同處**
```bash
$ git diff
```
**將修改檔案加入index**
```bash
# 所有修改進行提交
$ git add .
# 指定特定文件進行提交
$ git add -p{patch 批次確認} <filename>
# 解決衝突後，將檔案標記為以解決
$ git add <resolved-filename>
```
**提交更改**
```bash
$ git commit -a{略過 git add 步驟}
$ git commit -m "comment"
$ git commit --amend {撤回提交}
```
## git history
**顯示歷程**
```bash
$ git log
#顯示指定檔案所有修改
$ git log -p <filename>
```
## 分支
**git branch**
```bash
# 顯示所有分支
$ git branch -av
# 基於當前分支創建新分支
$ git branch <new-branch>
# 刪除本地分支
$ git branch -d <branch>
# 刪除遠端分支
$ git branch -dr <branch>
```
**git tag**
給當前提交設標籤
```bash
$ git tag <tag-name>
```
## 更新與發佈
**git remote**
```bash
# 列出遠端庫
$ git remote -v
# 新增遠端庫
$ git remote add <nickname> <url>
```
**git fetch**
```bash
# 取得遠端庫所有變動，不合併到local
$ git fetch <remote>
# 
```
**git pull**
```bash
# 取得遠端庫所有變動，不合併到local
$ git pull <remote> <branch>
$ git pull origin development
```
## 合併
**git merge**
```bash
# 將當前分支合併到指定分支
$ git merge <branch>
```
**git rebase**
```bash
# 將當前分支重新接枝到指定分支
$ git rebase <branch>
# 終止rebase
$ git rebase --abort
# 解決衝突後繼續重新接枝
$ git rebase --continue
```
## 撤銷
**git reset**
```bash
# 放棄目前所有修改
$ git reset --hard HEAD
# 回到指定commit，並將之後修改標記
$ git reset <commit>
# 放棄目前所有修改，並回到指定commit
$ git reset --hard <commit>
# 回到指定commit，並保留local尚未提交的修改
$ git reset --keep <commit>
# 取消已 add file
$ git reset HEAD <filename>
```
**git revert**
```bash
# 回到前一次 commit 的狀態
$ git revert HEAD 
```
## 本機憑證快取
**記憶憑證**
```bash
git config credential.helper store
```
**清除憑證**
```bash
git config --global --unset credential.helper
git config --system --unset credential.helper
```

## Git checkout
**Git 從特定 branch 取得特定檔案**
```bash
$ git checkout <another-branch> <path-to-file> [<one-more-file> ...]
$ git status
$ git commit -m "'Merge' specific file from '<another-branch>'"
```

## Git 標準流程
如專案已從 Gitlab 上下載並開發完成請從 Step 5. 開始
```bash
# Step 1. 
$ git clone http://gitlab/zk/<project>.git
# Step 2. 
$ git checkout development
# Step 3. 
$ git pull
# Step 4.
Coding ...
# Step 5.
$ git add <file> or . {all file that was changed}
# Step 6. check the files you add
$ git status
# Step 7.
$ git commit -m "comment"
# Step 8.
$ git push origin development
# Step 9. git conflict happen
Resolve... 
# Step 10. git conflict happen
$ git add <resolved-file>
# Step 11.
$ git push origin development
```

## Git merge branch
```bash
$ git checkout release
$ git merge development
$ git add .
$ git push

### forced update
$ git checkout release
$ git reset --hard development
$ git push --force origin release
```

## Git feature
### 1. merge origin/master branch to feature branch
```bash
# step1: change branch to master, and pull to update all commits
$ git checkout master
$ git pull

# step2: change branch to target, and pull to update commits
$ git checkout feature
$ git pull

# step3: merge master to feature(⚠️ current is feature branch)
$ git merge master
```
### 2. merge feature branch to origin/master branch
```bash
$ git checkout master
$ git pull origin master

$ git merge feature
$ git push origin master
```

## gitignore file
```bash
git rm -r --cached .
git add .
git commit -m ".gitignore"
```

## git branch
### feature
```bash
# create feature
git checkout -b desc-feature master
git status
git add <some-file>
git commit

# finish dev
git push -u origin desc-feature
git push

# merge master
git checkout master
git pull
#git pull origin desc-feature
git merge desc-feature

git push

git branch -d desc-feature

```

## Git module
```
git submodule add <repository> [<path>]</path></repository>
```