說明
常用 Git Commands 記錄
Commands
Common
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| git pull
git pull --progress -v --no-rebase "origin"
git log --stat -p .
git show {{commit id | 前 6 碼}}
git ls-files git ls-tree --full-tree -r --name-only HEAD
git ls-tree -r HEAD --name-only
git remote set-url origin {git url}
git clone {git url}
git rm {file path}
|
Commit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| git status .
git ls-files -m
git add {{file path}}
git add -u
git add -e
git diff {file path}
git diff --name-status
git restore --staged {file path}
git commit
|
Push
1 2 3 4 5 6 7 8 9
| git remote -v origin ... origin ...
git push {remote name} {branch name}
|
Branch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| git branch
git branch -a
git branch {new branch name}
git checkout {to branch name}
git merge {from branch name}
git merge --abort
git branch -d {branch name}
git push origin :{branch name}
|
Conflict
Solution A
git status
看是那個檔案的狀態是 both modified
- 透過 vi 或其它的文字編輯器查看異動地方,並修正
1 2 3 4 5
| <<<<<<< HEAD (目前的 branch) <div>我是 Cat</div> ======= <div>我是 Dog</div> >>>>>>> master (合併進來的)
|
- 利用
git add {file path}
to mark resolution
git commit
Solution B
git status
看是那個檔案的狀態是 both modified
- 直接使用某一方的 Code
1 2 3 4 5
| git checkout --theirs {file path}
git checkout --ours {file path}
|
git commit
Delete Remote Commit
- 先 刪掉 現存的所有的 clone
最好是團隊所有人都刪掉,如果沒先刪掉的話,就算 A 刪掉了 XXX Commit ,但 B 在刪之前就先把 XXX Commit 拉下來了,這時後 A 刪完後, B 又再拉下來後就會把 XXX Commit 又再 Merge 進去
- 重新抓 Code 下來
- 把 Tag 都抓下來
- 退回到想要的版本(軟退回,保留檔案)
- 重新 commit code
- 強制 push 上去蓋掉 remote 上的 trunk
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| rm -rf {folder name}
git clone {repository URL}
git pull --all --tags
git reset --soft HEAD~1
git reset --soft {git commit id}
git add {file name} git commit
git push origin {branches name} --force
git reset --soft {git commit id}
git commit
git push origin {branches name} --force
|
Revert
1 2 3 4 5 6 7 8 9
| git checkout -- {file}
git reset --hard {git commit id} --hard to revert files --soft to keep change
git pull -f origin {branches name}
|
參考
Git 教學
Delete Remote Commit