Git从入门到熟悉
【原理】
1)Git介紹
2)Git和SVN的區(qū)別、SVN工作流程、架構(gòu)圖講解
3)Git優(yōu)點、架構(gòu)圖、文件三種狀態(tài)、三個工作區(qū)域、工作流程講解
【實戰(zhàn)】
1)準備工作
2)創(chuàng)建版本庫
3)版本回退
4)工作區(qū)與暫存區(qū)的區(qū)別
5)刪除文件和恢復(fù)
6)遠程倉庫
7)遠程克隆到本地
8)GitHub刪除項目
9)分支的創(chuàng)建與合并
10)分支合并沖突解決
11)查看歷史合并功能
12)分支的注意事項
13)雙分支操作
14)強制刪除分支
15)多人協(xié)作
16)創(chuàng)建工作目錄的兩個方法
17)忽略需要push的文件
18)更新本地工作區(qū)
19)CentOS下搭建Git服務(wù)器
20)鉤子腳本
【拓展知識】
1)別名配置
2)標簽操作
【Git常用命令總結(jié)】
前言:本人技術(shù)有限,如有說得不對或做的不對的地方,請大家提出來,謝謝大家
一、
Git介紹:
Git:是可以提供版本控制的一個工具
Github:提供了你用git這個工具的一個平臺,公共代碼倉庫
git服務(wù)器:自建倉庫,私有倉庫(企業(yè)用)
二、
Git和SVN的區(qū)別
Git是分布式版本控制系統(tǒng),SVN是集中式版本控制系統(tǒng)
SVN的工作流程?
SVN有一臺中央服務(wù)器,下面有一臺至多臺的客戶端,完全依賴于中央服務(wù)器工作
SVN工作流程:
客戶端通過把內(nèi)容Check out到本地,然后提交到中央服務(wù)器,后面再更新,然后再提交
弊端:1、如果中央服務(wù)器斷網(wǎng)了,則會導致無法更新
? ? 2、如果網(wǎng)絡(luò)不好,則會導致更新緩慢
? ? 3、如果中央服務(wù)器單點故障,又沒及時更新的話,則會導致數(shù)據(jù)丟失
架構(gòu)圖:
三、
Git優(yōu)點:
1、Git沒有中央管理器,每個人的電腦就是一臺完整的版本庫
2、工作不需要聯(lián)網(wǎng),因為版本都在自已的電腦上
3、如果多個人共同協(xié)作,只需要push到github倉庫上即可進行多人協(xié)作
4、項目提交到GitHub,每一次提到到本地操作,都是一次對代碼倉庫的完整備份
架構(gòu)圖:
上圖Git服務(wù)器是企業(yè)內(nèi)部自已搭的私有的,如果是自已玩的,我們自已的電腦就是自已的倉庫,我們不需要聯(lián)網(wǎng),然后有一個GitHub是公共倉庫,我們還可以把自已的項目提交到公共倉庫,相當于對自已代碼又做了一個備份,而企業(yè)內(nèi)部,則需要自建一臺私有的Git服務(wù)器,大家通過先把代碼提交到自已電腦的倉庫,然后再提交到Git服務(wù)器這樣的一個工作流程
Git文件三種狀態(tài)
已提交(committed)
表示該文件已經(jīng)被安全地保存在本地數(shù)據(jù)庫中了
已修改(modified)
表示修改了某文件,但還沒有提交保存
已暫存(staged)
表示把已修改的文件放在下次提交時要保存的清單中
Git文件流轉(zhuǎn)時的三個工作區(qū)域:
Git的工作目錄:
保存著特定的版本文件,屬于提交狀態(tài)
暫存區(qū)域:
做了修改并已放入暫存區(qū)域
本地倉庫:
顧名思義,本的版本倉庫
Git工作流程:
1、在工作目錄中修改某些文件
2、對修改后的文件進行快照,然后保存到暫存區(qū)域
3、提交更新,將保存在暫存區(qū)域的文件快照永久轉(zhuǎn)儲到Git目錄中
【實戰(zhàn)】
一、Git準備工作
1)cygwin和git bash的對比
由于我們在Windows上執(zhí)行g(shù)it的命令需要一個仿Unix終端的軟件,在這里,我給大家介紹兩款軟件,一款是cygwin,一款是git bash,git bash是git提供給你用的軟件,那么這兩款軟件有啥不一樣呢,請看圖:
1、左邊那款是git bash,他能夠把你當前用戶家目錄所有文件包括windows的都列出來,而右邊的cygwin只列出他用戶家目錄的文件,并沒有列出windows的文件
總結(jié)第一點:cygwin簡潔性強,而bit bash把windows家目錄的東西都列出來,讓你也可以進行操作,至于你覺得哪個好,這個看你個人喜好
再來看第二張圖
2、左邊那款還是git bash,右邊那款則是cygwin,兩者都在f盤下執(zhí)行了ls,看到兩者不什么不一樣了嗎,git bash能把文件夾以高這度藍色顯示,而cygwin不管你是什么文件都是白色,這個倒是讓我們不好區(qū)分
總結(jié)第二點:覺得這個高這度顯示各種不一樣的文件這個優(yōu)越性git bash是做的比較好的
我的選擇:git bash
為什么 ?
因為git bash不緊可以高亮度顯示各種不一樣的文件,讓你可以快速操作,而且在家目錄下他也可以列出你的所有windows下的文件,提供給你操作,我覺得選擇git bash還是比選擇cygwin好的,不過這個最終還是看你自已個人選擇,我在這里只是說出我自已的想法而已
2)下載安裝
git下載鏈接:https://git-scm.com/download/win
我的安裝是默認一路向北
cygwin下載鏈接:https://cygwin.com/install.html
安裝教程:http://jingyan.baidu.com/article/6b97984d83dfe51ca2b0bf0e.html
3)桌面右健鼠標點擊Git Bash Here打開Git Bash,界面如下:
它是在Windows下模仿Shell命令行的一個終端
4)查看幫助
Administrator@YYBFVJDMAPF13NB?MINGW64?~/Desktop $?git?help?git?????????#git?help?<verb>Administrator@YYBFVJDMAPF13NB?MINGW64?~/Desktop $?git?config?--help????#git?<verb>?--help5)設(shè)置身份
因為git是分布式版本控制系統(tǒng),所以需要填寫用戶名和郵箱做為一個標識
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?config?--global?user.name?'wsyht'Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?config?--global?user.email?t89191958201@139.com注意:git config 下有三個參數(shù),
--global: 表示對這臺機器上的所有Git倉庫都會使用這個配置
--system:用戶級別配置,針對某個用戶生效
--local:針對本地有效
6)設(shè)置比較工具(可選)
Administrator@YYBFVJDMAPF13NB?MINGW64?~/Desktop $??git?config?--global?merge.tool?vimdiff7)檢查配置
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?config?--list core.symlinks=false core.autocrlf=true core.fscache=true color.diff=auto color.status=auto color.branch=auto color.interactive=true help.format=html http.sslcainfo=C:/Program?Files/Git/mingw64/ssl/certs/ca-bundle.crt diff.astextplain.textconv=astextplain rebase.autosquash=true merge.tool=vimdiff user.name=wsyht user.email=t89191958201@139.com core.repositoryformatversion=0 core.filemode=false core.bare=false core.logallrefupdates=true core.symlinks=false core.ignorecase=true二、創(chuàng)建版本庫
1)(關(guān)健字:git init)
版本庫:又名倉庫,你可以簡單的理解成一個目錄,這個目錄里面的所有文件都可以被Git管理起來,包括修改、刪除、提交、回滾
Administrator@YYBFVJDMAPF13NB?MINGW64?~/Desktop $?cd?f:?Administrator@YYBFVJDMAPF13NB?MINGW64?/f $?mkdir?demo1Administrator@YYBFVJDMAPF13NB?MINGW64?/f $?cd?demo1/Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1 $?git?init Initialized?empty?Git?repository?in?F:/demo1/.git/Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $這個時候會多了一個.git的隱藏目錄,他是用來管理跟蹤管理版本的,里面文件不可以修改
2)提交任務(wù)(關(guān)健字:git add,git commit)
3)查看狀態(tài)(關(guān)健字:git status)
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?status On?branch?master nothing?to?commit,?working?tree?cleanAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $4)修改裝態(tài)查看?(關(guān)健字:git status,modify)
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?ls demo1.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?echo?"1111"?>>?demo1.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?status On?branch?master Changes?not?staged?for?commit:(use?"git?add?<file>..."?to?update?what?will?be?committed)(use?"git?checkout?--?<file>..."?to?discard?changes?in?working?directory)modified:???demo1.txt????#修改裝態(tài) no?changes?added?to?commit?(use?"git?add"?and/or?"git?commit?-a") Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master)5)對比查看?(關(guān)健字:git diff)
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?diff?demo1.txt diff?--git?a/demo1.txt?b/demo1.txt index?a5bce3f..979b2de?100644 ---?a/demo1.txt +++?b/demo1.txt @@?-1?+1,2?@@test1 +1111???????#可看出多了這一行 warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $注意:提交前須查看一下文件修改的內(nèi)容,如不問題,再git add,然后git commit
6)查看提交歷史記錄(關(guān)健字:git log)
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?log commit?8445579cbd259610f94a6c665fc841313a131516???#每次提交的版本號 Author:?wsyht?<t89191958201@139.com> Date:???Thu?Jul?28?23:15:40?2016?+0800文件增加2222一行???????????????????????????????#增加內(nèi)容顯示的注釋,最近的一次提交,從上到下排序 commit?25e4895bd19c0833fa75d4b71ec8d621f3cbe2e7 Author:?wsyht?<t89191958201@139.com> Date:???Thu?Jul?28?23:15:08?2016?+0800增加1111一行 commit?a7e593c34a10f0b2e732153c09bce74381e7de35 Author:?wsyht?<t89191958201@139.com> Date:???Thu?Jul?28?22:56:25?2016?+0800demo1提交Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $注意:剛才是我自已做實驗一共提交了三次,并未貼出來到博客而已,所以出現(xiàn)了三次提交的歷史記錄
7)省略部分信息 (關(guān)健字:--pretty=oneline)
三、版本回退
1)回退命令
? ?1、回退到上一個版本
????? git reset --hard HEAD^
? ?2、回退到上上一個版本
??????git reset --hard HEAD^^
? ?3、回退到前100個版本
???? ?git reset --hard HEAD~100
演示:
回退到上一個版本
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?cat?demo1.txt test1 1111 2222Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?reset?--hard?HEAD^ HEAD?is?now?at?25e4895?增加1111一行Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?cat?demo1.txt test1 1111Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?log commit?25e4895bd19c0833fa75d4b71ec8d621f3cbe2e7 Author:?wsyht?<t89191958201@139.com> Date:???Thu?Jul?28?23:15:08?2016?+0800增加1111一行 commit?a7e593c34a10f0b2e732153c09bce74381e7de35 Author:?wsyht?<t89191958201@139.com> Date:???Thu?Jul?28?22:56:25?2016?+0800demo1提交Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $2)回退到剛才的最新版本
(1)首先獲取版本號 (關(guān)健字:git reflog)
(2)恢復(fù)版本(關(guān)健字:git reset --hard 版本號)
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?reset?--hard?8445579 HEAD?is?now?at?8445579?文件增加2222一行Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $(3)查看恢復(fù)
$?cat?demo1.txt test1 1111 2222Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?log commit?8445579cbd259610f94a6c665fc841313a131516 Author:?wsyht?<t89191958201@139.com> Date:???Thu?Jul?28?23:15:40?2016?+0800文件增加2222一行 commit?25e4895bd19c0833fa75d4b71ec8d621f3cbe2e7 Author:?wsyht?<t89191958201@139.com> Date:???Thu?Jul?28?23:15:08?2016?+0800增加1111一行 commit?a7e593c34a10f0b2e732153c09bce74381e7de35 Author:?wsyht?<t89191958201@139.com> Date:???Thu?Jul?28?22:56:25?2016?+0800demo1提交Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $四、工作區(qū)與暫存區(qū)的區(qū)別
工作區(qū):就是在我創(chuàng)建的demo1的版本庫目錄里的文件,.git目錄除外
版本庫:.git目錄就是版本庫,版本庫存著很多東西,其中最重要的就是stage(暫存區(qū)),還有Git為我們自動創(chuàng)建了第一個分支Master,以及指向Master的第一個指針HEAD,
提交Git文件到版本庫有兩步;
第一步:是使有tgit add把文件添加進去,實際上就是把文件添加到暫存區(qū)
第二步:使用git commit提交更改,實際上就是把暫存區(qū)的所有內(nèi)容提交到當前分支上
演示:
(1)修改文件和創(chuàng)建新文件查看
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?echo?"3333"?>>?demo1.txt?Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?echo?"1111"?>>?demo2.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?status On?branch?master Changes?not?staged?for?commit:(use?"git?add?<file>..."?to?update?what?will?be?committed)(use?"git?checkout?--?<file>..."?to?discard?changes?in?working?directory)modified:???demo1.txt Untracked?files:(use?"git?add?<file>..."?to?include?in?what?will?be?committed)demo2.txt no?changes?added?to?commit?(use?"git?add"?and/or?"git?commit?-a")Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $(2)添加到暫存區(qū)查看
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?add?demo1.txt?demo2.txt warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. warning:?LF?will?be?replaced?by?CRLF?in?demo2.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?status warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. On?branch?master Changes?to?be?committed:(use?"git?reset?HEAD?<file>..."?to?unstage)modified:???demo1.txtnew?file:???demo2.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $(3)提交再查看
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?commit?-m"4444"?demo1.txt warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. [master?warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. 85ce2b6]?4444 warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.1?file?changed,?1?insertion(+)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?commit?-m"1111"?demo2.txt warning:?LF?will?be?replaced?by?CRLF?in?demo2.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. [master?fc33a37]?1111 warning:?LF?will?be?replaced?by?CRLF?in?demo2.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.1?file?changed,?1?insertion(+)create?mode?100644?demo2.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?status On?branch?master nothing?to?commit,?working?tree?cleanAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $(4)一次性提交所有文件方法
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?echo?"5555"?>>?demo1.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?echo?"5555"?>>?demo2.txtc Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master)$?git?add?demo1.txt?demo2.txt warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. warning:?LF?will?be?replaced?by?CRLF?in?demo2.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?commit?-m?"一次性提交所有文件" [master?warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. warning:?LF?will?be?replaced?by?CRLF?in?demo2.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. 7ecdd08]?一次性提交所有文件 warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. warning:?LF?will?be?replaced?by?CRLF?in?demo2.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.2?files?changed,?2?insertions(+)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $四、撤銷修改
如果在一行里面添加了新內(nèi)容,現(xiàn)在要撤消
總結(jié)一下一共有三種方法:
第一種:把要刪除的內(nèi)容添刪掉,然后add,commit
第二種:恢復(fù)上一個版本,git reset --hard HEAD^
第三種:如下 (關(guān)健字:git checkout -- 文件名)-- 很重要,請注意,這里說的是兩個'-',如果沒有 -- 的話;那么命令變成切換分支了 或者用 git checkout . 推薦使用這個,可以批量還原修改的文件,前提是未add和commit,但是不包括新增的文件,只是針對已經(jīng)存在文件且修改了內(nèi)容的文件,進行撤消
$?cat?demo1.txt test1 1111 2222 3333 5555 Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?echo?"6666"?>>?demo1.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?status On?branch?master Changes?not?staged?for?commit:(use?"git?add?<file>..."?to?update?what?will?be?committed)(use?"git?checkout?--?<file>..."?to?discard?changes?in?working?directory)modified:???demo1.txt no?changes?added?to?commit?(use?"git?add"?and/or?"git?commit?-a")Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?checkout?--?demo1.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?cat?demo1.txt test1 1111 2222 3333 5555如果,已經(jīng)add添加到暫存區(qū)的,那么撤銷方法如下
接著再如下圖操作,
如果新添加的文件,但是還沒有add,現(xiàn)在我們不想用這個新文件了,那么我們直接刪除就行了,如下:
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?echo?"111"?>>?wsyht.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?status On?branch?master Untracked?files:(use?"git?add?<file>..."?to?include?in?what?will?be?committed)wsyht.txt nothing?added?to?commit?but?untracked?files?present?(use?"git?add"?to?track)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?ls demo1.txt??oo.txt??wsyht.txt??ye.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?rm?-rf?wsyht.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?status On?branch?master nothing?to?commit,?working?tree?cleanAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $五、刪除文件和恢復(fù)
(1)刪除演示
$?git?status On?branch?master nothing?to?commit,?working?tree?cleanAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?ls demo1.txt??demo2.txt??test1.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?rm?-rf?test1.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?status On?branch?master Changes?not?staged?for?commit:(use?"git?add/rm?<file>..."?to?update?what?will?be?committed)(use?"git?checkout?--?<file>..."?to?discard?changes?in?working?directory)deleted:????test1.txt no?changes?added?to?commit?(use?"git?add"?and/or?"git?commit?-a")Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?add?test1.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?commit?-m?'delete?test1.txt' [master?43185ac]?delete?test1.txt1?file?changed,?1?deletion(-)delete?mode?100644?test1.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?status On?branch?master nothing?to?commit,?working?tree?cleanAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $(2)恢復(fù)演示
$?ls demo1.txt??demo2.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?rm?-rf?demo2.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?status On?branch?master Changes?not?staged?for?commit:(use?"git?add/rm?<file>..."?to?update?what?will?be?committed)(use?"git?checkout?--?<file>..."?to?discard?changes?in?working?directory)deleted:????demo2.txt no?changes?added?to?commit?(use?"git?add"?and/or?"git?commit?-a")Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?checkout?--?demo2.txt Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?ls demo1.txt??demo2.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?status On?branch?master nothing?to?commit,?working?tree?cleanAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $總結(jié):
三種情況
1、未add想轍消(包括增刪改文件內(nèi)容,也包括刪文件)
? ?git checkout -- 文件名??(轍消工作區(qū)的修改)? ? 或
? ?git checkout .
2、add到暫存區(qū)后,需分兩步操作
? ?第一步:git reset HEAD 文件名??(轍消暫存區(qū)的修改)
3、commit之后
? ?git reset --hard HEAD^ ? ? ?#版本回退1個版本
? ?git reset --hard HEAD^^ ? ? #版本回退2個版本 ??
? ?git reset --hard HEAD~100 ? ?#回退到前100個版本
???? ?git reflog ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#獲取版本號
? ? ??git reset --hard 版本號 ? ? ? ? ? ? ? ?#恢復(fù)版本號
? ? ?
六、遠程倉庫(GitHub公共倉庫)
1)先注冊github賬號
省略...
2)生成ssh-keygen密鑰對
Administrator@YYBFVJDMAPF13NB?MINGW64?~ $?cdAdministrator@YYBFVJDMAPF13NB?MINGW64?~ $?pwd /c/Users/AdministratorAdministrator@YYBFVJDMAPF13NB?MINGW64?~ $?ssh-keygen?-C?'wsyht'???#生成密鑰對,-C?就是把引號內(nèi)容給id_rsa.pub公鑰做注釋 Generating?public/private?rsa?key?pair. Enter?file?in?which?to?save?the?key?(/c/Users/Administrator/.ssh/id_rsa): Created?directory?'/c/Users/Administrator/.ssh'. Enter?passphrase?(empty?for?no?passphrase): Enter?same?passphrase?again: Your?identification?has?been?saved?in?/c/Users/Administrator/.ssh/id_rsa. Your?public?key?has?been?saved?in?/c/Users/Administrator/.ssh/id_rsa.pub. The?key?fingerprint?is: SHA256:oDD45Fhl3zdLv6EwJivEAHTGRyiyxifnLfYv0mDE7m0?wsyht The?key's?randomart?p_w_picpath?is: +---[RSA?2048]----+ |o..o=.???????????| |.+o=?o?.?????????| |+.O?.?o?.?+??????| |.X?X?.?.?o?+?????| |o?X?=?.?S?.?o????| |???O?.?+?o?.?o???| |??+?B?.???.?.????| |???o?E???????????| |????o?o.?????????| +----[SHA256]-----+Administrator@YYBFVJDMAPF13NB?MINGW64?~ $3、在GitHub上創(chuàng)建項目
4、創(chuàng)建展示
5、完成展示
在如上截圖顯示內(nèi)容我要說一下了
通過本地倉庫和GitHub倉庫的連接方式有兩種:
第一種:通過HTTPS方式連接,就是如上紅框顯示,這種方式在本地遠程GitHub倉庫,需要用輸入你的GitHub賬號和密碼,而且每次都要輸入 ?缺點:不安全,麻煩
第二種:通過ssh方式連接,在本地創(chuàng)建密鑰對, 再把公鑰上傳到GitHub服務(wù)器,然后實現(xiàn)無密碼連接如下截圖這是通過ssh的連接方式: ? ? 優(yōu)點:安全,方便
先說第一種連接方式:
這種方式是通過第一種https連接方式,先進入到我的工作目錄下,也就是創(chuàng)建的版本庫目錄下,
然后輸入遠程命令,用戶名和密碼即可
第二種方式展示
上面已經(jīng)生成了密鑰對了,然后我們把id_rsa.pub的公鑰復(fù)制到GitHub上去
本地測試
修改配置文件,進到版本庫目錄.git下,修改config文件 Administrator@YYBFVJDMAPF13NB?MINGW64?~/.ssh $?cd?f:Administrator@YYBFVJDMAPF13NB?MINGW64?/f $?cd?demo1/.git/Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/.git?(GIT_DIR!) $?ls COMMIT_EDITMSG??description??hooks/??info/??objects/???refs/ config??????????HEAD?????????index???logs/??ORIG_HEAD Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/.git?(GIT_DIR!)$vim config
如上圖所示,原本默認是https的連接方式,現(xiàn)在改成通過ssh方式去連接,只需要把giuhub剛創(chuàng)建項目時他顯示的連接命令考到config文件url后面即可
測試連接:
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?ssh?-T?git@github.com The?authenticity?of?host?'github.com?(192.30.253.113)'?can't?be?established. RSA?key?fingerprint?is?SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8. Are?you?sure?you?want?to?continue?connecting?(yes/no)??yes Warning:?Permanently?added?'github.com,192.30.253.113'?(RSA)?to?the?list?of?known?hosts. Hi?wsyht!?You've?successfully?authenticated,?but?GitHub?does?not?provide?shell?access.Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $如圖所示,看到successfully表示成功了
現(xiàn)在我們再更改一個文件,然后再Push到github
先提交到本地
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?echo?"888"?>>?demo1.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?add?demo1.txt warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?commit?-m?'888' [master?warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. d693375]?888 warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.1?file?changed,?1?insertion(+)現(xiàn)在我們push到github了
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?remote?add?origin?git@github.com:wsyht/demo_test.git fatal:?remote?origin?already?exists.Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?push?-u?origin?master fatal:?remote?error:wsyht/demo/demo_test?is?not?a?valid?repository?nameEmail?support@github.com?for?help如果報如上錯,解決辦法如下:
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?remote?rm?origin??#刪除遠程默認倉庫名"origin" #刪除遠程庫名后,則會把.git/config文件的url也刪除,所以到下面又要重新添加Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?remote?add?origin?git@github.com:wsyht/demo_test.git?#config文件重新添加urlAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?push?-u?origin?master???#第一次push需要加-u參數(shù) Counting?objects:?3,?done. Delta?compression?using?up?to?4?threads. Compressing?objects:?100%?(2/2),?done. Writing?objects:?100%?(3/3),?281?bytes?|?0?bytes/s,?done. Total?3?(delta?0),?reused?0?(delta?0) To?git@github.com:wsyht/demo_test.git43185ac..07e6d4c??master?->?master Branch?master?set?up?to?track?remote?branch?master?from?origin.成功了,哈哈哈。。。
.config文件展示
剛剛上面執(zhí)行g(shù)it remote rm origin就是把url那一行刪了
接著又執(zhí)行了git remote add origin git@github.com:wsyht/demo_test.git
相當于重新加載了那一行,所以又能push上了,push即是推送文件上去github公共倉庫里
wsyht是我建注冊的github的用戶名,demo_test是我在GitHub上建的項目名,git是用戶名,github.com是github網(wǎng)站的域名,origin是遠程倉庫默認的庫名
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/.git?(GIT_DIR!) $?cat?config [core]repositoryformatversion?=?0filemode?=?falsebare?=?falselogallrefupdates?=?truesymlinks?=?falseignorecase?=?true [branch?"master"] [branch?"master"] [branch?"wsyht"] #[alias]#br?=?branch [remote?"origin"]url?=?git@github.com:wsyht/demo_test.gitfetch?=?+refs/heads/*:refs/remotes/origin/*再查看github
OK,已經(jīng)提交上去了
現(xiàn)在我們再來看一下config這個配置文件
Administrator@YYBFVJDMAPF13NB MINGW64 /f/demo1/.git (GIT_DIR!)
$ cat config
[core]
? ? ? ? repositoryformatversion = 0
? ? ? ? filemode = false
? ? ? ? bare = false
? ? ? ? logallrefupdates = true
? ? ? ? symlinks = false
? ? ? ? ignorecase = true
[branch "master"]
[remote "origin"]
? ? ? ? url = git@github.com:wsyht/demo_test.git ? #這一行是講你所用的連接方式
? ? ? ? fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
? ? ? ? remote = origin ? ? ? ? ? ? ?
? ? ? ? merge = refs/heads/master ? ? #合并分支是master,到后面我再講什么是分支
注意:在第一次我們推送master分支時,加上了-u參數(shù),git會本地的Master分支推送到github的master分支,并且會關(guān)聯(lián)起來,所以的就不需要再加入-u參數(shù)了
七、遠程庫克隆到本地
點擊進去
刪除提交后,現(xiàn)在我們在把GitHub克隆到本地,你會發(fā)現(xiàn)少了一個文件
本地操作:關(guān)健字: git clone
可以看到多了一個目錄出來
八、GitHub刪除項目
九、分支的創(chuàng)建與合并
首先我們來講一下什么是分支,分支就相當于我現(xiàn)在在做一個項目A,然后項目里面有一個功能的BUG我需要去做修改,然后為了安全,不影響我的Master分支,我就在我的Master分支上再創(chuàng)建一個分支出來,然后我就在我這個分支上修改我的代碼,修改完成后,再與我的Master主分支合并,這樣就不會影響到我的原文件,分支其實就是基于某一時刻的代碼進行修改,這個分支實際上只存儲這些修改,也可以說是項目某個時期的快照,但他不是真實賦值文件,修改完成后,合并到Master分支,這樣,Master分支的內(nèi)容從而也會跟著發(fā)生改變,而創(chuàng)建的臨時文件,則怎么修改也不會影響我的源文件,即Master分支的內(nèi)容,相當于你在虛擬機的一個測試環(huán)境,你愛昨玩昨玩,都不會影響到我的真實系統(tǒng)
1)創(chuàng)建分支wsyht 關(guān)健字:git checkout -b 分支名字 ,git branch
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(master) $?git?checkout?-b?wsyht?????????#創(chuàng)建并切換分支 Switched?to?a?new?branch?'wsyht'Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(wsyht) $?git?branch????#查看當前所在分支master *?wsyht???Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(wsyht) $git branch wsyht ? #創(chuàng)建分支
git checkout wsyht #切換分支
在wsyht分支上進行修改內(nèi)容提交操作?
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(wsyht) $?echo?"999"?>>?demo1.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(wsyht) $?git?add?demo1.txt warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(wsyht)$?git?commit?-m'wsyht999' [wsyht?warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. c4ff9ea]?wsyht999 warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.1?file?changed,?1?insertion(+)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(wsyht) $?git?checkout?master Switched?to?branch?'master' Your?branch?is?up-to-date?with?'origin/master'.Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(master) $?cat?demo1.txt test1 1111 2222 3333 5555 777 888 Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(master) $發(fā)現(xiàn)在wsyht分支上修改提交,再切換回master分支,并不影響master分支文件的內(nèi)容
現(xiàn)在合并分支
把Master分支和wsyht分支內(nèi)容合并在一起,只留Master分支,再看Master分支的內(nèi)容,關(guān)健字:git merge wsyht
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(master) $?git?branch *?masterwsyhtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(master) $?git?merge?wsyht Updating?3020d4c..c4ff9ea Fast-forwarddemo1.txt?|?1?+1?file?changed,?1?insertion(+)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(master) $?cat?demo1.txt test1 1111 2222 3333 5555 777 888 999Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(master) $Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(master) $??git?branch?-d?wsyht Deleted?branch?hb?(was?5939679).可查看到,在wsyht分支上修改的內(nèi)容,合并后,在Master分支上已顯示出來
注意:在創(chuàng)建的臨時分支上修改了文件內(nèi)容后,此時主分區(qū)不能進行合并,一定要add,commit了然后再在主分區(qū)上進行合并操作,合并后,要刪除臨時分支,否則分支了多會凌亂,合并后可以通過git log -1查看最新合并的日志,合并后無需再做任何操作,直接push到git服務(wù)器或GitHub即可
命令總結(jié)
查看分支:git branch
創(chuàng)建分支:git branch 分支名字
切換分支: git checkout 分支名字
創(chuàng)建切換分支: git checkout -b 分支名字
合并某分支到當前分支: git merge 分支名字
刪除分支: git branch -d 分支名字
十、分支合并沖突解決
$?git?branch *?masterAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(master) $?git?checkout?-b?hb Switched?to?a?new?branch?'hb'Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(hb) $?cat?demo1.txt 111Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(hb) $?echo?"333"?>>?demo1.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(hb) $?git?add?demo1.txt warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(hb) $?git?commit?-m"hb3333" [hb?warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. e224bca]?hb3333 warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.1?file?changed,?1?insertion(+)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(hb) $?git?checkout?master Switched?to?branch?'master' Your?branch?is?ahead?of?'origin/master'?by?4?commits.(use?"git?push"?to?publish?your?local?commits)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(master) $?cat?demo1.txt 111Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(master) $?echo?"222"?>>?demo1.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(master) $?git?add?demo1.txt warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(master) $?git?commit?-m?'master?commit' [master?warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. f60973f]?master?commit warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.1?file?changed,?1?insertion(+)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(master) $?git?merge?hb Auto-merging?demo1.txt CONFLICT?(content):?Merge?conflict?in?demo1.txt Automatic?merge?failed;?fix?conflicts?and?then?commit?the?result.?Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(master|MERGING) $?cat?demo1.txt 111 <<<<<<<?HEAD 222???#master分支的內(nèi)容 ======= 333???#hb分支的內(nèi)容 >>>>>>>?hb???Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(master|MERGING) $#修改成主分支修改的內(nèi)容,刪了hb分支的內(nèi)容再提交就正常了
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(master|MERGING) $?cat?demo1.txt 111 222Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(master|MERGING) $?git?add?demo1.txt Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(master|MERGING)$?git?commit?-m'new' [master?b2a4ccb]?new Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(master)$?cat?demo1.txt 111 222十一、查看歷史合并功能
在合并分支時,默認會用fast forware模式,這樣的話在我們刪除分支后,會刪掉我們曾經(jīng)合并過的信息,此時,你再用git log就查看不了我們跟哪些分支做過合并了,所以在合并的時候我們要加上一個參數(shù),--no-ff
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(master) $?git?checkout?-b?wsyht Switched?to?a?new?branch?'wsyht'Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(wsyht) $?cat?demo1.txt 111 222Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(wsyht) $?echo?"333"?>>?demo1.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(wsyht) $?git?add?demo1.txt warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. gAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(wsyht) $?git?commit?-m?"add?333" [wsyht?warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. 34d979f]?add?333 warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.1?file?changed,?1?insertion(+) Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(wsyht)$?git?checkout?master Switched?to?branch?'master' Your?branch?is?ahead?of?'origin/master'?by?7?commits.(use?"git?push"?to?publish?your?local?commits) Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(master)$?git?merge?--no-ff?-m?'合并wsyht測試'?wsyht Merge?made?by?the?'recursive'?strategy.demo1.txt?|?1?+1?file?changed,?1?insertion(+)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(master) $?git?log?--graph?--pretty=oneline?--abbrev-commit????#--graph以圖形化顯示如下圖最左上角,--abbrev-commit則是縮短版本號簡短顯示 *???9e70f26?合并wsyht測試????#可以查看到wsyht的合并信息 |\ |?*?34d979f?add?333 |/ *???b2a4ccb?new |\ |?*?e224bca?hb3333 *?|?f60973f?master?commit |/ *?ce302ae?delete?oo.txt *?5939679?111 *?c831711?qk *?c4ff9ea?wsyht999 *?3020d4c?oo *?d497da7?Delete?demo2.txt *?d693375?888 *?07e6d4c?777 *?43185ac?delete?test1.txt *?7ecdd08?一次性提交所有文件 *?fc33a37?1111 *?85ce2b6?4444 *?8445579?文件增加2222一行 *?25e4895?增加1111一行 *?a7e593c?demo1提交Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/demo_test?(master) $十一、分支的注意事項
第一個要注意的地方:如下可以看到git切換到wsyht分支,但是wsyht分支沒有add和commit,所以再切回master,他就相當于直接改了Master的文件,所以修改了一定要,add再commit后再切回Master
第二個要注意的地方:當你在其他分支上修改了文件提交后,合并到Master后,一定要刪了開出來的分支,然后再開新的分支用,畢免二次污染
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?cat?demo1.txt 1111 1111 2222 3333 5555 777 888Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?checkout?-b?wsyht Switched?to?a?new?branch?'wsyht'Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?cat?demo1.txt 1111 1111 2222 3333 5555 777 888Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?sed?-i?'1s/1111/wsyht/g'?demo1.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?cat?demo1.txt wsyht 1111 2222 3333 5555 777 888Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?status On?branch?wsyht Changes?not?staged?for?commit:(use?"git?add?<file>..."?to?update?what?will?be?committed)(use?"git?checkout?--?<file>..."?to?discard?changes?in?working?directory)modified:???demo1.txt no?changes?added?to?commit?(use?"git?add"?and/or?"git?commit?-a")Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?checkout?master M???????demo1.txt Switched?to?branch?'master' Your?branch?is?ahead?of?'origin/master'?by?1?commit.(use?"git?push"?to?publish?your?local?commits)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?ls demo1.txt??demo2.txt cAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?cat?demo1.txt wsyht 1111 2222 3333 5555 777 888Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?add?demo1.txt warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?commit?-m?'wsyht?commint' [master?warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. 3a59897]?wsyht?commint warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.1?file?changed,?1?insertion(+),?1?deletion(-)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?ls demo1.txt??demo2.txt cAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?cat?demo1.txt wsyht 1111 2222 3333 5555 777 888Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?checkout?wsyht Switched?to?branch?'wsyht'Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?cat?demo1.txt 1111 1111 2222 3333 5555 777 888十二、雙分支操作
我在這里講一句:實踐出真知,有時候想一些東西,還不如親自動手操作明白來得快,有些地方你想來想去不明白,但你動手操作了,你就明白他是怎么一回事了,所以不要光看不練,相信我,下面對于屬于開發(fā)比較復(fù)雜的項目的開發(fā)來說很重 ,請大家認真看
#這里是指使用第一個分支開發(fā),但是還沒開發(fā)完新功能,所以并未能add提交,這個時候上頭又說要馬止改一個很重要的bug,要快速完成,所以我們在把這個分支儲藏起來,下次回來再拿出來繼續(xù)開發(fā)
第一步:
#第一個wsyht分支操作 ??
wsyht分支開發(fā)->儲藏-暫停
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?branch *?masterAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?checkout?-b?wsyht Switched?to?a?new?branch?'wsyht'Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?ls demo1.txt??demo2.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?cat?demo1.txt 1111 1111 2222 3333 5555 777 888Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?sed?-i?'1s/1111/wsyht/g'?demo1.txt Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?cat?demo1.txt wsyht 1111 2222 3333 5555 777 888Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?status On?branch?wsyht Changes?not?staged?for?commit:(use?"git?add?<file>..."?to?update?what?will?be?committed)(use?"git?checkout?--?<file>..."?to?discard?changes?in?working?directory)modified:???demo1.txt no?changes?added?to?commit?(use?"git?add"?and/or?"git?commit?-a")Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?stash????#儲藏當前修改狀態(tài) warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. Saved?working?directory?and?index?state?WIP?on?wsyht:?0631e15?wsyht?commit HEAD?is?now?at?0631e15?wsyht?commitAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?status?????#再次查看狀態(tài)發(fā)現(xiàn)什么都沒有 On?branch?wsyht nothing?to?commit,?working?tree?clean第二步:
第二個bug分支操作
bug分支修改->提交-合并-刪除bug分支
第三步:
#回到第一個分支wsyht操作
合并master分支->調(diào)回wsyht修改狀態(tài)->add提交
合并master分支主要就是先把剛剛修改的bug先合并了,然后再調(diào)回剛才修改的狀態(tài),繼續(xù)開發(fā)提交
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?checkout?wsyht Switched?to?branch?'wsyht'Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?ls demo1.txt??demo2.txt c Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?cat?demo1.txt 1111 1111 2222 3333 5555 777 888Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?status On?branch?wsyht nothing?to?commit,?working?tree?cleanAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?merge?master Updating?0631e15..cd961d2 Fast-forwarddemo1.txt?|?2?+-1?file?changed,?1?insertion(+),?1?deletion(-)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?ls demo1.txt??demo2.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?cat?demo1.txt 1111 1111 2222 3333 5555 777 bbbAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?stash?pop????????#恢復(fù)原來修改的狀態(tài) Auto-merging?demo1.txt On?branch?wsyht Changes?not?staged?for?commit:(use?"git?add?<file>..."?to?update?what?will?be?committed)(use?"git?checkout?--?<file>..."?to?discard?changes?in?working?directory)modified:???demo1.txt no?changes?added?to?commit?(use?"git?add"?and/or?"git?commit?-a") Dropped?refs/stash@{0}?(d1ba7adfb82639ae0ecf1f946349dc064fb30617) Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?ls demo1.txt??demo2.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?cat?demo1.txt wsyht 1111 2222 3333 5555 777 bbbAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?add?demo1.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?status On?branch?wsyht Changes?to?be?committed:(use?"git?reset?HEAD?<file>..."?to?unstage)modified:???demo1.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?commit?-m'commit?wsyht' [wsyht?9d505e6]?commit?wsyht1?file?changed,?1?insertion(+),?1?deletion(-)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?ls demo1.txt??demo2.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?status On?branch?wsyht nothing?to?commit,?working?tree?cleanAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?cat?demo1.txt wsyht 1111 2222 3333 5555 777 bbbAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?log?--graph?--pretty=oneline?--abbrev-commit *?9d505e6?commit?wsyht *???cd961d2?merged?bug |\ |?*?ed1eded?commit?bug |/ *?0631e15?wsyht?commit *?3a59897?wsyht?commint *?c1e71b1?test *?d693375?888 *?07e6d4c?777 *?43185ac?delete?test1.txt *?7ecdd08?一次性提交所有文件 *?fc33a37?1111 *?85ce2b6?4444 *?8445579?文件增加2222一行 *?25e4895?增加1111一行 *?a7e593c?demo1提交 Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?ls demo1.txt??demo2.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?cat?demo1.txt wsyht 1111 2222 3333 5555 777 bbb第四步
#主分支Master操作
切回主分支->合并wsyht分支->add提交->刪除wsyht分支
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?checkout?master Switched?to?branch?'master' Your?branch?is?ahead?of?'origin/master'?by?5?commits.(use?"git?push"?to?publish?your?local?commits)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?ls demo1.txt??demo2.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?cat?demo1.txt 1111 1111 2222 3333 5555 777 bbbAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?merge?wsyht Updating?cd961d2..9d505e6 Fast-forwarddemo1.txt?|?2?+-1?file?changed,?1?insertion(+),?1?deletion(-)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?cat?demo1.txt wsyht 1111 2222 3333 5555 777 bbbAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?branch?-d?wsyht Deleted?branch?wsyht?(was?9d505e6).雙分支操作總結(jié):主分支、第一分支、第二分支
第一步:第一分支開發(fā)修改、然后儲藏狀態(tài)
第二步:從第一分支切回Master分支,再創(chuàng)建并切去第二分支,開發(fā)修改,提交,并合并到Master分支,刪除第一分支
第三步:從Master分支切到第二分支,并在第二分支合并Master分支,恢復(fù)第二分支修改狀態(tài),繼續(xù)修第四步:從第二分支再切回Master分支,并合并第二分支修改的內(nèi)容,刪除第二分支
分支命令總結(jié):
查看分支合并情況
git log --graph --pretty=oneline --abbrev-commit ? #--graph圖形化顯示,--pretty=oneline簡短顯示,--abbrev-commit驗證碼省略顯示
git stash????#儲藏內(nèi)容
git stash list ? #查看儲藏內(nèi)容
git stash pop/git stash apply ? ?#恢復(fù)上一個工作內(nèi)容
二者不同的是git stash apply恢復(fù)后,stash內(nèi)容并未刪除,你需要用git stash drop來刪除,而用git stash pop,恢復(fù)的同時把stash內(nèi)容也刪了
十四、強制刪除分支
當我們在某第二個分支開發(fā)某一個功能,提交分支之后,回頭策劃卻說還是不要改了,這個時候我們不能合并分支,只能刪除第二個分支
如下:
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?branch *?masterAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?checkout?-b?wsyht Switched?to?a?new?branch?'wsyht'Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?ls demo1.txt??demo2.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?rm?-rf?demo1.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?ls demo2.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?add?demo1.txt Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?commit?-m'delete?demo1' [wsyht?c83d5a1]?delete?demo11?file?changed,?7?deletions(-)delete?mode?100644?demo1.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?checkout?master Switched?to?branch?'master' Your?branch?is?ahead?of?'origin/master'?by?6?commits.(use?"git?push"?to?publish?your?local?commits) Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master)$?git?branch?-d?wsyht??? error:?The?branch?'wsyht'?is?not?fully?merged. If?you?are?sure?you?want?to?delete?it,?run?'git?branch?-D?wsyht'.???#這里說還沒有合不能用-d刪除,只能用大D強制刪除Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?branch?-D?wsyht???#用大D強制刪除 Deleted?branch?wsyht?(was?c83d5a1).Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?branch *?master Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $
十五、多人協(xié)作
當在你提交或幫隆的時候會自動幫你把本地和遠程的master分支對應(yīng)起來,遠程庫的默認名稱是origin
#查看遠程庫
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?remote???#查看遠程庫的信息 originAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?remote?-v????#查看遠程庫的詳信息 origin??git@github.com:wsyht/demo_test.git?(fetch) origin??git@github.com:wsyht/demo_test.git?(push)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $上面顯示了可以抓取和推送的origin的地址。如果沒有推送權(quán)限,就看不到push的地址
#推送分支
推送分支就是把本地的分支提交到對應(yīng)的遠程庫的分支中,如果是Master分支就提交到遠程庫的Master分支,如果是其它分支如wsyht,就提交到遠程庫的wsyht分支中與之相對應(yīng),如果一些bug分支不需要提交的,就把他先主分支先合并了,然后再提交到遠程庫中
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?cat?demo1.txt wsyht 1111 2222 3333 5555 777 bbbAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?echo?"test"?>?demo1.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?cat?demo1.txt testAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?add?demo1.txt warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master)$?git?commit?-m?'test?commit'?demo1.txt warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. [master?warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. 5e368ff]?test?commit warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.1?file?changed,?1?insertion(+),?7?deletions(-)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?pull remote:?Counting?objects:?5,?done. remote:?Compressing?objects:?100%?(3/3),?done. remote:?Total?5?(delta?0),?reused?3?(delta?0),?pack-reused?0 Unpacking?objects:?100%?(5/5),?done. From?github.com:wsyht/demo_testd693375..3020d4c??master?????->?origin/master Removing?demo2.txt Merge?made?by?the?'recursive'?strategy.demo2.txt?|?2?--oo.txt????|?1?+2?files?changed,?1?insertion(+),?2?deletions(-)delete?mode?100644?demo2.txtcreate?mode?100644?oo.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?ls demo1.txt??oo.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?push?-u?origin?master?????#成功了 Counting?objects:?19,?done. Delta?compression?using?up?to?4?threads. Compressing?objects:?100%?(14/14),?done. Writing?objects:?100%?(19/19),?1.70?KiB?|?0?bytes/s,?done. Total?19?(delta?2),?reused?0?(delta?0) To?git@github.com:wsyht/demo_test.git3020d4c..6bbbcdf??master?->?master Branch?master?set?up?to?track?remote?branch?master?from?origin.#這里再強調(diào)一次,是當?shù)谝淮蝡ush你才需要-u,到后面就不需要再加-u參數(shù)了
查看GitHub成功同步上去了
2)多人同推沖突
現(xiàn)在假如有兩個人兩臺電腦共同推一個分支的文件,假如我先推了,張三也又接著推了,張三就會報錯推不上去,這個時候,我們來看看怎么操作
角色:“我” 和 “張三”
首先我的在我的電腦上創(chuàng)建一個wsyht分支用來做開發(fā),因為master分支張三是無法拉取下來做開發(fā)的,所以只能搞一個wsyht分支,在wsyht分支上做開發(fā),開發(fā)完成功能后,提交wsyht分支,然后push到git服務(wù)器,再合并master分支,然后把master分支提交,push到git服務(wù)器
我的電腦操作,如下:
流程:創(chuàng)建分支->切換分支->推送 ? #此分支一般工作中事先存在,而我這里為了做事驗,所以臨時創(chuàng)建
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?branch *?masterAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?branch?wsyhtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?branch *?masterwsyhtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(master) $?git?push?origin?wsyht Everything?up-to-date張三電腦操作
操作過程:取遠程庫的分支到本地->創(chuàng)遠程分支到本地->修改提交->push
Administrator@YYBFVJDMAPF13NB?MINGW64?/f $?mkdir?demo2Administrator@YYBFVJDMAPF13NB?MINGW64?/f $?cd?demo2Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2 $?git?init Initialized?empty?Git?repository?in?F:/demo2/.git/Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2?(master) $?git?remote?add?origin?git@github.com:wsyht/demo_test.gitAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2?(master) $?git?fetch?origin???#取遠程庫的分支到本地,(如果只想取某個分支,git?fetch?origin?分支名) remote:?Counting?objects:?58,?done. remote:?Compressing?objects:?100%?(36/36),?done. remote:?Total?58?(delta?3),?reused?56?(delta?3),?pack-reused?0 Unpacking?objects:?100%?(58/58),?done. From?github.com:wsyht/demo_test*?[new?branch]??????master?????->?origin/master*?[new?branch]??????wsyht??????->?origin/wsyhtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2?(master) $?lsAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2?(master) $?git?branch?wsyht?origin/wsyht????#創(chuàng)建遠程庫的分支到本地 Branch?wsyht?set?up?to?track?remote?branch?wsyht?from?origin.Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2?(master) $?lsAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2?(master) $?git?checkout?wsyht??#切換到創(chuàng)建的分支 Switched?to?branch?'wsyht' Your?branch?is?up-to-date?with?'origin/wsyht'. l Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2?(wsyht) $?ls???#可查看到該分支的文件了 demo1.txt??oo.txt$?cat?demo1.txt test 222 333Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2?(wsyht) $?echo?"111"?>>?demo1.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2?(wsyht) $?cat?demo1.txt test 222 333 111Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2?(wsyht) $?git?add?demo1.txt warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2?(wsyht) $?git?commit?-m?'zhangsan?add?111?demo1' [wsyht?warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. e4ace0b]?zhangsan?add?111?demo1 warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.1?file?changed,?1?insertion(+)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2?(wsyht) $?git?push?origin?wsyht Counting?objects:?3,?done. Delta?compression?using?up?to?4?threads. Compressing?objects:?100%?(2/2),?done. Writing?objects:?100%?(3/3),?290?bytes?|?0?bytes/s,?done. Total?3?(delta?0),?reused?0?(delta?0) To?git@github.com:wsyht/demo_test.git924fcc5..e4ace0b??wsyht?->?wsyht我的電腦操作
操作過程:切換分支->修改文件->提交->push->解決沖突->再push
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?pwd /f/demo1Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?checkout?wsyht Already?on?'wsyht'Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?cat?demo1.txt test 222 333Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?echo?'111'?>>?demo1.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?add?demo1.txt warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?commit?-m'wo?add?111?demo' [wsyht?warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. 805dcba]?wo?add?111?demo warning:?LF?will?be?replaced?by?CRLF?in?demo1.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.1?file?changed,?1?insertion(+)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?push?origin?wsyht???#這里push失敗是因為沖突了,提示叫你git?pull To?git@github.com:wsyht/demo_test.git!?[rejected]????????wsyht?->?wsyht?(fetch?first) error:?failed?to?push?some?refs?to?'git@github.com:wsyht/demo_test.git' hint:?Updates?were?rejected?because?the?remote?contains?work?that?you?do hint:?not?have?locally.?This?is?usually?caused?by?another?repository?pushing hint:?to?the?same?ref.?You?may?want?to?first?integrate?the?remote?changes hint:?(e.g.,?'git?pull?...')?before?pushing?again. hint:?See?the?'Note?about?fast-forwards'?in?'git?push?--help'?for?details.Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?pull???#這里git?pull又提示進行下面的一步操作 remote:?Counting?objects:?3,?done. remote:?Compressing?objects:?100%?(2/2),?done. remote:?Total?3?(delta?0),?reused?3?(delta?0),?pack-reused?0 Unpacking?objects:?100%?(3/3),?done. From?github.com:wsyht/demo_test924fcc5..e4ace0b??wsyht??????->?origin/wsyht There?is?no?tracking?information?for?the?current?branch. Please?specify?which?branch?you?want?to?merge?with. See?git-pull(1)?for?details.git?pull?<remote>?<branch>If?you?wish?to?set?tracking?information?for?this?branch?you?can?do?so?with:git?branch?--set-upstream-to=origin/<branch>?wsyhtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?branch?--set-upstream-to=origin/wsyht Branch?wsyht?set?up?to?track?remote?branch?wsyht?from?origin.Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?pull???#執(zhí)行完上面那一步,終于可以pull了,輸出的提示合并也沒有問題,如果合并有問題,就修改剛剛修改的文件,然后再push Merge?made?by?the?'recursive'?strategy.Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?pull?? Already?up-to-date.Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?cat?demo1.txt test 222 333 111Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?push?origin?wsyht???#好push成功 Counting?objects:?2,?done. Delta?compression?using?up?to?4?threads. Compressing?objects:?100%?(2/2),?done. Writing?objects:?100%?(2/2),?338?bytes?|?0?bytes/s,?done. Total?2?(delta?1),?reused?0?(delta?0) To?git@github.com:wsyht/demo_test.gite4ace0b..ca2b12a??wsyht?->?wsyht現(xiàn)在我們看看GitHub上有沒有push成功了
多人協(xié)作沖突解決總結(jié):1、如果我push報錯,則是別人先push了
???????????????????? ?2、這個時候我就提示git pull,或進行下一步操作
???????????????????? ?3、如果git pull的時候合并有問題,就要修改文件,解決沖突
????????????????????? 4、再次push就成功了
十六、創(chuàng)建工作目錄的兩個方法
第一種、就是上面張三用的方:,git init后,然后使用取得遠程庫的分支,創(chuàng)建遠程庫的分支
現(xiàn)在我們來演示第二種方法:git clone
當來了一位新員工,我們就給克隆git的項目下來給他開發(fā)推送
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo3 $?git?clone?git@github.com:wsyht/demo_test.git Cloning?into?'demo_test'... remote:?Counting?objects:?73,?done. remote:?Compressing?objects:?100%?(45/45),?done. remote:?Total?73?(delta?5),?reused?71?(delta?5),?pack-reused?0 Receiving?objects:?100%?(73/73),?6.22?KiB?|?0?bytes/s,?done. Resolving?deltas:?100%?(5/5),?done. Checking?connectivity...?done.?Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo3 $?ls demo_test/ cd Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo3 $?cd?demo_test/Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo3/demo_test?(master) $?ls demo1.txt??oo.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo3/demo_test?(master) $?cat?oo.txt 111Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo3/demo_test?(master) $?echo?"222"?>>oo.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo3/demo_test?(master) $?git?status On?branch?master Your?branch?is?up-to-date?with?'origin/master'. Changes?not?staged?for?commit:(use?"git?add?<file>..."?to?update?what?will?be?committed)(use?"git?checkout?--?<file>..."?to?discard?changes?in?working?directory)modified:???oo.txtno?changes?added?to?commit?(use?"git?add"?and/or?"git?commit?-a")Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo3/demo_test?(master) $?git?add?oo.txt warning:?LF?will?be?replaced?by?CRLF?in?oo.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo3/demo_test?(master) $?git?status warning:?LF?will?be?replaced?by?CRLF?in?oo.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. On?branch?master Your?branch?is?up-to-date?with?'origin/master'. Changes?to?be?committed:(use?"git?reset?HEAD?<file>..."?to?unstage)modified:???oo.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo3/demo_test?(master) $?git?commit?-m'yes' [master?warning:?LF?will?be?replaced?by?CRLF?in?oo.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. 3538a0c]?yes warning:?LF?will?be?replaced?by?CRLF?in?oo.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.1?file?changed,?1?insertion(+)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo3/demo_test?(master) $?ls demo1.txt??oo.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo3/demo_test?(master) $?cat?oo.txt 111 222Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo3/demo_test?(master) $?git?push?origin?master???#沒有問題,推送成功 Counting?objects:?3,?done. Delta?compression?using?up?to?4?threads. Compressing?objects:?100%?(2/2),?done. Writing?objects:?100%?(3/3),?265?bytes?|?0?bytes/s,?done. Total?3?(delta?0),?reused?1?(delta?0) To?git@github.com:wsyht/demo_test.git55c5d89..3538a0c??master?->?master十七、忽略需要push的文件
忽略push的文件很簡單,只需要在你的工作區(qū)目錄下創(chuàng)建一個特珠的.gitignore文件,然后把忽略的文件名或文件尾輟填進去即可,但在windows下你是創(chuàng)建不了.xxx的文件,他會提示你輸入文件名,這個時候你可以用編輯器另存為就可以創(chuàng)建出這個文件
如:
如果你添加的某個文件被忽略了,但你又想添加,
有兩種方法解決:第一:修改.gitignore文件
????????????????第二:就是強制執(zhí)行他
第一種方法:查看要添加的那個文件在.gitignore的哪一行設(shè)置了忽略,然后去修改.gitignore文件
git?check-ignore?-v?xx.xx??#xx.xx是被忽略的那個文件第二種方法:加上-f 強制添加執(zhí)行
git?add?-f?xx.xxGitHub也為我們準備了各種配置文件,大家可以參考一下
https://github.com/github/gitignore
十八、更新本地工作區(qū)
張三操作: ?#demo2是張三的工作區(qū)目錄
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2?(wsyht) $?echo?"333"?>>?oo.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2?(wsyht) $?git?add?oo.txt warning:?LF?will?be?replaced?by?CRLF?in?oo.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2?(wsyht) $?git?commit?-m'333' [wsyht?warning:?LF?will?be?replaced?by?CRLF?in?oo.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. af8dc1c]?333 warning:?LF?will?be?replaced?by?CRLF?in?oo.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.1?file?changed,?1?insertion(+)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2?(wsyht) $?git?push?origin?wsyht Counting?objects:?3,?done. Delta?compression?using?up?to?4?threads. Compressing?objects:?100%?(2/2),?done. Writing?objects:?100%?(3/3),?269?bytes?|?0?bytes/s,?done. Total?3?(delta?0),?reused?0?(delta?0) To?git@github.com:wsyht/demo_test.gitc3f4ac0..af8dc1c??wsyht?->?wsyht我的操作: 關(guān)健字:?git pull #更新本地工作區(qū)
記得修改文件之前先git pull一下,再修改和push
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2?(wsyht) $?cd?../demo1Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?ls demo1.txt??oo.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?ls demo1.txt??oo.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?pull remote:?Counting?objects:?3,?done. remote:?Compressing?objects:?100%?(2/2),?done. remote:?Total?3?(delta?0),?reused?3?(delta?0),?pack-reused?0 Unpacking?objects:?100%?(3/3),?done. From?github.com:wsyht/demo_testc3f4ac0..af8dc1c??wsyht??????->?origin/wsyht Updating?c3f4ac0..af8dc1c Fast-forwardoo.txt?|?1?+1?file?changed,?1?insertion(+)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?cat?oo.txt??#查看已經(jīng)看到333了 111 222 333十九、CentOS下搭建Git服務(wù)器
#服務(wù)器端部署
[root@localhost?local]#?ifconfig?|grep??"inet?addr:"?|?grep?Bcast?|?awk?-F":"?'{print?$2}'?|?awk?-F'?'?'{print?$1}' 192.168.1.102 [root@localhost?~]#?yum?-y?install?git [root@localhost?~]#?useradd??git??#添加的git一定要能登陸,否則你clone和push都會報錯,所以不能加-s參數(shù) [root@localhost?~]#?git?init?--bare?/usr/local/wsyht.git [root@localhost?~]#?ls?/usr/local/wsyht.git/ branches??description??hooks??objects config????HEAD?????????info???refs [root@localhost?~]#?chown?-R?git:git?/usr/local/wsyht.git?#這里一定要加-R,否則本地push會報沒有權(quán)限 [root@localhost?~]#?cd?/home/git/ [root@localhost?git]#?mkdir?-m?700?.ssh??#一定要給700權(quán)限,否則密鑰對不上 [root@localhost?git]#?chown?git:?.ssh????#記住所有者和所屬組一定要給git用戶,否則會連不上,提示你輸入密碼,所以權(quán)限方面要注意兩個方面,第一個就是所有者所屬組,第二個就是他的讀寫執(zhí)行權(quán)限 [root@localhost?git]#?cd?.ssh [root@localhost?.ssh]#?echo?"ssh-rsa?AAAAB3NzaC1yc2EAAAADAQABAAABAQC6y9vxDIo9rqCAW7xebYpFFIL5M0wvwfe/qbcehE5VRvcVvKnlH/TsEygxGJEMCgjfLW7sKo5Me3W5KJNfNFeWQcn6AMSm08ZQk9qCbP1DpyW+AWvnz2KORYzM0+yLSbjd8py6XpWaMO1zX4TFm6VH3IRfaIpe0G/DhIzMxfNnNdCcXw6ou1Bb6tI41Q78ovGZGyPum4Nt03vLQpe0d6p5bqk21h1VeDbnH2mKa5GbS+OLgK+1qMvUy5+q8deASn18gepxhNtk+7LqNWlhTN8hQBFHOsqMuLvb5IGqBTRy/HfEFPiEkmgy9YrSWHiaPqJ/rCIYufaIKEgyAlISIqPj?wsyht"?>>?authorized_keys解釋:上述的--bare意思是只會創(chuàng)建一個裸倉庫,而沒有工作區(qū),因為服務(wù)器上的Git倉庫純碎是為了共享,所以不讓用戶直接到服務(wù)器上去改工作區(qū),并且服務(wù)器上的倉庫通常以.git結(jié)尾,所以我上面起了一個wsyht.git的倉庫名字,再把倉庫的權(quán)限改好,把公鑰的Key配上去,生成key的使用方式前面已講了,這里就不再闡述,到下面我們就可以在客戶端上進行clone,push等之類的操作
#本地操作
連接方式是 git clone git@server ip:/倉庫路徑/倉庫目錄名字
Administrator@YYBFVJDMAPF13NB?MINGW64?/f $?mkdir?demo2Administrator@YYBFVJDMAPF13NB?MINGW64?/f $?cd?demo2Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2 $?git?clone?git@192.168.1.102:/usr/local/wsyht.git Cloning?into?'wsyht'... remote:?Counting?objects:?11,?done. remote:?Compressing?objects:?100%?(4/4),?done. remote:?Total?11?(delta?0),?reused?0?(delta?0) Receiving?objects:?100%?(11/11),?done. Checking?connectivity...?done.Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2/wsyht?(master) $?git?remote?-v origin??git@192.168.1.102:/usr/local/wsyht.git?(fetch) origin??git@192.168.1.102:/usr/local/wsyht.git?(push)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2 $?ls wsyht/ c Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2 $?cd?wsyhtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2/wsyht?(master) $?lsAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2/wsyht?(master) $?echo?"111"?>?test.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2/wsyht?(master) $?git?add?test.txt warning:?LF?will?be?replaced?by?CRLF?in?test.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2/wsyht?(master) $?git?commit?-m'com?test.txt' [master?7108256]?com?test.txt warning:?LF?will?be?replaced?by?CRLF?in?test.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.1?file?changed,?1?insertion(+)create?mode?100644?test.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2/wsyht?(master) $?git?push?origin?master Counting?objects:?3,?done. Writing?objects:?100%?(3/3),?238?bytes?|?0?bytes/s,?done. Total?3?(delta?0),?reused?2?(delta?0) To?git@192.168.1.102:/usr/local/wsyht.git65c1f60..7108256??master?->?masterOK,可以看到clone和push都沒有問題一切正常,其他命令操作方式跟遠程倉庫一樣,唯一不一樣的就是連接的clone后面的那一串域名不同
注意:我在本地push文件到Git服務(wù)器后,因為創(chuàng)建的這個是裸倉庫是沒有工作區(qū)的,所以他的存文件的方式是不一樣的,貌似是以什么進制方式存著,所以你在Git服務(wù)器里是找不到他的文件的,這個很正常,他是占用服務(wù)器的磁盤空間的
這個是Git服務(wù)器存取分支目錄:
/usr/local/wsyht.git/refs/heads/
當我在本地創(chuàng)建了新分支的時候,則所有分支文件會在上面目錄下顯示出來
如下我又創(chuàng)建了一個wsyht分支,則在服務(wù)器上又顯示一個wsyht的普通文件
[root@localhost?heads]#?pwd /usr/local/wsyht.git/refs/heads [root@localhost?heads]#?ll 總用量?8 -rw-rw-r--?1?git?git?41?7月??31?21:12?master -rw-rw-r--?1?git?git?41?7月??31?21:20?wsyht而這個目錄是存儲標簽內(nèi)容,這里我就不再一一演示了
[root@localhost tags]# pwd
/usr/local/wsyht.git/refs/tags
報錯展示:
1)因為git服務(wù)器里的git用戶禁止登陸了,本地clone不上所以報的錯
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/KuGou $?git?clone?git@192.168.1.102:/usr/local/wsyht.git Cloning?into?'wsyht'... fatal:?protocol?error:?bad?line?length?character:?This解決方法:讓git可以登陸,但不用設(shè)密碼,因為人我們是用key去連的,這樣才安全
2)因為git權(quán)限不夠報的錯
?Administrator@YYBFVJDMAPF13NB?MINGW64?/f/KuGou yht?(master) $?git?push?origin?master Counting?objects:?3,?done. Writing?objects:?100%?(3/3),?203?bytes?|?0?bytes/s,?done. Total?3?(delta?0),?reused?0?(delta?0) error:?insufficient?permission?for?adding?an?object?to?repository?database?./objects fatal:?failed?to?write?object error:?unpack?failed:?unpack-objects?abnormal?exit To?git@192.168.1.102:/usr/local yht.git!?[remote?rejected]?master?->?master?(n/a?(unpacker?error)) error:?failed?to?push?some?refs?to?'git@192.168.1.102:/usr/local yht.git'解決方法:在Git服務(wù)器上把Git倉庫的整個目錄和文件子目錄子文件的所有者和所屬組都給Git即可
二十、鉤子腳本
SVN常用腳本:pre-commit,post-commit
Git常用腳本:post-receive
SVN和Git的pre-commit說明
這是提交要執(zhí)行的動作,沒什么好說的,不一樣的是Git是在本地執(zhí)行動作
SVN和Git的post-commit說明
這是提交后要執(zhí)行的操作,也沒什么好說的,同理,不一樣的是,Git他提交后執(zhí)行的動作是在本地執(zhí)行的,因為他提交的內(nèi)容是提交到本地倉庫,他本地就是一個倉庫
Git的 post-receive ? => 重點內(nèi)容到了
Git的post-receive是當接到你在本地push內(nèi)容到我的Git服務(wù)器后,觸發(fā)了我Git服務(wù)器的post-receive腳本去執(zhí)行相應(yīng)的動作,而這個要腳本里面要執(zhí)行的這個動作就由你自已去定義
現(xiàn)在我們來演示一下
Git服務(wù)器作:
一定要先把文件復(fù)制出來,把后面的.sample去掉才能使用
從上圖可以看到,復(fù)制出來的post-receive權(quán)限不對,我們要先改權(quán)限
添加腳本要執(zhí)行的內(nèi)容
[root@bogon?hooks]#?echo?"/bin/touch?/tmp/test_wsyht.txt"?>>?post-receive??? [root@bogon?hooks]#?cat?post-receive #!/bin/sh # #?An?example?hook?script?for?the?"post-receive"?event. # #?The?"post-receive"?script?is?run?after?receive-pack?has?accepted?a?pack #?and?the?repository?has?been?updated.??It?is?passed?arguments?in?through #?stdin?in?the?form #??<oldrev>?<newrev>?<refname> #?For?example: #??aa453216d1b3e49e7f6f98441fa56946ddcd6a20?68f7abf4e6f922807889f52bc043ecd31b79f814?refs/heads/master # #?see?contrib/hooks/?for?a?sample,?or?uncomment?the?next?line?and #?rename?the?file?to?"post-receive". #.?/usr/share/git-core/contrib/hooks/post-receive-email /bin/touch?/tmp/test_wsyht.txt因為/tmp目錄下是所有用戶都有讀、寫、執(zhí)行的,如果要對其他目錄有讀寫執(zhí)行權(quán)限,必須得先把所有者所屬組改成Git,然后再給他相應(yīng)的讀、寫、執(zhí)行權(quán)限即可,
再查看一下/tmp目錄下的內(nèi)容,目前沒有test_wsyht.txt文件
[root@bogon?hooks]#?ls?/tmp/ keyring-R8s7ni??pulse-gbg3wjQuvzvI??virtual-root.cNwkfe??vmware-config0 orbit-gdm???????pulse-zqLc6oaaNKa9??virtual-root.D0hQX0??VMwareDnD orbit-root??????ssh-hotQgm2726??????virtual-root.KIVXJC??vmware-root好,到這一步,我們的Git服務(wù)器就算配置完了
本地操作:
提交和push
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/test/wsyht?(master) $?echo?"test?commit"?>>?wsyht.txtAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/test/wsyht?(master) $?git?add?wsyht.txt warning:?LF?will?be?replaced?by?CRLF?in?wsyht.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.Administrator@YYBFVJDMAPF13NB?MINGW64?/f/test/wsyht?(master) $?git?commit?-m'commit?test' [master?warning:?LF?will?be?replaced?by?CRLF?in?wsyht.txt. The?file?will?have?its?original?line?endings?in?your?working?directory. 673c27a]?commit?test warning:?LF?will?be?replaced?by?CRLF?in?wsyht.txt. The?file?will?have?its?original?line?endings?in?your?working?directory.1?file?changed,?1?insertion(+)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/test/wsyht?(master) $?git?push?origin?master Counting?objects:?3,?done. Delta?compression?using?up?to?4?threads. Compressing?objects:?100%?(3/3),?done. Writing?objects:?100%?(3/3),?289?bytes?|?0?bytes/s,?done. Total?3?(delta?1),?reused?0?(delta?0) To?git@192.168.1.113:/usr/local/wsyht.gitf979810..673c27a??master?->?master查看Git服務(wù)器
【拓展知識】
一、別名配置
法1:真接命令行配置
輸入--global是對當前用戶起作用的,如果不加只對當前倉庫起作用
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2?(wsyht) $?git?config?--global?alias.st?status???#把status?配置成別名?stAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2?(wsyht) $?git?config?--global?alias.co?checkout?#把checkout?配置成別名?coAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2?(wsyht) $?git?config?--global?alias.ci?commit???#把commit?配置成別名?ciAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2?(wsyht) $?git?config?--global?alias.br?branch???#把branch?配置成別名?brAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?config?--global?alias.unstage?'reset?HEAD'Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?config?--global?alias.last?'log?-1'Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2?(wsyht) $?git?status???#用原來的命令查看狀態(tài) On?branch?wsyht Your?branch?is?up-to-date?with?'origin/wsyht'. nothing?to?commit,?working?tree?cleanAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo2?(wsyht) $?git?st???????#用別名查看狀態(tài),成功了,其他別名同樣使用方法,這里不再一一演示 On?branch?wsyht Your?branch?is?up-to-date?with?'origin/wsyht'. nothing?to?commit,?working?tree?clean法2:配置文件配置
我們可以改兩個配置文件
首先先說第一個配置文件,我們在命令行輸入命令--global設(shè)置別名,也是把命令寫進這個文件的,我們也可以進到這個文件修改,他就是你家目錄下的.gitconfig,敲cd回車就可以回到你的家目錄
第一個配置文件配置
可以看到.gitconfig文件寫入了我在命令行設(shè)置別名的命令,如果你加--global就會寫入這個配置文件,你也可以直接進到這個文件添加,而不在終端執(zhí)行命令,要刪除別名的話進到這個文件把相應(yīng)的別名命令刪除即可,這個文件是針對當前用戶生效的
第二個配置文件
在倉庫目錄下.git/config目錄下,此配置文件,是針對當前倉庫有效,所以在當你在終端執(zhí)行命令的時候不加--global就會把命令寫入這個文件,你也可以進到這個文件進行添加刪除別名命令,下面我們來看配置文件內(nèi)容
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/.git?(GIT_DIR!) $?pwd /f/demo1/.gitAdministrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1/.git?(GIT_DIR!) $?cat?config [core]repositoryformatversion?=?0filemode?=?falsebare?=?falselogallrefupdates?=?truesymlinks?=?falseignorecase?=?true [branch?"master"] [remote?"origin"]url?=?git@github.com:wsyht/demo_test.gitfetch?=?+refs/heads/*:refs/remotes/origin/* [branch?"master"]remote?=?originmerge?=?refs/heads/master [branch?"wsyht"]remote?=?originmerge?=?refs/heads/wsyht #[alias]#br?=?branch添加方法一樣,跟上面的第一個配置文件.gitconfig一樣,這里我只是為了演示,所以我把他注釋掉了
二、標簽操作
1)創(chuàng)建標簽
2)刪除和push標簽
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?tag?-d?v0.3 Deleted?tag?'v0.3'?(was?784bb96)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?tag v0.4 v1.0 v1.1Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?push?origin?v0.4??#提交單個標簽 Counting?objects:?1,?done. Writing?objects:?100%?(1/1),?180?bytes?|?0?bytes/s,?done. Total?1?(delta?0),?reused?0?(delta?0) To?git@github.com:wsyht/demo_test.git*?[new?tag]?????????v0.4?->?v0.4Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?push?origin??--tags???#提交所有標簽 Total?0?(delta?0),?reused?0?(delta?0) To?git@github.com:wsyht/demo_test.git*?[new?tag]?????????v1.0?->?v1.0*?[new?tag]?????????v1.1?->?v1.1在GitHub上查看
標簽都出來了,push成功
3)刪除遠程庫的標簽
Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?tag?-d?v0.4 Deleted?tag?'v0.4'?(was?dea0b58)Administrator@YYBFVJDMAPF13NB?MINGW64?/f/demo1?(wsyht) $?git?push?origin?:refs/tags/v0.4 To?git@github.com:wsyht/demo_test.git-?[deleted]?????????v0.4可以看到,遠程庫的標簽已經(jīng)刪除成功
最后再說一下,有的人說SVN有工作可以提交上傳,但是Git卻要敲命令,現(xiàn)在我告訴你Git也是有工具的,如果你不喜歡命令行的話,你可以去下載這個工具,github sourcetree?如下圖,這是我網(wǎng)上找來的圖片,界面就是這個樣子
但是我還想說,我還是喜歡用命令行,上面那個工具我沒用過,我相信大多數(shù)人都是用命令行,因為你通過命令行去做執(zhí)行的話,你會更有滿足感,你操作速度會更快更靈活更有快感,而且你知道每一步的步驟,你也不會混亂,相信我,用命令行吧,我相信大多數(shù)人都是用命令git bash的,放棄這個工具吧
Git常用命令總結(jié):
git help git ? ? ? ? ? ? ? ?#查看git命令幫助
git config --help ? ? ? ? ? ? #查看config幫助
git config --global user.name 'wsyht' ? ? ? ? ? #設(shè)置用戶名標識
git config --global user.email t89191958201@139.com ?#設(shè)置郵箱標識
git config --list ? ? ? ? ? ?#查看配置內(nèi)容
git init ? ? ? ? ? ? ? ? ?#創(chuàng)建版本庫
git add 文件名 ? ? ? ? ? ? ?#添加到暫存區(qū)
git commit -m'你描述的內(nèi)容' ?#提交
git status ?? ? ? ? #查看狀態(tài)
git diff ?文件名 ? ? #查看文件比較
git log ? ? ? ? ? ?#查看提交的歷史記錄
git log --graph --pretty=oneline --abbrev-commit ? ? #--graph以圖形化顯示,--pretty=oneline省略部分信息輸出,--abbrev-commit則是縮短版本號簡短顯示
git reset --hard HEAD^ ? ? ? #git reset --hard HEAD^
git reset --hard HEAD^^ ? ? ?#git reset --hard HEAD^^
git reset --hard HEAD~100 ? ? #回退到前100個版本
git reflog? ? ? ? ? ? ? #獲取版本號
git reset --hard 版本號 ? ? ?#恢復(fù)版本
git checkout -- 文件名 ? ? ? #撤銷工作區(qū)的修改
git checkout .? ? ? ? ? ? #撤銷工作區(qū)的修改
git reset HEAD 文件名? ? ? ?#轍消暫存區(qū)的修改
git remote add origin git@github.com:wsyht/demo_test.git ? #添加遠程url連接
git push -u origin master ? ?#第一次push需要加-u
git remote rm origin ? ? ? #刪除遠程默認倉庫名"origin
git branch ? ? #查看分支
git branch 分支名字 ? ? ? ? ?#創(chuàng)建分支 ? ? ? ? ? ? ?
git checkout 分支名字 ? ? ? ? #切換分支
git checkout -b 分支名字 ? ? ? #創(chuàng)建切換分支
git merge 分支名字 ? ? ? ? ? #合并某分支到當前分支
git branch -d 分支名字 ? ? ? ?#刪除分支
git branch -D wsyht ? ? ? ? ?#大D強制刪除
git stash? ? ? ? ?#儲藏當前修改狀態(tài)
git stash list ? ? ?#查看儲藏內(nèi)容
git stash pop ? ? ? #恢復(fù)原來修改的狀態(tài)
git remote -v ? ? ? #查看遠程庫的詳信息
git pull ? ? ? ? ? ? ?#更新本地工作區(qū)
git fetch origin ? ? ? ? #取所有遠程庫的分支到本地
git fetch origin 分支名 ? ?#取某個分支
git branch 分支名 origin/分支名 ? ? ? ? ? ? ?#創(chuàng)建遠程庫的分支到本地
git clone git@github.com:github注冊名/項目名.git ? #克隆GitHub項目到本地
git clone git@server ip:/倉庫路徑/倉庫目錄名字 ? ?#克隆Git服務(wù)器項目到本地
git check-ignore -v xx.xx ? ? ?#查看要添加的那個文件在.gitignore的哪一行設(shè)置了忽略
git add -f 文件名 ? ? ? ? ? ?#強制添加此文件到暫存區(qū)
it tag v1.0 ? ? ? ?? ? ? ?#創(chuàng)建標簽v1.0
git tag v1.1 805dcba ? ? ? ? ?#通過指定他的號碼來給他創(chuàng)建標簽
git tag -a v0.4 -m "我的第三個標簽" e4ace0b ? ?#-a 指定標簽號,-m 指定描述的內(nèi)容
git tag ? ? ? ? ? ? ? ? ? ? ? ? ? ?#查看所有標簽
git tag -d v0.3? ? ? ? ? ? #刪除標簽
git push origin v0.4 ?? ? ? ?#提交單個標簽
git push origin ?--tags ? ? ? #提交所有標簽
git tag -d v0.4? ? ? ? ? ? ?#刪除遠程標簽第一步
git push origin :refs/tags/v0.4 ?#刪除遠程標簽第二步
git config --global alias.st status ? #把status 配置成別名 st,其他命令設(shè)別名依此類推
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit" ?#日志別名配置
Git擴展技術(shù):Git flow工作流程?
這個工作流程大家可以去百度上搜索一下文章來看看,這個我目前也不懂,不過他是一個不錯的東西,他能夠規(guī)范化你的開發(fā)流程
轉(zhuǎn)載于:https://blog.51cto.com/yht1990/1832284
總結(jié)
- 上一篇: PS Tray Factory win7
- 下一篇: 后台清理 清理cookie_清理枯木