常用 git 指令

建立一個新的 repository

  1. Create a new project at your git server(remote)

  2. Initialize your new project at local

  3. Set the remote url

ssh: git@git.server.com:namespace/project.git
https: https://git.server.com/namespace/project.git

1
git remote add origin <remote_url>
  1. Push your project
  2. Done

config

檢查設定

1
git config --list

設定

1
2
$ git config --global user.name "Yuchi Tung"
$ git config --global user.email yuchi@example.com

設定檔路徑

1
~/.gitconfig




初始化

建立一個新的 repository

1
git init

取得 remote repository

1
2
3
git clone <url>

git clone git@gitlab.com:project/myProject.git




基本指令

查看狀態

1
git status

將修改的檔案加入 stage

1
git add  <file_path/file_name>

所有修改的檔案加入 stage

1
git add .

檢視所有 commit

1
git log

檢視特定檔案的所有 commit

1
git log --follow <file_name>

放棄所有沒有 commit 的修改

1
git reset --hard




commit 相關

建立 commit

1
2
git commit
git commit -m "commit message"

修改最後一個 commit 的 message

1
git commit --amend

查看 commit 的 comment

1
git show <commit_id>




branch 相關

查看 branch

1
git branch <branch_name>

列出所有 branch

1
git branch

切換 branch

1
git checkout <branch_name>

建立新 branch 並 checkout

1
git checkout -b <branch_name>

重新命名 branch name

1
git branch -m <new_branch_name>

刪除 branch

1
git branch -d <branch_name>

建立一個與 remote branch 同名的 branch 並 track 它

1
git checkout --track origin/branch_name

設定 branch 的 upstream

1
git branch -u <remote name> <branch name>




push & pull

pull

1
git pull

push

1
git push <remote_name> <branch_name>

強制 push

1
git push <remote name> <branch name> --force

Push the current branch and set the remote as upstream

1
git push -u <remote name> <branch_name>




merge 相關

開發分支上有多 commit,rebase 整理主線進分支,合併線圖,線圖看起來比較乾淨

1
git rebase master

把別的 branch merge 到目前 branch

1
git merge <target_branch_to_merge>

預覽 merge,不是 true merge,若是使用後發先有衝突可以使用 git merge –abort 還原。–no-ff 的意思是不要使用快轉模式合併

1
git merge --no-commit --no-ff <target_branch>




取消

取消這次的修改

1
git checkout -- <my_file>

將檔案移除 stage 狀態

1
2
git reset Head <my_file>
git reset -- <my_file>

取消剛剛的 merge/rebase/reset,回到上一個 commit

1
git reset --hard ORIG_HEAD

回到上一個 commit

1
git reset --hard HEAD^

回到上兩個 commit

1
git reset --hard HEAD~2

回到指定 commit

1
git reset --hard <commit_hsa1>




查詢

顯示 remote branch

1
2
3
git remote show origin
git branch -r
git branch -a

查詢 branch 追蹤的遠端分支

1
git branch -vv

以關鍵字查詢 branch name

1
git branch -a | grep <keyword>




diff

查看 staged files 的更動

1
git diff --staged

查看還沒有加入 stage 的異動

1
git diff <my_file_name>

diff 兩個 branch

1
git diff <branch_1> <branch_2>

diff 單一檔案

1
git diff <branch>:<dir>/<filename> <branch>:<div>/<filename>




stash 暫存

暫存

1
git stash

查看所有 stash

1
git stash list

套用最後一個 stash

1
git stash apply

套用指定的 stash

1
git stash apply <stash@{index}>

使用 cherry pick 挑出指定 commit

1
git cherry-pick <sha1> <sha1>

使用 cherry pick 挑出指定多個指定 commit

1
git cherry-pick <sha1> <sha1>




其他

從 git 中移除已經被 commited 的檔案

除了以下動作別忘了也要將移除的的檔案加入 .gitignore

只移除追蹤,保留檔案

1
git rm --cached <path/file>

移除追蹤,刪除檔案

1
git rm <path/file>

移除追蹤,移除 folder 下所有 file, -r 表 recursive

1
git rm -r --cached <path>

Reference

git cherry-pick 使用指南

How to undo ‘git reset’?

透過rebase -i, reset, revert還原某個commit的方法

Git 版本控制:利用 git reset 恢復檔案、暫存狀態、commit 訊息

How can I see which Git branches are tracking which remote / upstream branch?

Ignoring a directory from a Git repo after it’s been added

在 Merge 之前想試試看有沒有衝突?