首先確定電腦安裝了git
輸入git
會(huì)產(chǎn)生提示:
如果沒有安裝,那么就網(wǎng)上搜索下載完成安裝.
可以使用命令行安裝崖飘,也可以下載工具包安裝
確定文件保存在自己的倉庫哪個(gè)地方
命令行:
cd (不會(huì)路徑列肢,把文件拖動(dòng)到這里)
添加文件到暫緩區(qū)
針對(duì)單一文件的添加:git add (文件名)
如果提示密碼郵箱
global的作用
配置全局的用戶名和密碼— 其他地方可以不配置
git config —global user.name “why"
git config —global user.email “why@163.com"
正對(duì)整個(gè)項(xiàng)目的文件添加 git add .
將暫緩區(qū)的文件添加到分支
git commit -m "文件注釋扩所,也就是這些添加你要做什么"
查看目前暫緩區(qū)的狀態(tài)
git status
回到歷史中
穿梭前菊值,用git log
可以查看提交歷史抄淑,以便確定要回退到哪個(gè)版本猛遍。
回到未來
要重返未來馋记,用git reflog查看命令歷史号坡,以便確定要回到未來的哪個(gè)版本。
commit_id抗果。
版本回退:
git reset --hard (版本號(hào))
HEAD指向的版本就是當(dāng)前版本筋帖,因此,Git允許我們?cè)诎姹镜臍v史之間穿梭冤馏,使用命令git reset --hard
其他常用的git 命令
你做了修改后日麸,(還沒有對(duì)他進(jìn)行g(shù)it add .操作)放棄修改
git checkout -- file
例如:(對(duì)那個(gè)文件放棄修改)
git checkout -- readme.txt
撤銷已經(jīng)存在暫緩區(qū)的文件
git reset HEAD file可以把暫存區(qū)的修改撤銷掉(unstage)
假設(shè)你改錯(cuò)了東西,還從暫存區(qū)提交到了版本庫,直接使用上面的回到歷史中
一般團(tuán)隊(duì)開發(fā):要在分支進(jìn)行
git checkout -b dev
這個(gè)命令加上 -b 參數(shù)表示創(chuàng)建并切換相當(dāng)于
git branch dev
git checkout dev
查看當(dāng)前分支
git branch
這里在dev分支修改文件后逮光,提交(此處修改后的文件提交上去只是在dev分支上)
切換回主分支
git checkout master
git merge:合并指定分支到當(dāng)前分支
git merge dev
刪除dev分支
git branch -d dev
查看branch
git branch
上述分支主支一覽
Git鼓勵(lì)大量使用分支
查看分支:git branch
創(chuàng)建分支:git branch <name>
切換分支:git checkout <name>
創(chuàng)建+切換分支:git checkout -b <name>
合并某分支到當(dāng)前分支:git merge <name>
通常代箭,合并分支時(shí),如果可能涕刚,Git會(huì)用Fast forward模式嗡综,但這種模式下,刪除分支后杜漠,會(huì)丟掉分支信息
合并分支時(shí)极景,加上--no-ff參數(shù)就可以用普通模式合并,合并后的歷史有分支驾茴,能看出來曾經(jīng)做過合并盼樟,而fast forward合并就看不出來曾經(jīng)做過合并
git merge --no-ff -m "merge with no-ff" dev
刪除分支:git branch -d <name>
·1.推送分支 (這里origin是git默認(rèn)的主分支)
git push origin master
·2.推送其他分支
git push origin dev
例子簡介
多人協(xié)作的工作模式通常是這樣:
首先,可以試圖用git push origin branch-name
推送自己的修改锈至;
如果推送失敗晨缴,則因?yàn)檫h(yuǎn)程分支比你的本地更新,需要先用git pull
試圖合并峡捡;
如果合并有沖突击碗,則解決沖突,并在本地提交们拙;
沒有沖突或者解決掉沖突后稍途,再用git push origin branch-name
推送就能成功!
如果git pull
提示“no tracking information”睛竣,則說明本地分支和遠(yuǎn)程分支的鏈接關(guān)系沒有創(chuàng)建晰房,用命令git branch --set-upstream branch-name origin/branch-name。
這就是多人協(xié)作的工作模式射沟,一旦熟悉了,就非常簡單与境。
多人協(xié)作時(shí)验夯,大家都會(huì)往master和dev分支上推送各自的修改。
現(xiàn)在摔刁,模擬一個(gè)你的小伙伴挥转,可以在另一臺(tái)電腦(注意要把SSH Key添加到GitHub)或者同一臺(tái)電腦的另一個(gè)目錄下克隆:
$ git clone git@github.com:suny/learngit.git
Cloning into 'learngit'...
remote: Counting objects: 46, done.
remote: Compressing objects: 100% (26/26), done.
remote: Total 46 (delta 16), reused 45 (delta 15)
Receiving objects: 100% (46/46), 15.69 KiB | 6 KiB/s, done.
Resolving deltas: 100% (16/16), done.
當(dāng)你的小伙伴從遠(yuǎn)程庫clone時(shí),默認(rèn)情況下绑谣,你的小伙伴只能看到本地的master分支党窜。不信可以用git branch命令看看:
git branch
`* mas
現(xiàn)在,你的小伙伴要在dev分支上開發(fā)借宵,就必須創(chuàng)建遠(yuǎn)程origin的dev分支到本地幌衣,于是他用這個(gè)命令創(chuàng)建本地dev分支:
git checkout -b dev origin/dev
現(xiàn)在,他就可以在dev上繼續(xù)修改壤玫,然后豁护,時(shí)不時(shí)地把dev分支push到遠(yuǎn)程:
git commit -m "add /usr/bin/env"
[dev 291bea8] add /usr/bin/env
3 file changed, 1 insertion(+)
git push origin dev
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 359 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:suny/learngit.git
fc38031..291bea8 dev -> dev
你的小伙伴已經(jīng)向origin/dev分支推送了他的提交,而碰巧你也對(duì)同樣的文件作了修改欲间,并試圖推送:
git add hello.py
gi
git commit -m "add coding: utf-8"
[dev bd6ae48] add coding: utf-8
3 file changed, 1 insertion(+)
git push origin dev
To git@github.com:suny/learngit.git
! [rejected] dev -> dev (non-fast-forward)
error: failed to push some refs to 'git@github.com:suny/learngit.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
推送失敗楚里,因?yàn)槟愕男』锇榈淖钚绿峤缓湍阍噲D推送的提交有沖突,解決辦法也很簡單猎贴,Git已經(jīng)提示我們班缎,先用git pull把最新的提交從origin/dev抓下來,然后她渴,在本地合并达址,解決沖突,再推送:
git pull
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0)
Unpacking objects: 100% (4/4), done.
From github.com:michaelliao/learngit
fc38031..291bea8 dev -> origin/dev
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 dev origin/<branch>
git pull也失敗了惹骂,原因是沒有指定本地dev分支與遠(yuǎn)程origin/dev分支的鏈接苏携,根據(jù)提示,設(shè)置dev和origin/dev的鏈接:
多人協(xié)作
當(dāng)你從遠(yuǎn)程倉庫克隆時(shí)对粪,實(shí)際上Git自動(dòng)把本地的master分支和遠(yuǎn)程的master分支對(duì)應(yīng)起來了右冻,并且,遠(yuǎn)程倉庫的默認(rèn)名稱是origin著拭。
要查看遠(yuǎn)程庫的信息纱扭,用git remote:
git remote
origin
或者,用git remote -v顯示更詳細(xì)的信息:
git remote -v
origin git@github.com:suny/learngit.git (fetch)
origin git@github.com:suny/learngit.git (push)
上面顯示了可以抓取和推送的origin的地址儡遮。如果沒有推送權(quán)限乳蛾,就看不到push的地址。
推送分支
推送分支鄙币,就是把該分支上的所有本地提交推送到遠(yuǎn)程庫肃叶。推送時(shí),要指定本地分支十嘿,這樣因惭,Git就會(huì)把該分支推送到遠(yuǎn)程庫對(duì)應(yīng)的遠(yuǎn)程分支上:
git push origin master
如果要推送其他分支,比如dev绩衷,就改成:
git push origin dev
master分支是主分支蹦魔,因此要時(shí)刻與遠(yuǎn)程同步激率;
dev分支是開發(fā)分支,團(tuán)隊(duì)所有成員都需要在上面工作勿决,所以也需要與遠(yuǎn)程同步乒躺;
抓取分支
多人協(xié)作時(shí),大家都會(huì)往master和dev分支上推送各自的修改低缩。
現(xiàn)在嘉冒,模擬一個(gè)你的小伙伴,可以在另一臺(tái)電腦(注意要把SSH Key添加到GitHub)或者同一臺(tái)電腦的另一個(gè)目錄下克卤碇啤:
git clone git@github.com:suny/learngit.git
Cloning into 'learngit'...
remote: Counting objects: 46, done.
remote: Compressing objects: 100% (20/20), done.
remote: Total 46 (delta 16), reused 45 (delta 15)
Receiving objects: 100% (46/46), 105.69 KiB | 25 KiB/s, done.
Resolving deltas: 100% (19/19), done.
當(dāng)你的小伙伴從遠(yuǎn)程庫clone時(shí)健爬,默認(rèn)情況下,你的小伙伴只能看到本地的master分支么介。不信可以用git branch命令看看:
git branch
- master
現(xiàn)在娜遵,你的小伙伴要在dev分支上開發(fā),就必須創(chuàng)建遠(yuǎn)程origin的dev分支到本地壤短,于是他用這個(gè)命令創(chuàng)建本地dev分支:
git checkout -b dev origin/dev
現(xiàn)在设拟,他就可以在dev上繼續(xù)修改,然后久脯,時(shí)不時(shí)地把dev分支push到遠(yuǎn)程:
git commit -m "add /usr/bin/env"
[dev 291bea8] add /usr/bin/env
1 file changed, 1 insertion(+)
git push origin dev
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 349 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:suny/learngit.git fc38031..291bea8 dev -> dev
你的小伙伴已經(jīng)向origin/dev分支推送了他的提交纳胧,而碰巧你也對(duì)同樣的文件作了修改,并試圖推送:
$git add hello.py
$ git commit -m "add coding: utf-8"
[dev bd6ae48] add coding: utf-8
1 file changed, 1 insertion(+)
$ git push origin dev
To git@github.com:suny/learngit.git
! [rejected] dev -> dev (non-fast-forward)
error: failed to push some refs to 'git@github.com:suny/learngit.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
推送失敗帘撰,因?yàn)槟愕男』锇榈淖钚绿峤缓湍阍噲D推送的提交有沖突跑慕,解決辦法也很簡單,Git已經(jīng)提示我們摧找,先用git pull把最新的提交從origin/dev抓下來核行,然后,在本地合并蹬耘,解決沖突芝雪,再推送:
git pull
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0)
Unpacking objects: 100% (3/3), done.
From github.com:suny/learngit
fc38031..291bea8 dev -> origin/dev
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 dev origin/<branch>
git pull也失敗了,原因是沒有指定本地dev分支與遠(yuǎn)程origin/dev分支的鏈接综苔,根據(jù)提示惩系,設(shè)置dev和origin/dev的鏈接:
git branch --set-upstream dev origin/dev
Branch dev set up to track remote branch dev from origin.
再pull:
$
git pull
Auto-merging hello.py
CONFLICT (content): Merge conflict in hello.py
Automatic merge failed; fix conflicts and then commit the result.
這回git pull成功,但是合并有沖突如筛,需要手動(dòng)解決堡牡,解決的方法和分支管理中的解決沖突完全一樣。解決后杨刨,提交悴侵,再push:
$ git commit -m "merge & fix hello.py"
[dev adca45d] merge & fix hello.py
$ git push origin dev
Counting objects: 10, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (9/9), 747 bytes, done.
Total 6 (delta 0), reused 0 (delta 0)
To git@github.com:suny/learngit.git
291bea8..adca45d dev -> dev