常用的Git指令
Published in
May 31, 2024
強調一下:「我」常用的Git指令😄
分支
git有分成local/remote的分支。以下為local分支:
- 建立分支
git branch <branch_name>
- 建立並切換到該分支
git checkout -b <branch_name>
- 切換分支
git checkout <branch_name>
- 合併分支
git merge <branch_name>
- 查看local有哪些分支
git branch
- 查看remote有哪些分支
git branch -r
local與remote的差別是,remote會看到其他開發者的分支,並且帶上repo的前綴:origin/develop
、origin/fix-style
。
- 查看local、remote所有分支
git branch -a
- 刪除local分支
git branch -D <branch_name>
- 刪除remote分支
git push origin -d <branch_name>
- 同步local、remote分支(刪除不存在於remote中的local分支)
git fetch -p
如果沒有local跟remote沒有同步的話可能是因為:
- local分支沒有與remote分支關聯,那就不會同步
將local與remote關聯:
git branch --set-upstream-to=<remote>/<branch_name> <local_branch_name>
2. remote分支已被刪除,但local還沒被刪除,那就不會同步
刪除local分支:
git branch -D <branch_name>
若已開發完成要刪除分支,不要只刪除local端的分支,要連同remote一起刪除:
git branch -d <branch_name>
git push <remote> --delete <branch_name>
暫存
有時候工作還沒告一段落還沒推commit上去,切換分支時就會不讓我切,這時候就可以把改一半的內容暫存起來
- 暫存修改
git stash save "暫存訊息"
// 也可以直接
git stash
- 查看暫存列表
git stash list
stash@{0}: xxxxx
stash@{1}: xxxxx
stash@{2}: xxxxx
- 恢復暫存並且刪除該暫存資料
git stash pop stash@{0}
- 直接刪除暫存資料並不同步更新
git stash drop stash@{0}
- 直接刪除所有stash
git stash clear