git初次使用
這里說個(gè)有趣的事,如果電腦上之前沒有g(shù)it,為了使用,去下載一個(gè),初次使用git需要設(shè)置姓名和郵箱,每次提交至遠(yuǎn)程,git都有記錄,知道是誰提交了什么
git config --global user.name “Jhon”
git config --global user.email “johndoe@example.com”
設(shè)置好之后,以后所有的倉庫都不用再重新設(shè)置
還有一種情況就是電腦里的git有其他人使用過,那么需要把git默認(rèn)用戶名及郵箱進(jìn)行更改
首先,用config --list查看下當(dāng)前有沒有默認(rèn)的用戶名及郵箱
如果有,那么進(jìn)行如下操作更改;
git config --gloabl --replace-all user.name "Mary"
git config --global --replace-all user.email "123456@qq.com"
那么git的的用戶名及密碼就已更改,在后期工作中,如果哪天你的代碼造成重大 bug,方便產(chǎn)品經(jīng)理來砍你
Git使用優(yōu)勢
關(guān)于版本控制,版本控制是一種記錄一個(gè)或若干個(gè)文件內(nèi)容,以便將來查閱特定版本修訂情況的系統(tǒng)。
版本控制有三種:本地版本控制系統(tǒng),集中式的版本控制系統(tǒng)(SVN)有一個(gè)服務(wù)器,如果A修改,從服務(wù)器拉過來,改完再上傳服務(wù)器,B可以再改。服務(wù)器是一個(gè)集中的中心節(jié)點(diǎn),所有操作都必須提交到中心節(jié)點(diǎn)。缺點(diǎn):斷網(wǎng),提交不了;服務(wù)器壞了,全沒有了。
分布式版本控制系統(tǒng)(Git):本地、遠(yuǎn)程都有一個(gè)倉庫,兩個(gè)倉庫是一樣的,本地倉庫沒有問題,再提交至遠(yuǎn)程倉庫。斷網(wǎng)時(shí)可先保存至本地倉庫,相對安全。
- 可隨時(shí)查看之前版本,多人合作時(shí),我改完,上傳,別人再接著改,可以簡單實(shí)現(xiàn)分支管理。
- 速度快
- 設(shè)計(jì)的原理簡單
- 允許上千個(gè)并行分支
- 分布式
git hub的注冊
1.
2.


3.##一般第一次進(jìn)入git hub點(diǎn)擊新建一個(gè)項(xiàng)目就會(huì)進(jìn)入如下頁面:

創(chuàng)建一個(gè)遠(yuǎn)程庫后,發(fā)現(xiàn)右側(cè)的clone 或者download協(xié)議一般默認(rèn)是https,需更改為ssh協(xié)議

此外還有一種,是之前有g(shù)it hub 賬號,進(jìn)入之后,在右中部有一個(gè) New Respository

此步驟跟上面基本一致
填寫repository name ,點(diǎn)擊create repository

4.https(每次都需要用戶名和密碼),一般選擇SSH協(xié)議(在使用過程中,不需要輸入用戶名和密碼,需要設(shè)置公鑰和私鑰對,公鑰進(jìn)行匹配 ssh-keygen -t rsa -b 4096 -C+地址公鑰給別人,私鑰留給自己,進(jìn)行匹配)
點(diǎn)擊Git Hub里的個(gè)人設(shè)置settings,創(chuàng)造新的SSH密鑰,





5.復(fù)制內(nèi)容至Git Bash,之后一直點(diǎn)擊回車鍵即可

6.此時(shí)已生成ssh.pub文件,切換cd到家目錄下的.ssh文件,ls查看文件夾下的內(nèi)容,復(fù)制cat id_rsa.pub

7.復(fù)制以上內(nèi)容,到git hub上

8.git clone 將Git Hub上的blog文件(前期在github 上新建的倉庫)拷貝至本地倉庫



- cd blog1,切換到blog1文件,ls blog1查看blog1下的文件,vim index.html,并在vim編輯內(nèi)容并保存,git add . 將index.html添加到本地倉庫,git commit -am "add",提交,git push origin master推送到Git Hub.
可以將Git Hub上設(shè)置為網(wǎng)頁代碼,可讓別人看的
1.進(jìn)行設(shè)置

2.將GitHub Pages設(shè)置為master branch 并保存

3.即可以顯示代碼及頁面

github在本地創(chuàng)建倉庫,將遠(yuǎn)程的信息克隆到本地,操作,再推送
- 已提交的(mommitted)該文件已經(jīng)被安全地保存在本地?cái)?shù)據(jù)庫中
- 已修改(modified)修改了某個(gè)文件,但還沒有提交保存
- 已暫存(staged)把已修改的文件放在下次提交時(shí)要保存的清單中
1.選擇你的項(xiàng)目保存文件夾,
cd test
2.將遠(yuǎn)程倉庫的內(nèi)容克隆至本地項(xiàng)目文件夾test
git clone git@github.com:Super-Mary/test.git
3.添加文件夾及文件
touch a.html 添加文件
mkdir abc 添加文件夾
echo "hello" > a.html,將“hello”導(dǎo)入a.html
4.將文件放入暫存區(qū)
git add .當(dāng)前文件夾下的所有新增和修改都放入
5.git status檢測是否是暫存區(qū)內(nèi)容
6.將所有的變動(dòng)放入本地倉庫
git commit -am "add"(-a是所有的,m是備注 “”里面些什么都可以)
7.推送
git push origin master(第一次使用,其他可使用git push)已推送至遠(yuǎn)程分支
Administrator@PC-20170629JWFL MINGW64 ~
$ git config --global myc901225
error: key does not contain a section: myc901225
Administrator@PC-20170629JWFL MINGW64 ~
$ git config --global user.name myc901225
Administrator@PC-20170629JWFL MINGW64 ~
$ git config --global user.email 572280245@qq.com
Administrator@PC-20170629JWFL MINGW64 ~
$ pwd
/c/Users/Administrator
Administrator@PC-20170629JWFL MINGW64 ~
$ mkdir test
Administrator@PC-20170629JWFL MINGW64 ~
$ cd test
Administrator@PC-20170629JWFL MINGW64 ~/test
$ git clone git@github.com:myc901225/blog2.git
Cloning into 'blog2'...
The authenticity of host 'github.com (192.30.255.113)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': yes
Warning: Permanently added 'github.com,192.30.255.113' (RSA) to the list of known hosts.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Administrator@PC-20170629JWFL MINGW64 ~/test
$ git clone git@github.com:myc901225/blog2.git
Cloning into 'blog2'...
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Administrator@PC-20170629JWFL MINGW64 ~/test
$ ssh-keygen -t rsa -b 4096 -C "572280245@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):
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:e09AoJvXEzl32UT7fIrAhtaBSM14PfVa8bMKGqlFXmc 572280245@qq.com
The key's randomart image is:
+---[RSA 4096]----+
| .+.. .. ..o|
| ..o+oo. . *.|
| o...*o.E=oo|
| oo*o=o+ .+|
| o S=O.. .+|
| oooo+.....|
| .... o.. |
| . o |
| . |
+----[SHA256]-----+
Administrator@PC-20170629JWFL MINGW64 ~/test
$ ls
Administrator@PC-20170629JWFL MINGW64 ~/test
$ pwd
/c/Users/Administrator/test
Administrator@PC-20170629JWFL MINGW64 ~/test
$ cd ~/.ssh
Administrator@PC-20170629JWFL MINGW64 ~/.ssh
$ ls
id_rsa id_rsa.pub known_hosts
Administrator@PC-20170629JWFL MINGW64 ~/.ssh
$ cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCzG23/s2MBGIowxWw992DSdvHmONicL+dtxxogfcgXFAGuc5KIpdZlVq5gw/45OHZHbHjj0t4DhOxXvhn1GCgA44tA678+CEwMAOi5qXa0LfMY4EfaE6k0gfMVhv+AB01Yimkmn6sb02hUEmONyVpNoG+TQbvRkmEMIT3dI8pAVBxK44lqrlkiiljxxIa5ErHm+MnhAtlEWyjBxD+AM87lmpRy3yfudwC3zwfO1aLyX6HqIyvcDOsKxMz0MpUp6w9P+z1C5JcBe/pkOoycfNs1YUEJh9WNfyicgejJxriJPZIDsDKklDd8pS2Vqwu3ur1vgrKtQlqlsxweWhjm48h+mfErG4OxCy+b4HyT9fDUBULZ5F1hiQ4Uc/WuuuCVa9Z93Pt+DnZrDmfiOvMK25pI4B6BmH3dpMgI6cm4U2LCK3N2lrGke7rg++PSacHYQuYFXi6gMZEd3/6hnehHQr+sQ98js5jAPKgfkeVWIV5DJHImwZ2kJsd6/aOEaK18rWkUmqkMEaSrR0Df4nefOKJ8I1+o7qGpc7o4VL7ih6SJQSJR3iATTDtLMhrdJcMszHfbpipuZBcxpCjGeLdYfbspBh6Zpg0LSeD+zsgDnYgCf7jgNFEF14EXOJpMNVT9pd6fJRvpcWXwDbiL6MB/J7bIVX40/W4DIb7FO7MkVNJecQ== 572280245@qq.com
Administrator@PC-20170629JWFL MINGW64 ~/.ssh
$ cd ../
Administrator@PC-20170629JWFL MINGW64 ~
$ pwd
/c/Users/Administrator
Administrator@PC-20170629JWFL MINGW64 ~
$ cd test
Administrator@PC-20170629JWFL MINGW64 ~/test
$ mkdir blog3
Administrator@PC-20170629JWFL MINGW64 ~/test
$ git clone git@github.com:myc901225/blog2.git
Cloning into 'blog2'...
Warning: Permanently added the RSA host key for IP address '192.30.255.112' to the list of known hosts.
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
Administrator@PC-20170629JWFL MINGW64 ~/test
$ touch 重識markdown.md
Administrator@PC-20170629JWFL MINGW64 ~/test
$ vim 重識markdown.md
Administrator@PC-20170629JWFL MINGW64 ~/test
$ git add .
fatal: Not a git repository (or any of the parent directories): .git
Administrator@PC-20170629JWFL MINGW64 ~/test
$ cd blog2
Administrator@PC-20170629JWFL MINGW64 ~/test/blog2 (master)
$ git add .
Administrator@PC-20170629JWFL MINGW64 ~/test/blog2 (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: "\351\207\215\350\257\206markdown.md"
Administrator@PC-20170629JWFL MINGW64 ~/test/blog2 (master)
$ git commit -am "add"
[master 15b30c1] add
1 file changed, 1 insertion(+)
create mode 100644 "\351\207\215\350\257\206markdown.md"
Administrator@PC-20170629JWFL MINGW64 ~/test/blog2 (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), 308 bytes | 308.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:myc901225/blog2.git
dbd094a..15b30c1 master -> master
若遠(yuǎn)程的東西已修改,本地倉庫沒有修改,在推送之前,用git pull將遠(yuǎn)程的倉庫和本地倉庫自動(dòng)合并,并進(jìn)入VIM編輯器的命令模式,沒有問題直接退出保存即可,再重新執(zhí)行g(shù)it add . git commit -am "add" git push即可。
修改刪除文件
mkdir abc創(chuàng)建abc文件夾
cd abc 在abc文件夾下操作
touch a.html 新建a.html文件
vim a.md 編輯保存退出,
git stastus
git add .
git commit -a進(jìn)入VIM編輯器,可進(jìn)行修改,保存退出后,git push即可
刪除文件
可用git add .
git commit -am "delete b.html"
git push
本地新建空文件夾,需要git init使本地文件夾初始化倉庫,如果起初沒有倉庫,這樣會(huì)覆蓋,用一個(gè)空文件夾覆蓋 .git文件夾即為倉庫
git remote add origin 遠(yuǎn)程倉庫的地址,給遠(yuǎn)程倉庫起名為origin,方便記憶
用的時(shí)候再本地即可 git push origin master
git remote -v查看本地庫里的遠(yuǎn)程庫地址
git push -f origin master將本地倉庫,強(qiáng)制推送,覺得遠(yuǎn)程沒有用,直接推送,可以覆蓋所有
git remote add
git remote remove abc刪除abc標(biāo)簽
修改origin標(biāo)簽對應(yīng)的地址
git remote set-url origin 新的地址
分支操作:實(shí)現(xiàn)了多個(gè)人同時(shí)開發(fā)多個(gè)功能,待各自開發(fā)完成后,合并主干,即為最新的。
git branch -a查看所有分支,紅色的遠(yuǎn)程分支,藍(lán)色的是本地分支,*是當(dāng)前所處分支
open可打開文件
git checkout master 切換到主干
不同分支展示的內(nèi)容是不同的
在本地創(chuàng)建一個(gè)分支,經(jīng)過修改,再合并
git branch dev
git checkout dev
touch a.md
git add .
git commit -am "add a.md"
git push origin dev
git checkout master
git merge dev
git push origin master
當(dāng)自己和別人改同一個(gè)文件的同一個(gè)地方,在執(zhí)行g(shù)it pull時(shí)更新本地合并時(shí)會(huì)出現(xiàn)沖突
遠(yuǎn)程有別人更改為index.html為“你好”,而本地自己更改index.html為“Hello”,提交時(shí)會(huì)產(chǎn)生沖突,git執(zhí)行自動(dòng)合并時(shí),不知道要怎么辦。選擇哪一個(gè),需要手動(dòng)打開文件,進(jìn)行修改,修復(fù)沖突的方法就是 vim index.html 找到文件,并修改
git bash 命令行
pwd當(dāng)前終端窗口所在位置,查看當(dāng)前文件的路徑
ls 查看當(dāng)前目錄下的文件,不包括隱藏文件
ls -a 查看當(dāng)前千目錄下的文件,包括隱藏文件
ls -al查看當(dāng)前目錄下的文件的詳細(xì)信息,包括隱藏文件
cd切換目錄 cd +文件名,下一級文件
cd ../上級文件
cd /c/projrct打開c盤project文件夾
cd code 打開code文件夾
cd ../css打開上級目錄的css文件夾
cd ~/Desktop 打開桌面上的,家文件
tocuch a.html新建一個(gè)a.html文件
rm a.html刪除a.html文件,但是不能刪除文件夾
rm -r tasks 刪除tasks文件夾,會(huì)有進(jìn)一步提示是否刪除
rm -rf tasks強(qiáng)制刪除tasks文件夾,直接消失
mv a.html abc.html把a(bǔ).html重命名為abc.html
mkdir www創(chuàng)建一個(gè)www的文件夾
cd /根目錄
~家目錄
clear是清空當(dāng)前內(nèi)容