DEV Community

HyperRedStart
HyperRedStart

Posted on

Git Basic Tutorial

Git Tutorial

Git Command 影響範圍

enter image description here

Gitlab 專案初始化

Command line instructions

Git global setup

git config --global user.name "Corey Lai" git config --global user.email "corey.lai@deanshoes.com" 
Enter fullscreen mode Exit fullscreen mode

Create a new repository

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 
Enter fullscreen mode Exit fullscreen mode

Existing folder

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 
Enter fullscreen mode Exit fullscreen mode

Existing Git repository

cd existing_repo git remote add origin http://gitlab/ws/wsrm-react-ide.git git push -u origin --all git push -u origin --tags 
Enter fullscreen mode Exit fullscreen mode

創建

在local端建立一個

$ git init 
Enter fullscreen mode Exit fullscreen mode

從gitlab內載入已經建立的庫

$ git clone http://gitlab/zk/deplm.git 
Enter fullscreen mode Exit fullscreen mode

Local 端操作

顯示目前已修改文件

$ git status 
Enter fullscreen mode Exit fullscreen mode

顯示與上次提交版本不同處

$ git diff 
Enter fullscreen mode Exit fullscreen mode

將修改檔案加入index

# 所有修改進行提交 $ git add . # 指定特定文件進行提交 $ git add -p{patch 批次確認} <filename> # 解決衝突後,將檔案標記為以解決 $ git add <resolved-filename> 
Enter fullscreen mode Exit fullscreen mode

提交更改

$ git commit -a{略過 git add 步驟} $ git commit -m "comment" $ git commit --amend {撤回提交} 
Enter fullscreen mode Exit fullscreen mode

git history

顯示歷程

$ git log #顯示指定檔案所有修改 $ git log -p <filename> 
Enter fullscreen mode Exit fullscreen mode

分支

git branch

# 顯示所有分支 $ git branch -av # 基於當前分支創建新分支 $ git branch <new-branch> # 刪除本地分支 $ git branch -d <branch> # 刪除遠端分支 $ git branch -dr <branch> 
Enter fullscreen mode Exit fullscreen mode

git tag
給當前提交設標籤

$ git tag <tag-name> 
Enter fullscreen mode Exit fullscreen mode

更新與發佈

git remote

# 列出遠端庫 $ git remote -v # 新增遠端庫 $ git remote add <nickname> <url> 
Enter fullscreen mode Exit fullscreen mode

git fetch

# 取得遠端庫所有變動,不合併到local $ git fetch <remote> #  
Enter fullscreen mode Exit fullscreen mode

git pull

# 取得遠端庫所有變動,不合併到local $ git pull <remote> <branch> $ git pull origin development 
Enter fullscreen mode Exit fullscreen mode

合併

git merge

# 將當前分支合併到指定分支 $ git merge <branch> 
Enter fullscreen mode Exit fullscreen mode

git rebase

# 將當前分支重新接枝到指定分支 $ git rebase <branch> # 終止rebase $ git rebase --abort # 解決衝突後繼續重新接枝 $ git rebase --continue 
Enter fullscreen mode Exit fullscreen mode

撤銷

git reset

# 放棄目前所有修改 $ 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> 
Enter fullscreen mode Exit fullscreen mode

git revert

# 回到前一次 commit 的狀態 $ git revert HEAD 
Enter fullscreen mode Exit fullscreen mode

本機憑證快取

記憶憑證

git config credential.helper store 
Enter fullscreen mode Exit fullscreen mode

清除憑證

git config --global --unset credential.helper git config --system --unset credential.helper 
Enter fullscreen mode Exit fullscreen mode

Git checkout

Git 從特定 branch 取得特定檔案

$ git checkout <another-branch> <path-to-file> [<one-more-file> ...] $ git status $ git commit -m "'Merge' specific file from '<another-branch>'" 
Enter fullscreen mode Exit fullscreen mode

Git 標準流程

如專案已從 Gitlab 上下載並開發完成請從 Step 5. 開始

# 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 
Enter fullscreen mode Exit fullscreen mode

Git merge branch

$ git checkout release $ git merge development $ git add . $ git push ### forced update $ git checkout release $ git reset --hard development $ git push --force origin release 
Enter fullscreen mode Exit fullscreen mode

Git feature

1. merge origin/master branch to feature branch

# 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 
Enter fullscreen mode Exit fullscreen mode

2. merge feature branch to origin/master branch

$ git checkout master $ git pull origin master $ git merge feature $ git push origin master 
Enter fullscreen mode Exit fullscreen mode

gitignore file

git rm -r --cached . git add . git commit -m ".gitignore" 
Enter fullscreen mode Exit fullscreen mode

git branch

feature

# 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 
Enter fullscreen mode Exit fullscreen mode

Git module

git submodule add <repository> [<path>]</path></repository> 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)