Java

git 實用指令

79 / 100

git 實用指令

程式版本控制 on GitHub or Gitee or Bitbucket or GitLab 已有說明如何:將已有的地端專案上傳到雲端 GitLab 的雲端程式碼管理庫,在程式開發的過程中,有時會發生需要人工介入的指令,在此文章會將狀況說明,並將解決方式記錄,此篇文章將會持續更新,不再另外撰寫文章說明。

  • 憑證 SSL 錯誤
# 憑證若為自已產生時, git 會出現 SSL 錯誤: SSL certificate problem: self signed certificate, 可以執行下列指令
git config --global http.sslVerify false
  • 清空本地端的密碼

# 再重新 push 時,就會要再次輸入帳號密碼
git config --system --unset credential.helper

若是 windows 密碼變更時, 請到 Windown Credential 作變更

alternate text

  • 全域的設定 global setup

git config --global user.name "username"
git config --global user.email "username@yourmail"

# 設定 git 在 VS Code 全域的預設編輯器
git config --global core.editor "code --wait"

# 上述指令的另一指令如下
git config --global core.editor "code -w"

# Note: The --wait or -w flag is crucial without this git won't know the editing has completed and in turn won't finish executing the git command.
  • 設定 git 在 VS Code 預設編輯器

# 設定 git 在 VS Code 全域的預設編輯器
git config --global core.editor "code --wait" 

# 上述指令的另一指令如下 
git config --global core.editor "code -w" 

# Note: The --wait or -w flag is crucial without this git won't know the editing has completed and in turn won't finish executing the git command.

# Default VS Code As The Git Editor (Local Repo) 
git config --local core.editor "code -w"

#Revert Back To GNU nano (or default) 
git config --global --unset core.editor
  • 下載既有的Repository 並提交檔案 ( Create a new repository )

git clone https://github.com/polinwei/spring.git
cd spring
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
  • 提交一個新的 Repository ( Push an existing folder )

# create a new repository on the command line

echo "# spring" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M master
git remote add origin https://github.com/polinwei/spring.git
git push -u origin master
  • 現有的專案提交到空的 Repository ( Push an existing Git repository )

# push an existing repository from the command line

cd existing_repo
git remote rename origin old-origin
git remote add origin https://github.com/polinwei/spring.git
git push -u origin --all 
git push -u origin --tags
  • 正常提交程式到 Repository

正常的程式開發,通常應該是先下載,然後開發,最後再將已測試驗證的程式 commit & push。整個流程的 git 指令如下:

# 切換到開發的 brunch
$ git checkout master

#下載程式
$ git pull origin master

# 將所有增/修的程式加入索引
$ git add .

# 加入說明並 commit
$ git commit -m "Initial commit"

# 將程式上傳
$ git push -u origin master

# 查看狀況
$ git status
  • 強制 Push 程式碼, 並覆蓋

當兩位程式開發者在同時開發相同系統,或者同一位開發者在兩台電腦開發,開發過程中已有一些程式 commit & push,但希望強制由最後開發者的版本作強制 Push 覆蓋,則 git 指令需加入參數 -f 如下

V:\git\gitlib\apps>git push -u origin dev -f
Enumerating objects: 97, done.
Counting objects: 100% (97/97), done.
Delta compression using up to 4 threads
Compressing objects: 100% (44/44), done.
Writing objects: 100% (61/61), 18.38 KiB | 1.15 MiB/s, done.
Total 61 (delta 14), reused 3 (delta 0)
remote:
remote: To create a merge request for dev, visit:
remote:   https://gitlab.com/polin.wei/apps/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote:
To https://gitlab.com/polin.wei/apps.git
 + 31675f8...533e445 dev -> dev (forced update)
Branch 'dev' set up to track remote branch 'dev' from 'origin'.
  • 使用 git stash 暫存被修改的檔案

Git 有提供一個方法可以快速暫存檔案,然後再叫回來,可以解決突然有發現一個 bug 要立即解決或是你突然有一個 idea,想要做一個簡單的測試。

首先用 用 git stash 把還沒 commit 的檔案都暫存起來

$ git stash             # 暫存目前被修改的檔案
$ git stash list        # 列出所有 stash 紀錄

這時候就可用指令 git checkout <branch name> 切換到要修改的分支,進行作業。當完成後切換回原來的分支時,再用 git stash pop 把暫存的檔案叫回來

$ git stash pop                 # pop 第一個 stash(stash@{0})
$ git stash pop stash@{n}       # 指定要 pop 出來的 stash 編號

使用 git stash pop 之前,要先用 git status 清空,否則如果後來又提交了新的 patch,再做 stash pop 有可能會發生 conflict,發生 conflict 的時候,stash 不會被清掉,所以可以 git reset --hard HEAD 還原之後,再重做一次 git stash pop

使用 git stash drop 丟棄暫存的檔案

git stash drop 會丟棄 stash list 裡面的第一個 stash,也就是 stash@{0},可以在後面加上 short name,指定要丟棄的 stash 編號

$ git stash drop                # 丟棄第一個 stash(stash@{0})
$ git stash drop stash@{n}      # 丟棄編號 n 的 stash
使用 git stash clear 清空 stash list
$ git stash clear

 

參考:

Make VS Code Your Default Git Editor

How to use Visual Studio Code as Default Editor for Git