GIT

git初次使用

這里說個有趣的事,如果電腦上之前沒有g(shù)it,為了使用,去下載一個,初次使用git需要設(shè)置姓名和郵箱,每次提交至遠程,git都有記錄,知道是誰提交了什么

git config --global user.name “Jhon”  
git config --global user.email “johndoe@example.com”

設(shè)置好之后,以后所有的倉庫都不用再重新設(shè)置
還有一種情況就是電腦里的git有其他人使用過,那么需要把git默認用戶名及郵箱進行更改

首先,用config --list查看下當前有沒有默認的用戶名及郵箱

如果有,那么進行如下操作更改;

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)于版本控制,版本控制是一種記錄一個或若干個文件內(nèi)容,以便將來查閱特定版本修訂情況的系統(tǒng)。
版本控制有三種:本地版本控制系統(tǒng),集中式的版本控制系統(tǒng)(SVN)有一個服務(wù)器,如果A修改,從服務(wù)器拉過來,改完再上傳服務(wù)器,B可以再改。服務(wù)器是一個集中的中心節(jié)點,所有操作都必須提交到中心節(jié)點。缺點:斷網(wǎng),提交不了;服務(wù)器壞了,全沒有了。
分布式版本控制系統(tǒng)(Git):本地、遠程都有一個倉庫,兩個倉庫是一樣的,本地倉庫沒有問題,再提交至遠程倉庫。斷網(wǎng)時可先保存至本地倉庫,相對安全。

  • 可隨時查看之前版本,多人合作時,我改完,上傳,別人再接著改,可以簡單實現(xiàn)分支管理。
  • 速度快
  • 設(shè)計的原理簡單
  • 允許上千個并行分支
  • 分布式

git hub的注冊

1.
git hub的注冊.png

2.
GitHub登錄頁面.png
GitHub初次進入的頁面.png

3.##一般第一次進入git hub點擊新建一個項目就會進入如下頁面:


在遠程創(chuàng)建一個test的倉庫.png

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

image.png

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


新建一個倉庫.png

此步驟跟上面基本一致
填寫repository name ,點擊create repository


Create a New Repository.png

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


image.png
image.png

image.png

image.png

image.png

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


image.png

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

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

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


myc901225 blog1 我的博客.png
myc901225 blog1 我的博客.png
image.png
  1. 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.進行設(shè)置


image.png

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


image.png

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


image.png

github在本地創(chuàng)建倉庫,將遠程的信息克隆到本地,操作,再推送

  • 已提交的(mommitted)該文件已經(jīng)被安全地保存在本地數(shù)據(jù)庫中
  • 已修改(modified)修改了某個文件,但還沒有提交保存
  • 已暫存(staged)把已修改的文件放在下次提交時要保存的清單中
    1.選擇你的項目保存文件夾,
    cd test
    2.將遠程倉庫的內(nèi)容克隆至本地項目文件夾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 . 當前文件夾下的所有新增和修改都放入
    5.git status檢測是否是暫存區(qū)內(nèi)容
    6.將所有的變動放入本地倉庫
    git commit -am "add"(-a是所有的,m是備注 “”里面些什么都可以)
    7.推送
    git push origin master(第一次使用,其他可使用git push)已推送至遠程分支
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

若遠程的東西已修改,本地倉庫沒有修改,在推送之前,用git pull將遠程的倉庫和本地倉庫自動合并,并進入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進入VIM編輯器,可進行修改,保存退出后,git push即可
刪除文件
可用git add .
git commit -am "delete b.html"
git push
本地新建空文件夾,需要git init使本地文件夾初始化倉庫,如果起初沒有倉庫,這樣會覆蓋,用一個空文件夾覆蓋 .git文件夾即為倉庫
git remote add origin 遠程倉庫的地址,給遠程倉庫起名為origin,方便記憶
用的時候再本地即可 git push origin master
git remote -v查看本地庫里的遠程庫地址
git push -f origin master將本地倉庫,強制推送,覺得遠程沒有用,直接推送,可以覆蓋所有
git remote add
git remote remove abc刪除abc標簽
修改origin標簽對應(yīng)的地址
git remote set-url origin 新的地址
分支操作:實現(xiàn)了多個人同時開發(fā)多個功能,待各自開發(fā)完成后,合并主干,即為最新的。
git branch -a查看所有分支,紅色的遠程分支,藍色的是本地分支,*是當前所處分支
open可打開文件
git checkout master 切換到主干
不同分支展示的內(nèi)容是不同的
在本地創(chuàng)建一個分支,經(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
當自己和別人改同一個文件的同一個地方,在執(zhí)行g(shù)it pull時更新本地合并時會出現(xiàn)沖突
遠程有別人更改為index.html為“你好”,而本地自己更改index.html為“Hello”,提交時會產(chǎn)生沖突,git執(zhí)行自動合并時,不知道要怎么辦。選擇哪一個,需要手動打開文件,進行修改,修復(fù)沖突的方法就是 vim index.html 找到文件,并修改

git bash 命令行

pwd當前終端窗口所在位置,查看當前文件的路徑
ls 查看當前目錄下的文件,不包括隱藏文件
ls -a 查看當前千目錄下的文件,包括隱藏文件
ls -al查看當前目錄下的文件的詳細信息,包括隱藏文件
cd切換目錄 cd +文件名,下一級文件
cd ../上級文件
cd /c/projrct打開c盤project文件夾
cd code 打開code文件夾
cd ../css打開上級目錄的css文件夾
cd ~/Desktop 打開桌面上的,家文件
tocuch a.html新建一個a.html文件
rm a.html刪除a.html文件,但是不能刪除文件夾
rm -r tasks 刪除tasks文件夾,會有進一步提示是否刪除
rm -rf tasks強制刪除tasks文件夾,直接消失
mv a.html abc.html把a.html重命名為abc.html
mkdir www創(chuàng)建一個www的文件夾
cd /根目錄
~家目錄
clear是清空當前內(nèi)容

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容