# Git 學習筆記 ## SSH 學習 http://qbsuranalang.blogspot.tw/2015/01/gitgithubgitlab.html https://git-scm.com/book/zh-tw/v1/%E4%BC%BA%E6%9C%8D%E5%99%A8%E4%B8%8A%E7%9A%84-Git-%E7%94%9F%E6%88%90-SSH-%E5%85%AC%E9%96%8B%E9%87%91%E9%91%B0 ## git clone 太多了 ```git clone``` 默认会下载项目的完整历史版本,如果你只关心最新版的代码,而不关心之前的历史信息,可以使用 git 的浅复制功能: ```git git clone --depth=1 https://github.com/bcit-ci/CodeIgniter.git ``` ```--depth=1``` 表示只下载最近一次的版本,使用浅复制可以大大减少下载的数据量,例如,CodeIgniter 项目完整下载有近 100MiB ,而使用浅复制只有 5MiB 多,这样即使在恶劣的网络环境下,也可以快速的获得代码。如果之后又想获取完整历史信息,可以使用下面的命令: ```git git fetch --unshallow ``` https://gitbook.tw/chapters/github/pull-from-github.html ## 修改 Commit 修改最後一次 commit ```shell=git git commit --amend ``` 查找修改者 ```shell=git git blame ``` git reset 回復檔案 ```shell=git git reset #放棄 add,但保留修改 git reset --hard #放棄所有修改,回到上個 commit 完成後的狀態 git reset --hard HEAD~2 #放棄所有修改,回到前前一個 commit 版本 ``` 回到上一個版本 ```shell=git git checkout HEAD~2 git reset --soft HEAD^1 #回到前一個 commit 版本 ``` 5.2 代码回滚:Reset、Checkout、Revert 的选择 [參考資料](https://github.com/geeeeeeeeek/git-recipes/wiki/5.2-%E4%BB%A3%E7%A0%81%E5%9B%9E%E6%BB%9A%EF%BC%9AReset%E3%80%81Checkout%E3%80%81Revert-%E7%9A%84%E9%80%89%E6%8B%A9) 細說git reset和git checkout的不同之處 [參考資料](https://medicineyeh.wordpress.com/2015/01/22/%E7%B4%B0%E8%AA%AAgit-reset%E5%92%8Cgit-checkout%E7%9A%84%E4%B8%8D%E5%90%8C%E4%B9%8B%E8%99%95/) 只保留Git追蹤文件 ```shell=git git clean -fdx ``` 其他: ```shell=git #b.檔案:filename.txt 修改,並且已經add git reset --HARD #退到修改或刪除前 (常用) git reset --soft #退到 staging   (已add 但未commit) git reset --mixed #退到 untrack  (還沒add) ``` [參考資料](https://dotblogs.com.tw/wasichris/2016/04/29/225157) [建立以及使用Git的bare repo](https://apc999blog.wordpress.com/2014/05/22/git-%E5%BB%BA%E7%AB%8B%E4%BB%A5%E5%8F%8A%E4%BD%BF%E7%94%A8git%E7%9A%84bare-repo/) [git config --bool core.bare true](https://stackoverflow.com/questions/2816369/git-push-error-remote-rejected-master-master-branch-is-currently-checked) ## 斷行問題 ``` git config --global core.autocrlf true git config --global core.safecrlf true ``` 說明autocrlf = true的作用:commit時會自動將CRLF轉成LF;checkout時會自動將LF轉成CRLF --- ## git cherry-pick 命令 [cherry-pick]( https://ithelp.ithome.com.tw/articles/10139368) --- ### 執行```git reset --hard```復原方式 如果不小心執行 reset 或 rebase 等可能會造成版本號消失的指令時,而我們想要恢復但卻在演進線圖(commit log)上找不到commit結點,這個時候就可透過 git reflog 找出所有歷史紀錄,透過 HEAD@{N} 這個特殊的「參考名稱」來對此版本「定位」,進而使用 git reset --hard HEAD@{N} 指令回歸原有版本。 剛執行了 git reset --hard HEAD~2 來重置,此事若後悔想要恢復,線圖是找不到原始commit節點的 這時候可以執行 git reflog 列出所有歷史紀錄,其中HEAD@{0}~HEAD@{4}都是剛剛進行測試時所產生的,因此需要恢復到執行 reset 指令之前的 commit 結點,也就是HEAD@{5}這個結點位置。 執行 git reset --hard HEAD@{5} 回歸原始版本,會連同working tree(工作目錄)檔案也會一併恢復。 --- 修改倒數第n個 commit(n 為整數) ```shell=git git rebase -i HEAD~n ``` 完成後執行 `git commit —-amend`修改訊息,改完後執行 `git rebase --continue` 完成。 创建远程分支(本地分支push到远程):$ git push origin [name] ## 本地上遠端庫 ```shell=git git pull origin master --allow-unrelated-histories git pull --rebase git push ``` ## 資料損失 人生海海天有不測風雨,硬碟也是 使用 ```git fsck```工具,該工具會檢查倉庫的資料完整性。如果指定 ```--full``` 選項,該命令顯示所有未被其他物件引用 (指向) 的所有物件 若發現 fatal: loose object 86e7247af5865e857a3b61eed99986e2d9538df1 就刪除該物件檔案 ## git pull 錯誤 git fatal: failed to read object xxx: Invalid argument ```shell=git git fsck --full ``` ## 硬碟壞掉修復後資料錯誤處理 解法:回到有檔案的commit另開分支 錯誤訊息 ```shell=git Cloning into 'rwd1147'... done. error: inflate: data stream error (incorrect header check) fatal: failed to read object 59412237ebb05a0ff44d6baff5c367409882971b: Invalid argument warning: Clone succeeded, but checkout failed. You can inspect what was checked out with 'git status' and retry the checkout with 'git checkout -f HEAD' ``` Clone 後卻沒檔案 解答: 看来,git在.git / objects中为新提交创建了文件,但没有成功写入它们。 我解决了它,一次删除它们,并重新运行git fsck --full来找到下一个。 我从最初由git fsck报告的那个开始: 參考資料: * http://stackoverflow.org.cn/front/ask/view?ask_id=80816 * https://stackoverflow.com/questions/23725925/git-repository-corrupt-incorrect-header-check-loose-object-is-corrupt ## 測試 ```shell=git Omar@DESKTOP-0F94SJL MINGW64 /d/Workspace/shopstyle/rwd1033 (master) $ git push Counting objects: 13, done. Delta compression using up to 4 threads. Compressing objects: 100% (13/13), done. Writing objects: 100% (13/13), 8.61 KiB | 0 bytes/s, done. Total 13 (delta 9), reused 0 (delta 0) remote: error: inflate: data stream error (unknown compression method) remote: fatal: failed to read object f98855fe4967cba00c17191be7a3af4af3af5a0c: Invalid argument error: unpack failed: unpack-objects abnormal exit To //192.168.0.200/設計部共用/SharedRepository/shopstyle/rwd1033 ! [remote rejected] master -> master (unpacker error) error: failed to push some refs to '//192.168.0.200/設計部共用/SharedRepository/shopstyle/rwd1033' Omar@DESKTOP-0F94SJL MINGW64 /d/Workspace/shopstyle/rwd1033 (master) https://stackoverflow.com/questions/20870895/how-to-diagnose-and-fix-git-fatal-unable-to-read-tree ``` 其他筆記:https://noootown.wordpress.com/2015/06/19/git-first-use/ --- Git refusing to merge unrelated histories The default behavior has changed since git 2.9: "git merge" used to allow merging two branches that have no common base by default, which led to a brand new history of an existing project created and then get pulled by an unsuspecting maintainer, which allowed an unnecessary parallel history merged into the existing project. The command has been taught not to allow this by default, with an escape hatch --allow-unrelated-histories option to be used in a rare event that merges histories of two projects that started their lives independently. See the git release changelog for more information. You can use --allow-unrelated-histories to force the merge to happen. https://stackoverflow.com/questions/37937984/git-refusing-to-merge-unrelated-histories https://gitbook.tw/chapters/github/fail-to-push.html ###### tags: `Git`