Git常用操作命令
阿新 • • 發佈:2021-06-19
- 把當前目錄變為git倉庫
git init
- 關聯到遠端倉庫
# 新增關聯的倉庫地址
git remote add origin [email protected]:wintests/gitDemo.git
# 刪除當前已關聯的倉庫地址
git remote rm origin
- 檢視遠端庫的詳細資訊
git remote -v
- 克隆程式碼到本地
git clone [email protected]:wintests/gitDemo.git
- 檢視分支
# 檢視當前分支 git branch # 檢視遠端分支 git branch -r # 檢視所有分支 git branch -a
- 切換到 dev 分支
git checkout dev
- 建立並切換到新分支
git checkout -b 新分支
- 檢視當前分支狀態
git status
- 新增檔案到暫存區
# 新增指定檔案到暫存區
git add api/user.py
# 新增目錄下所有改動檔案到暫存區
git add .
- 提交
git commit -m "提交文字描述"
- 將程式碼推送到伺服器主分支
git push origin master
- 更新本地的倉庫
# 不填寫,則預設當前分支
git pull
# 指定分支
git pull origin test_002
- 合併分支
# 把 dev 分支的程式碼合併到當前分支
git merge dev
- 建立分支
# 本地建立分支
git branch test_002
# 將分支推送到伺服器
git push origin test_002
注意:
stash
命令僅針對在git版本控制中的檔案,如果是新增檔案,那麼需要先通過 git add 把新增檔案加到git版本控制,然後再使用 stash 命令。
- 儲存當前工作進度(執行後工作區/暫存區被還原到修改之前)
# 直接儲存
git stash
# 儲存時新增備註,主要為了方便查詢
git stash save "新增備註資訊"
- 顯示所有已儲存的工作進度
git stash list
- 恢復工作進度,不會刪除該條儲存進度
# 預設取 stash@{0} 進行恢復
git stash apply
# 從已儲存工作進度中,指定一個恢復
git stash apply stash@{num}
- 刪除單個工作進度
# 預設取 stash@{0} 進行刪除
git stash drop
# 從已儲存工作進度中,指定一個刪除
git stash drop stash@{num}
- 恢復工作進度,同時會刪除該條儲存進度
# 預設取 stash@{0} 進行恢復
git stash pop
# 從已儲存工作進度中,指定一個恢復
git stash pop stash@{num}
- 刪除所有儲存的工作進度
git stash clear
- 放棄工作區的修改
git checkout -- api/user.py
- 放棄暫存區的檔案
git reset HEAD api/user.py
- 刪除檔案
git rm -rf api/user.py
- 刪除分支
# 刪除本地分支
git branch -d test_003
# 刪除遠端分支
git push origin --delete test_003
- 檢視歷史記錄
# 顯示詳細提交日誌(如果要退出,輸入字母Q即可)
git log
# 只顯示commit版本號和備註資訊
git log --pretty=oneline
# 檢視所有分支的所有操作記錄(包括被刪除的)
git reflog