隨手紀錄一下日常遇到的git問題
重新命名 master 為 main 分支
因為 visual studio 開專案預設還是使用 master 當作分支 , 如果還在 local 的話可以這樣做
1 | git branch -m master main |
因為 gitlab
現在預設都用 main
如果要匯入 gitlab
的話 , 在 gitlab
點加號 Create new project
=> Create blank project
填入你的專案名稱等等 , 記得不要勾加入 readme
再次確認你現有的 local project 看看分支是否為 main
最後輸入以下指令即可
1 | git remote add origin https://oooooxxxxx/ladisai/DemoApiDoc.git |
中文亂碼
參考以下文章
powershell
https://www.cnblogs.com/Laggage/p/12301495.html
git bash
https://dotblogs.com.tw/H20/2018/06/22/111411
遠端切換分支
參考
https://stackoverflow.com/questions/67699/how-to-clone-all-remote-branches-in-git
自己的 git 太廢學了又忘筆記一下常用的多人開發狀況
A 開發者
1 | 列出所有分支 |
B 開發者
1 | 下載遠端追蹤分支 |
visual studio clone 分支
如果要用 visual studio
切換的話請參考 這篇
先點右下角的 Manage Branches
接著他會 show 出 本地 (local)
分支及 遠端 (remotes/origin)
分支
接著點開 遠端 remotes
分支看看目前有哪些可用分支 , 點選 New Branch
建立跟 遠端 reomtes
一樣的分支名稱 , 本例為 ladisai
, 接著選你要 based
的遠端分支 , 本例為 origin/ladisai
記得要勾選 checkout
及 Track remote branch
此時專案應該就正常了
救援
腦子不清楚寫錯 code 時放棄目前所有檔案變更
1 | git reset --hard HEAD |
將自己的分支上傳前被遠端 reject 以後 pull 時用
只更新索引不強制復原檔案(預設就是 –mixed)
1 | git reset --mixed |
修正之前寫錯的 commit (會自動帶出之前 commit 的 message 內容)
或是追加檔案在最後一個 commit 注意只在 local 使用
1 | git commit -amend |
還原誤刪的檔案
1 | rm test.html |
log
log 近三次的
1 | git log -3 |
log 單一檔案
1 | git log gy.html |
log 詳細單一檔案
1 | git log -p gy.html |
log 作者名稱 gy
1 | git log --oneline --author="gy" |
log 找罵客戶的
1 | git log --oneline --grep="gy" |
找內容有髒話的
1 | git log -S "gy" |
參考自遺留系統重建實戰
找 10 天內最常修改的 source code"[^\s]"
=> 去除空白行
特別要注意排序部分有可能有相同數量的檔案 , 所以依照 count 接著 name 進行排序
bash
1 | git log --since="10 days ago" --pretty=format:"" --name-only | grep "[^\s]" | sort | uniq -c | sort -nr | head -10 |
powershell
1 | git log --since="10 days ago" --pretty=format:"" --name-only | Select-String -Pattern "[^\s]" | Group-Object | sort count , name -desc | select Count , Name -first 10 |
刪檔
讓 git 刪除檔案
1 | git rm index.html |
讓檔案變成 untrack 狀態
1 | git rm --cached index.html |
刪除火大的 Untrack file Thumbs.db 參考
1 | git clean -f |
忽略檔案
git 通靈目錄
1 | mkdir test |
git 忽略檔案
1 | 建立忽略文件列表 |
.gitignore template
visual studio 底下建 .gitignore 的方法
git you give love a bad name
1 | git blame bonjovi.html |
萬一被 cache 鎖住更新不了可以參考這篇
1 | git rm -r --cached . |
fatal: fsync error on ‘.git/objects/pack/tmp_pack_DZTfQ3’: Bad file descriptor
今天遇到這個問題 , 我是用 visual studio 開啟專案 , 然後開 git bash 執行 pull 發生的 , 怎麼樣都沒法正確 pull 下來
後來參考老外
搞了半天最後把 visual studio 關閉 , 然後用 git bash pull 就成功了 , 不曉得是不是 lock 什麼檔案
1 | fatal: fsync error on '.git/objects/pack/tmp_pack_DZTfQ3': Bad file descriptor |
repository 改名
今天在用 git 遇到的問題 , 原本的 repository 被改名了 , 這樣比較好運的意思
只要設定下面這個就搞定 git remote set-url origin https://xxx.com.tw/oxapi.git
1 | remote: To create a merge request for xxxapi, visit: |
排除 web.config 裡面的敏感資訊
最近遇到一個問題 , 業主要求要給 git 版控紀錄 , 老實說還真不想給 , 這樣內部密碼等敏感資訊不就裸奔 , 所以研究一下怎麼搞 , 主要參考老外 及這篇 也可以參考微軟
新增一個連線字串的檔案 ConnectionStrings.config
然後把原本的連線字串都丟進來 , 這裡用 LocalDb
1 | <connectionStrings> |
接著修改 web.config
的內容 , 找到原本連線字串的地方 , 改為這樣
1 | <configuration> |
在 .gitignore
裡面加上排除條件
1 | # 排除 web.config 裡面的敏感資訊 |
執行 git status
大概會長這樣 , 這樣就排除了
1 | git status |
接著隨便寫段 code 連看看應該就沒事了
1 | public class DapperBaseRepository |
1 | <appSettings file="AppSettingsSecrets.config"> |
接著新增 AppSettingsSecrets.config
注意要在 gitignore 裡面先設定
1 | <appSettings> |
撈資料
1 | var x = System.Configuration.ConfigurationManager.AppSettings["mailAccount"]; |
本地看不到遠端分支
今天發生個詭異問題 , gitlab 上面看得到分支 , 但是自己電腦上看不到遠端分支 , 簡直活見鬼
1 | #看不到 |
將 local 現有專案 push 至 gitlab 的方法
首先在 gitlab 點選 create blank project
由於現在 gitlab 都會用 main 當作主支 , 所以 init 時記得設定下
1 | git init --initial-branch=main |