# Git 常用指令 [TOC] --- ## 一、帳號設定 ### 列出git設定參數(全域) ```bash= $ git config --global --list ``` #### 如何退出 list 的結果 按 `q` ### 修改使用者名稱與信箱 commit上識別用 #### 全域設定請加上 `--global` ```bash= $ git config --gloabl user.name "你的名字" $ git config --global user.email "你的Email" ``` #### 專案內設定用 `--local` 這個設定只會在執行以下指令的根目錄下適用,通常就是針對某個 repository。 ```shell $ git config --local user.name "你的名字" $ git config --local user.email "你的Email" ``` ### 重設git密碼 #### 重設所有本機端上的git密碼 以下指令會把目前Windows上已登入的git都清除。 ```bash= $ git credential-manager uninstall ``` #### 重設 Global 密碼 這個應該只會重設密碼,然後在下次push等行為時會跳出帳號密碼輸入提示。 ```bash= $ git config --global --unset user.password ``` #### Windows 10 可參考 1. 開啟控制台 2. 檢視方式改為圖示 3. 點選認證管理員(Credential Manager) 4. 選擇新視窗中的Windows 認證 5. (清單) 一般認證 6. 編輯需要修改的git帳號密碼 ![](https://i.imgur.com/mjKizzo.png) > 此章節參考來源: > [StackOverflow--How do I update the password for Git?](https://stackoverflow.com/q/20195304/9982091) ## 二、建立git本地儲存庫(Repository) 以下行為都請先cd到要建立儲存庫的資料夾。 ### 初始化git環境 ```bash= $ git init ``` 執行成功後會在根目錄下產生一個git的資料夾,通常為隱藏檔。 ### 加入要版控的檔案(track) #### 個別加入 ```bash= $ git add 檔案名稱或路徑 ``` #### 全部加入 ```bash= $ git add . ``` ### 檢查是否有加入成功 ```bash= $ git status ``` ### 建立一個 commit ```bash= $ git commit -m "本次提交的註解" ``` ### 查看commit紀錄 ```bash= $ git log ``` ## 三、分支 ### 建立分支 ```bash= $ git branch 新分支的名稱 ``` ### 切換目前分支 ```bash= $ git checkout 要切換的分支名稱 ``` ## 四、遠端儲存庫 請先cd到要儲存遠端儲存庫的目錄下。 ### 複製遠端儲存庫: clone ```bash= $ git clone <遠端路徑> <指定的本地端資料夾名稱(選填)> ``` ### 設定遠端儲存庫: remote ```bash= $ git remote add 遠端名稱 遠端URL ``` ### 推送至遠端儲存庫: push ```bash= $ git push -u 遠端名稱 遠端分支名稱 ``` > **-u 等於 --set-upstream** > > *upstream,中文翻譯成「上游」。不要被名詞嚇到了,這 upstream 的概念其實就只是另一個分支的名字而已。在 Git 裡,每個分支可以設定一個「上游」(但每個分支最多只能設定一個 upstream),它會指向並追蹤(track)某個分支。通常 upstream 會是遠端 Server 上的某個分支,但其實要設定在本地端的其它分支也可以。如果有設定,當下次執行 git push 指令的時候,它就會用來當預設值。*-- [Push 上傳到Github--為你自己學Git](https://gitbook.tw/chapters/github/push-to-github.html) ### 取得遠端的狀態(reference): fetch ```bash= $ git fetch ``` ### 拉下遠端的資料: pull ```bash= $ git pull ``` > [Pull 下載更新](https://gitbook.tw/chapters/github/pull-from-github.html) ### 拉取遠端中的其中一部份 sparse checkout > [善用 Git 的 sparse checkout 跟 shallow clone/pull 來提高工作效率](https://www.peterdavehello.org/2015/05/use-git-sparse-checkout-and-shallow-clone-pull-to-increase-work-efficient/) > [name=Mos 大神][time=Tue, Dec 29, 2020 9:25 AM] ## 五、其他 ### 匯出git log成csv ```bash= $ git log --pretty=format:%h,%an,%ae,%s,%b > D://gitLog.csv ``` > [GitDoc_PRETTY FORMATS](https://git-scm.com/docs/pretty-formats) > [Export GIT LOG into an Excel file](https://stackoverflow.com/q/39253307/9982091) ### 重設 .gitignore > 有時候會遇到專案一開始沒有設定 .gitignore,造成所有檔案都加入到git裡。 1. 建好要使用的 `.gitignore` 於專案根目錄 2. `git rm -r --cached <TARGET FILE>` (將資料從 git 中移除) - `<TARGET FILE>` 請替換成要移除的檔案。如果是 `.` 會是移除全部。 3. `git add <NEW FILE>` (重新將檔案加入 git。) - `<NEW FILE>` 請替換成要加入的檔案。如果是 `.` 會是加入全部。 4. 重新 commit 即可。 > [How to refresh gitignore](https://sigalambigha.home.blog/2020/03/11/how-to-refresh-gitignore/) > [【狀況題】有些檔案我不想放在 Git 裡面…](https://gitbook.tw/chapters/using-git/ignore.html) > [How can I make Git "forget" about a file that was tracked, but is now in .gitignore?](https://stackoverflow.com/questions/1274057/how-can-i-make-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitign) ### 更新 Git 版本 :::info 👉 [Git 更新版本](/CgEG3Q7TQ2CgPdxu3InzOA) ::: ### `.DS_Store` 該丟上 Git 嗎? 基本上不建議,而且如果開發環境是 Windows 跟 MacOS 雙棲的話,真的可以不用。 想了解更多細節,可以參考 Ray 大大寫的這篇 [靠北 .DS_Store 到底是什麼?該不該加入到版本控制內?](https://israynotarray.com/git/20220724/3916507104/)。 ## 六、參考資料 - [高見龍-為你自己學git](https://gitbook.tw/) - [git 官方文件](https://git-scm.com/book/en/v2) ###### tags: `Git`