前言:
Git作為分布式版本控制系統(tǒng)后控,是我們工作和開源代碼平臺(tái)項(xiàng)目管理最火的工具之一,基本上是每個(gè)入職的同學(xué)都要熟知和學(xué)習(xí)的。由于我以前的公司都是用的SVN走越,有時(shí)也會(huì)用的github客戶端,最近抽空來學(xué)習(xí)下Git的指令使用耻瑟,聽說這樣的操作比較騷哦
一旨指、Git下載
- Git下載地址:https://git-scm.com/downloads
- 這邊我在win10安裝,桌面右鍵打開Bash輸入界面匆赃,在本地創(chuàng)建我們的測(cè)試倉庫
cd ..
//進(jìn)入d盤創(chuàng)建我們的測(cè)試倉庫
cd d:
mkdir git_repository
cd git_repository/
//pwd打印當(dāng)前目錄地址
$ pwd
/d/git_repository
-
通過git init初始化淤毛,ls -ah顯示隱藏目錄即可看到相應(yīng)的文件,我們的git倉庫便創(chuàng)建好了
二算柳、Gti文件狀態(tài)生命周期和文件流向
1. Git文件狀態(tài)通過git status查詢低淡,分別是Untracked,Unmodified,Modified,staged,對(duì)應(yīng)關(guān)系如下圖
下面我們來驗(yàn)證瞬项,我這邊測(cè)試是我github上的一個(gè)demo蔗蹋,通過一個(gè)簡(jiǎn)單的txt文檔的創(chuàng)建修改和提交,來演示這四種生命狀態(tài):
-
創(chuàng)建新的文檔囱淋,識(shí)別味Untracked狀態(tài)
-
下面add a.txt猪杭,狀態(tài)編碼Umodified,顯示我們可以直接commit,add之后修改文檔妥衣,顯示味Modified狀態(tài)
-
commit 文件皂吮,再次查看,狀態(tài)回到了Unmodified,再次修改又回出現(xiàn)上一條的狀態(tài)結(jié)果
三税手、Git關(guān)聯(lián)&推送&回退&刪除&克隆操作以github為例
進(jìn)入我的github個(gè)人賬號(hào)蜂筹,new a Repositories,我的項(xiàng)目名稱:NativeRegisterDemo
-
關(guān)聯(lián)并上傳項(xiàng)目
//進(jìn)入項(xiàng)目工程目錄,初始化工程目錄為git倉庫 echo "# NativeRegisterDemo" >> README.md git init git add README.md git add . //此處為添加工程所有文件到git暫存區(qū) git commit -m "first commit" git remote add origin https://github.com/fmer/NativeRegisterDemo.git //關(guān)聯(lián)一個(gè)遠(yuǎn)程倉庫 git push -u origin master //要求輸入Github的賬號(hào)密碼芦倒,推送到主線
-
**修改提交: **README.md
git add README.md git commit -m "update my readme.md" git push -u origin master
-
查看提交日志:能看到我們的2次提交記錄艺挪,git log --pretty=oneline 命令顯示從最近到最遠(yuǎn)的提交日志,后綴"--pretty=oneline"為過濾信息兵扬,其中那一串
-
回退我們的上一個(gè)版本麻裳,回退后我們?cè)倏磍og歷史就只有一條提交記錄了
git reset --hard HEAD~1 //HEAD代表當(dāng)前版本,0-100代表往后的版本器钟,0為當(dāng)前津坑,1為上一個(gè)版本 cat README.md//果然已經(jīng)回退到上個(gè) first commit版本
-
參數(shù)含義:通過git reset --help查看
git reset [<mode>] [<commit>] This form resets the current branch head to <commit> and possibly updates the index (resetting it to the tree of <commit>) and the working tree depending on <mode>. If <mode> is omitted, defaults to "--mixed". The <mode> must be one of the following: --soft Does not touch the index file or the working tree at all (but resets the head to <commit>, just like all modes do). This leaves all your changed files "Changes to be committed", as git status would put it. --mixed Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action. If -N is specified, removed paths are marked as intent-to-add (see git-add(1)). --hard Resets the index and working tree. Any changes to tracked files in the working tree since <commit> are discarded. --merge Resets the index and updates the files in the working tree that are different between <commit> and HEAD, but keeps those which are different between the index and working tree (i.e. which have changes which have not been added). If a file that is different between <commit> and the index has unstaged changes, reset is aborted. In other words, --merge does something like a git read-tree -u -m <commit>, but carries forward unmerged index entries. --keep Resets index entries and updates files in the working tree that are different between <commit> and HEAD. If a file that is different between <commit> and HEAD has local changes, reset is aborted.
-
-
如果我們想再回到剛才"update my readme.txt"的版本,也就是在當(dāng)前的版本上前進(jìn)到未來版本,我們需要通過git reflog 查看操作記錄傲霸,然后再通過reset 指定歷史版本的commit id即可推進(jìn)到我們最新的版本
git reflog git reset --hard 4cabf0bd3fb24768bc108ef5bc81fa581d115fee
撤銷修改:git checkout -- file国瓮,將文件撤回到上一個(gè)commit或者add狀態(tài)或者 通過git reset HEAD file將暫存區(qū)的修改撤回到工作區(qū)
刪除文件,rm a.txt 刪除本地 git rm a.txt 本地庫刪除 然后commit
-
從遠(yuǎn)程庫克隆,上面說了推送本地項(xiàng)目到我們的github倉庫乃摹,現(xiàn)在介紹如何克隆項(xiàng)目禁漓,我們還是這個(gè)項(xiàng)目,新建一個(gè)文件夾test
git clone git@github.com:fmer/NativeRegisterDemo.git
-
ssh的使用:以上一條clone為例孵睬,為了避免https麻煩的密碼輸入播歼,我們可以選擇使用ssh的方式,需要在本地和遠(yuǎn)程倉庫關(guān)聯(lián)一個(gè)ssh key掰读,否則會(huì)被權(quán)限拒絕秘狞,如圖操作。
- 本地生成ssh的rsa密鑰:ssh-keygen -t rsa -C fmer_lin@foxmail.com蹈集,會(huì)在本地生成一個(gè).ssh文件夾烁试,里面包含文件id_rsa和id_rsa.pub
- 關(guān)聯(lián)遠(yuǎn)程庫,以github為例:在https://github.com/settings/keys頁面新建一個(gè)SSH key拢肆,將本地id_rsa.pub內(nèi)容復(fù)制進(jìn)去减响,具體操作如下圖:
四、Git項(xiàng)目分支創(chuàng)建管理
創(chuàng)建分支:git checkout -b branch_1 "git checkout -b "---創(chuàng)建并選擇分支
查看分支:git branch
-
選擇分支:git checkout master
-
合并分支:git merge branch_1 郭怪,可以通過指定參數(shù)--no-ff支示,如git merge --no-ff branch_1 禁用fast forward模式,默認(rèn)味分支改動(dòng)模式鄙才,這樣方便知道改動(dòng)內(nèi)容
-
合并沖突處理:再次在brach_1的a.txt添加內(nèi)容再合并颂鸿,會(huì)發(fā)現(xiàn)出現(xiàn)合并沖突,下圖可以看到分別在主線和支線提交相同文檔造成的沖突問題攒庵,合并后可通過指令查看:git log --graph --pretty=oneline --abbrev-commit
LP@lp MINGW64 /d/ipcdemo/test/NativeRegisterDemo (master) $ git checkout branch_1 M a.txt Switched to branch 'branch_1' LP@lp MINGW64 /d/ipcdemo/test/NativeRegisterDemo (branch_1) $ echo modify_branch>merge.txt LP@lp MINGW64 /d/ipcdemo/test/NativeRegisterDemo (branch_1) $ git add merge.txt warning: LF will be replaced by CRLF in merge.txt. The file will have its original line endings in your working directory. LP@lp MINGW64 /d/ipcdemo/test/NativeRegisterDemo (branch_1) $ git commit -m "modify from branch_1" [branch_1 cc45372] modify from branch_1 warning: LF will be replaced by CRLF in merge.txt. The file will have its original line endings in your working directory. 1 file changed, 1 insertion(+) create mode 100644 merge.txt LP@lp MINGW64 /d/ipcdemo/test/NativeRegisterDemo (branch_1) $ git checkout master M a.txt Switched to branch 'master' Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) LP@lp MINGW64 /d/ipcdemo/test/NativeRegisterDemo (master) $ echo modify_master>>merge.txt LP@lp MINGW64 /d/ipcdemo/test/NativeRegisterDemo (master) $ git add merge.txt warning: LF will be replaced by CRLF in merge.txt. The file will have its original line endings in your working directory. LP@lp MINGW64 /d/ipcdemo/test/NativeRegisterDemo (master) $ git commit -m "modify from master" [master 71e1e12] modify from master warning: LF will be replaced by CRLF in merge.txt. The file will have its original line endings in your working directory. 1 file changed, 1 insertion(+) create mode 100644 merge.txt LP@lp MINGW64 /d/ipcdemo/test/NativeRegisterDemo (master) $ git merge branch_1 Auto-merging merge.txt CONFLICT (add/add): Merge conflict in merge.txt Automatic merge failed; fix conflicts and then commit the result. LP@lp MINGW64 /d/ipcdemo/test/NativeRegisterDemo (master|MERGING) $ cat merge.txt <<<<<<< HEAD modify_master ======= modify_branch >>>>>>> branch_1 LP@lp MINGW64 /d/ipcdemo/test/NativeRegisterDemo (master) $ git log --graph --pretty=oneline --abbrev-commit * 71d1f1a fix my abort |\ | * cc45372 modify from branch_1 * | 71e1e12 modify from master |/ * 0c2dc77 add from branch_1 tes * ab377d0 remove a.txt * f7b229e a.txt * 4cabf0b update my readme.md * bd26f65 first commit
刪除分支:git branch -d brach_1
-
分支狀態(tài)保管:git stash,保存當(dāng)前的分支狀態(tài)即工作內(nèi)容嘴纺,便地切換到其他分支工作,它的保存歷史作為一個(gè)list存儲(chǔ)浓冒,可以保存多次
- git stash list查看列表
- git stash apply 恢復(fù)栽渴,通過git stash apply stash@{x} x為保管列表id,指定恢復(fù)
- git stash drop 取第一個(gè)恢復(fù)并刪除
- git stash drop 刪除
關(guān)聯(lián)本地分支和遠(yuǎn)程分支:git branch --set-upstream-to <branch-name> origin/<branch-name>
查看遠(yuǎn)程庫信息:git remote -v
添加標(biāo)簽: git tag v1.0.0 在當(dāng)前的commit上打標(biāo)簽裆蒸,或者在后面指定commit id指定打在相應(yīng)的commit記錄上如:git tag v1.0.1 fsfag143
刪除本地git倉庫:find . -name ".git" | xargs rm -Rf 在線:rm -rf https://github.com/NeroSolomon/VLearning.git
四熔萧、在具體項(xiàng)目協(xié)作中如何整合版本
- 在與同事協(xié)作開發(fā)時(shí)糖驴,如果出現(xiàn)同事事先提交了代碼僚祷,我們?cè)偃ush就會(huì)出現(xiàn)推送失敗的情況,這是我們需要先抓取遠(yuǎn)程倉庫代碼贮缕,在本地合并后再提交
- 要注意的時(shí)辙谜,抓取遠(yuǎn)程分支需要關(guān)聯(lián)本地和遠(yuǎn)程分支,參考上節(jié)第8
- 在抓取代碼再提交中感昼,學(xué)會(huì)使用rebase去把本地的分支提交記錄歸檔成一條主線
- 在提交代碼或者版本時(shí)装哆,打上標(biāo)簽,這樣能讓大家簡(jiǎn)單明了的瀏覽history,上節(jié)第10
好了,一些常用的指令操作已經(jīng)學(xué)完了蜕琴,快去練習(xí)一下吧萍桌。git的操作遠(yuǎn)遠(yuǎn)不止這些,在開發(fā)過程中可以參考他的中文指導(dǎo)文檔:
https://git-scm.com/book/zh/v1/%E8%B5%B7%E6%AD%A5