目錄
Git-Book
Git思維導(dǎo)圖
0 常見(jiàn)命令
1 git branch
2 git tag
3 git checkout
4 git add . 和 git add *
4.1 git rm 刪除指定文件/文件夾
4.2 .gitignore
5 reset
6 git pull 和 git push
7 fix you conflicts
8 update
9 git空間限制
10 使用 git-lfs
11 ping github.com
12 網(wǎng)頁(yè)打不開(kāi)github
13 vim
14 安裝 git-lfs
ssh-keygen -t rsa -C "youremail@example.com"
可以在用戶主目錄里找到.ssh目錄,里面有id_rsa和id_rsa.pub兩個(gè)文件,這兩個(gè)就是SSH Key的秘鑰對(duì),id_rsa是私鑰,不能泄露出去,id_rsa.pub是公鑰,可以放心地告訴任何人。
0 常見(jiàn)命令
cd 目錄
git init
git config --global user.name "username"
git config --global user.email "useremail"
git config core.ignorecase false
git remote rm origin
git remote add alias https://github.com/yourgithubname/mastername.git // 會(huì)覆蓋
git pull origin master
git add filename.text
git add dirname/*
git commit -m "add filename.text"
git push -u orgin master //以master身份提交到遠(yuǎn)程倉(cāng)庫(kù) -u命令的作用相當(dāng)于幫你記住當(dāng)前分支,下次不用寫(xiě)了,直接寫(xiě)git push都可以
//刪除文件夾下的所有 .svn 文件
find . -name ".svn" | xargs rm -Rf
//刪除文件夾下的所有 .git 文件
find . -name ".git" | xargs rm -Rf
git clone 地址 文件夾名(不寫(xiě)則默認(rèn)為遠(yuǎn)程倉(cāng)庫(kù)名)
git clone https://github.com/yourgithubname/mastername.git
git clone https://github.com/yourgithubname/mastername.git ForkDir
git status 當(dāng)前文件狀態(tài)
git diff 修改內(nèi)容
git log 修改日志
git log -- dir/* or dir/ or dir 該目錄日志
git log -- file 該文件日志
git log --pretty=oneline 日志只顯示主要內(nèi)容,一行顯示
cat 查看文件
git reflog 獲取歷史版本號(hào)
1 git branch
創(chuàng)建、合并和刪除分支非??欤膭?lì)你使用分支來(lái)完成某個(gè)短期任務(wù),合并后再刪掉,比起直接在master上工作過(guò)程更安全。
查看當(dāng)前分支
git branch
創(chuàng)建本地分支localBranch,然后切換到該分支
git branch localBranch
git checkout localBranch
git checkout 加 -b 參數(shù)表示創(chuàng)建并切換,相當(dāng)于以上兩條命令
git checkout -b localBranch
添加文件 "Readme.txt", 提交到本地localBranch分支
git add Readme.txt
git commit -m "branch localBranch test"
分支的工作完成,切換回master分支:
git checkout master
把localBranch分支的工作內(nèi)容merge合并到master分支上。
git merge命令用于合并指定分支到當(dāng)前分支。合并后,再查看Readme.txt的內(nèi)容,和dev分支的最新提交是完全一致的)。
此時(shí)合并即fast-forward(快進(jìn)模式),合并速度快。并非每次代碼合并都能實(shí)現(xiàn)fast-forward。
git merge dev
使用no-fast-forward模式將dev2合并到master分支上
git merge --no-ff -m dev2
合并完成后刪除dev分支
git branch -d dev
二 遠(yuǎn)程分支
刪除遠(yuǎn)程分支
git push origin --delete Chapater6
拉取遠(yuǎn)程分支并創(chuàng)建本地分支
git checkout -b 本地分支名x origin/遠(yuǎn)程分支名x
查看所有分支
git push origin test
git branch -a
注:remote/origin/[name]表示的是遠(yuǎn)程分支
清除本地更改
清楚所有更改
git checkout . && git clean -xdf
清除某一個(gè)文件的更改
git checkout -- file
將dev分支推送到遠(yuǎn)程
git push origin dev
Everything up-to-date
將本地分支dev關(guān)聯(lián)到遠(yuǎn)程分支dev上
git branch --set-upstream-to=origin/dev
'dev' set up to track remote branch 'muscleape' from 'origin'
查看本地分支和遠(yuǎn)程分支的映射關(guān)系
git branch -vv
dev f938a3d8e9 [origin/dev: gone] 測(cè)試test
查看遠(yuǎn)程分支
git branch -r
origin/dev
查看本地各個(gè)分支目前最新的提交
git branch -v
dev f938a3d8e9 測(cè)試test
查看遠(yuǎn)程各個(gè)分支目前最新的提交
git branch -r -v
origin/dev f938a3d8e9 測(cè)試test

2 git tag
查看所有tag
git tag
查看指定版本的tag
git tag -l '1.0.*'
顯示特定tag信息
git show 1.0.1
刪除本地tag
git tag -d v1.0.2
刪除遠(yuǎn)程的tag
git push origin --delete tag v1.0.1
新建tag,-m 注釋
git tag v1.0.3
git tag -a v1.0.3 -m 'some description'
將本地特定或所有tag推送到遠(yuǎn)程
git push origin v1.0.3
git push --tags
創(chuàng)建一個(gè)基于指定tag的分支,開(kāi)發(fā)后,切換到master merge合并
git checkout -b newBranch v1.0.4
3 git checkout
假如以某條歷史commit為基準(zhǔn)來(lái)release,但沒(méi)有對(duì)該commit添加tag。此時(shí)有如下2中方式:
1)版本回退到特定commit,然后release。不推薦,原因是會(huì)丟失開(kāi)發(fā)工作;
2)git checkout <commit id>,將本地工程切換到某條commit記錄上,具體如下。
具體方法
1. git log,甄別特定commit,拷貝commit id 如 31d4ba9b17d23c21dd2bdc39a5c2c97659fc41f0
2. git checkout <commit id> 即 git checkout 31d4ba9b17d23c21dd2bdc39a5c2c97659fc41f0
3. git tag v1.0.4 參見(jiàn)[git tag 小節(jié)]
4. git push origin v1.0.4 將本地tag提交到遠(yuǎn)程倉(cāng)庫(kù)上
5. git status 查看所有變化的文件,終端會(huì)快速顯示內(nèi)容,正常
6. git checkout master 切回master主分支
7. git pull 更新到master最新代碼
4 git add . 和 git add * 區(qū)別
- git add . 將本地所有untrack的文件都加入暫存區(qū),并且會(huì)根據(jù).gitignore做過(guò)濾
- git add * 會(huì)忽略.gitignore把任何文件都加入
4.1 git rm 刪除指定文件/文件夾
在本地倉(cāng)庫(kù)刪除指定文件
git rm 文件名名稱
在本地倉(cāng)庫(kù)刪除指定文件夾
git rm -r 文件夾/
提交修改
git commit -m '刪除文件夾'
推送到遠(yuǎn)程倉(cāng)庫(kù)
git push origin 遠(yuǎn)程倉(cāng)庫(kù)連接
git rm命令
git rm -h
用法:git rm [<選項(xiàng)>] [--] <文件>...
-n, --dry-run 演習(xí)
-q, --quiet 不列出刪除的文件
--cached 只從索引區(qū)刪除
-f, --force 忽略文件更新?tīng)顟B(tài)檢查
-r 允許遞歸刪除
--ignore-unmatch 即使沒(méi)有匹配,也以零狀態(tài)退出
4.2 .gitignore
養(yǎng)成在項(xiàng)目開(kāi)始就創(chuàng)建.gitignore文件的習(xí)慣
1、配置語(yǔ)法:
以斜杠/開(kāi)頭表示目錄;
以星號(hào)*通配多個(gè)字符;
以問(wèn)號(hào)?通配單個(gè)字符
以方括號(hào)[]包含單個(gè)字符的匹配列表;
以嘆號(hào)!表示不忽略(跟蹤)匹配到的文件或目錄;
此外,git 對(duì)于 .ignore 配置文件是按行從上到下進(jìn)行規(guī)則匹配的,意味著如果前面的規(guī)則匹配的范圍更大,則后面的規(guī)則將不會(huì)生效;
2、示例說(shuō)明
a、規(guī)則:fd1/*
說(shuō)明:忽略目錄 fd1 下的全部?jī)?nèi)容;注意,不管是根目錄下的 /fd1/ 目錄,還是某個(gè)子目錄 /child/fd1/ 目錄,都會(huì)被忽略;
b、規(guī)則:/fd1/*
說(shuō)明:忽略根目錄下的 /fd1/ 目錄的全部?jī)?nèi)容;
c、規(guī)則:
/*
!.gitignore
!/fw/bin/
!/fw/sf/
說(shuō)明:忽略全部?jī)?nèi)容,但是不忽略 .gitignore 文件、根目錄下的 /fw/bin/ 和 /fw/sf/ 目錄;
5 reset
不刪除工作空間改動(dòng)代碼,撤銷commit,并且撤銷git add,回滾到目標(biāo)狀態(tài)
git reset --mixed HEAD^
git reset HEAD^
git reset 版本號(hào)
git reset --hard HEAD^ 回到上個(gè)版本 刪除工作空間改動(dòng)代碼,撤銷commit,撤銷git add
git reset --hard HEAD^^ 回到上上個(gè)版本
git reset --hard HEAD~100 回到100個(gè)版本前
git reset --hard 版本號(hào) 回到某個(gè)特定的版本
git reset --soft HEAD^ 不刪除工作空間改動(dòng)代碼,撤銷commit,不撤銷git add
git commit --amend 若commit注釋寫(xiě)錯(cuò)了,只修改注釋,此時(shí)會(huì)進(jìn)入默認(rèn)vim,修改注釋后保存
6 pull or push 時(shí)的問(wèn)題
在git提交或更新時(shí)時(shí),Please commit your changes or stash them before you merge. 是一個(gè)常見(jiàn)的問(wèn)題。(在你merge or change master 前,提交你的改變,或者存儲(chǔ)改變。)
問(wèn)題原因:上次commit后,代碼發(fā)生了新的變化,如果merge或者change master 就可能導(dǎo)致代碼丟失。
解決方案:保留本地的修改 的改法——通過(guò)git stash
git stash
git pull origin master
git push -f // 本地強(qiáng)制覆蓋遠(yuǎn)程
git rebase master
git stash pop
git stash list
git stash clear
git stash list
7 說(shuō)明fix you conflicts
git stash: 備份當(dāng)前的工作區(qū)的內(nèi)容,從最近的一次提交中讀取相關(guān)內(nèi)容,讓工作區(qū)保證和上次提交的內(nèi)容一致。同時(shí),將當(dāng)前的工作區(qū)內(nèi)容保存到Git棧中。通過(guò)git stash將工作區(qū)恢復(fù)到上次提交的內(nèi)容,同時(shí)備份本地所做的修改,之后就可以正常git pull了,git pull完成后,執(zhí)行g(shù)it stash pop將之前本地做的修改應(yīng)用到當(dāng)前工作區(qū)。
git stash pop: 從Git棧中讀取最近一次保存的內(nèi)容,恢復(fù)工作區(qū)的相關(guān)內(nèi)容。由于可能存在多個(gè)Stash的內(nèi)容,所以用棧來(lái)管理,pop會(huì)從最近的一個(gè)stash中讀取內(nèi)容并恢復(fù)。
git stash list: 顯示Git棧內(nèi)的所有備份,可以利用這個(gè)列表來(lái)決定從那個(gè)地方恢復(fù)。
git stash clear: 清空Git棧。此時(shí)使用gitg等圖形化工具會(huì)發(fā)現(xiàn),原來(lái)stash的哪些節(jié)點(diǎn)都消失了。
8 update
git fetch --all # fetch所有分支上的內(nèi)容,也可以選擇只備份一部分內(nèi)容
git reset --hard origin/master # 重置本地分支
git pull
9 git空間限制
error: RPC failed; curl 18 transfer closed with outstanding read data remain
fatal: the remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed
git config --global http.postBuffer 1048576000
git config --global http.lowSpeedLimit 0
git config --global http.lowSpeedTime 999999
10 使用 git-lfs
git lfs track '.a' // 添加追蹤 .a或其他類型大文件
git lfs untrack '.a' // 取消追蹤 .a或其他類型大文件
git lfs track // 查看追蹤的類型文件
git add .gitattributes
git commint -m 'lfs add .gitattributes'
git push
git add youModifiedDir/* or *
git commit -m 'your modify des'
git push
11 ping github.com
在安裝git-lfs時(shí),遇到問(wèn)題,發(fā)現(xiàn)ping github.com 失敗,如下操作后可以ping通。
終端輸入以下命令,二選一,進(jìn)入本機(jī)hosts配置文件:
sudo vim /etc/hosts
sudo vim /private/etc/hosts
在hosts配置文件尾行換行添加如下兩個(gè)IP映射:
36.7.68.22 github.com git
140.82.112.3 github.global.ssl.fastly.net
sudo killall -HUP mDNSResponder // 刷新本地DNS緩存
保存并退出
注意:36.7.68.22 是本機(jī)IP地址,140.82.112.3 是github的IP地址
ipaddress查詢,默認(rèn)顯示本機(jī)的IP地址,輸入域名可查詢對(duì)應(yīng)IP地址
github的IP查詢,如下圖



12 網(wǎng)頁(yè)打不開(kāi)github
網(wǎng)頁(yè)打不開(kāi)github.可能自己以前在/etc/hosts中添加了github的IP映射,包括上述ping github.com中的兩個(gè)映射.此時(shí)進(jìn)入上述文件,刪除有關(guān)github的IP映射保存退出后,即可在網(wǎng)頁(yè)打開(kāi)github.
13 vim-windows
c 進(jìn)入編輯模式
ESC
ZZ 退出
進(jìn)入編輯模式:
小寫(xiě)i:在光標(biāo)所在行位置停止不動(dòng)開(kāi)始寫(xiě)入內(nèi)容
大寫(xiě)I:在光標(biāo)所在行行首開(kāi)始寫(xiě)入內(nèi)容
小寫(xiě)a:在光標(biāo)所在行當(dāng)前字符后開(kāi)始寫(xiě)入內(nèi)容
大寫(xiě)A:在光標(biāo)所在行行尾開(kāi)始寫(xiě)入內(nèi)容
小寫(xiě)o:在光標(biāo)所在行下一行開(kāi)始寫(xiě)入內(nèi)容
大寫(xiě)O:在光標(biāo)所在行上一行開(kāi)始寫(xiě)入內(nèi)容
退出編輯模式:
:w:保存文本
:q:退出編輯模式
:w!:強(qiáng)制保存,在root用戶下即使文本只讀也可以強(qiáng)制保存
:q!:強(qiáng)制退出,所有改動(dòng)不生效
:wq:保存并退出
14安裝 git-lfs
push 超過(guò)100M文件
: brew install git-lfs
==> Tapping homebrew/core
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...
fatal: unable to access 'https://github.com/Homebrew/homebrew-core/': SSL certificate problem: self signed certificate
Error: Failure while executing; git clone https://github.com/Homebrew/homebrew-core /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core exited with 128.
Error: Failure while executing; /usr/local/bin/brew tap homebrew/core exited with 1.
:
: git clone git://mirrors.ustc.edu.cn/homebrew-core.git/ /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core --depth=1
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...
remote: Enumerating objects: 5264, done.
remote: Counting objects: 100% (5264/5264), done.
remote: Compressing objects: 100% (5058/5058), done.
remote: Total 5264 (delta 44), reused 980 (delta 3)
Receiving objects: 100% (5264/5264), 4.30 MiB | 2.68 MiB/s, done.
Resolving deltas: 100% (44/44), done.
Updating files: 100% (5283/5283), done.
: cd "(brew --repo)/Library/Taps/homebrew/homebrew-core"
: git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
:
: brew update
Already up-to-date.
:
: brew install git-lfs
==> Downloading https://homebrew.bintray.com/bottles/git-lfs-2.11.0.catalina.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/35/351d2d6cb168249b9b7b0d55628574126b9787eab1441861dfd324b952057651?__gd
######################################################################## 100.0%
==> Pouring git-lfs-2.11.0.catalina.bottle.tar.gz
==> Caveats
Update your git config to finish installation:
Update global git config
$ git lfs install
Update system git config
$ git lfs install --system
==> Summary
?? /usr/local/Cellar/git-lfs/2.11.0: 65 files, 12.7MB
: git lfs
git-lfs/2.11.0 (GitHub; darwin amd64; go 1.14.2)
:
: git lfd install --system
git: 'lfd' is not a git command. See 'git --help'.
SSL certificate problem: self signed certificate
fatal: unable to access 'https://.../.git': SSL certificate problem: self signed certificate
git config --global http.sslVerify false
Git Large File Storage 上傳幫助
https://stackoverflow.com/questions/48734119/git-lfs-is-not-a-git-command-unclear