Git 기본 튜토리얼

10581 단어 gitbeginnerstutorial

힘내 튜토리얼



Git 명령 影響範圍





Gitlab 專案初始化



명령줄 지침

Git 전역 설정

git config --global user.name "Corey Lai"
git config --global user.email "[email protected]"


새 저장소 만들기

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


기존 폴더

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


기존 Git 저장소

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端建立一個

$ git init


從gitlab內載入已經建立的庫

$ git clone http://gitlab/zk/deplm.git


현지 端操作



顯示目前已修改文件

$ git status


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

$ git diff


將修改檔案加入index

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


提交更改

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


자식 역사



顯示歷程

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


分支



자식 분기

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


자식 태그
給當前提交設標籤

$ git tag <tag-name>


更新與發佈



자식 원격

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


자식 가져오기

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


자식 풀

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


合併



자식 병합

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


자식 리베이스

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


撤銷



자식 리셋

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


자식 되돌리기

# 回到前一次 commit 的狀態
$ git revert HEAD 


本機憑證快取



記憶憑證

git config credential.helper store


清除憑證

git config --global --unset credential.helper
git config --system --unset credential.helper


힘내 체크 아웃



Git 從特定 분기 取得特定檔案

$ 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. 開始

# 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 병합 분기




$ git checkout release
$ git merge development
$ git add .
$ git push

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


힘내 기능



1. 오리진/마스터 브랜치를 기능 브랜치에 병합




# 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. 기능 브랜치를 오리진/마스터 브랜치에 병합




$ git checkout master
$ git pull origin master

$ git merge feature
$ git push origin master


.gitignore 파일




git rm -r --cached .
git add .
git commit -m ".gitignore"


자식 분기



특징




# 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 submodule add <repository> [<path>]</path></repository>

좋은 웹페이지 즐겨찾기