git的使用

git的使用教程

安裝地址:http://git-scm.com

image-20200426134229696

1、安裝完之后,右鍵git bash here 輸入git --version

image-20200426134709514

當(dāng)看到版本號(hào)的時(shí)候,說明已經(jīng)安裝成功。

2、設(shè)置用戶名和郵箱,盡量和github賬號(hào)一致

git config --global user.name topljd(您的用戶名)
git config --global user.email 820230548@qq.com(您的郵箱)

image-20200426135053660

3、創(chuàng)建工作區(qū),在文件夾內(nèi)右鍵 git bash

image-20200426135326667

創(chuàng)建工作區(qū)的時(shí)候,要在想要?jiǎng)?chuàng)建的文件夾內(nèi)!

如果我要在G:\git下創(chuàng)建工作區(qū),.git是隱藏的文件夾,里面的文件不用管就可以了!

image-20200426135531534

git的使用,舉例

  • 1、創(chuàng)建readme.txt文件,并增加到緩存區(qū)

    git add readme.txt //將readme.txt這個(gè)文件增加到 暫存區(qū)
    git add .          //這個(gè) 點(diǎn) 表示當(dāng)前目錄下的所有文件
    
    
  • 2、commit提交到版本區(qū)

    git commit -m "1、添加readme.txt文件"  //后面引號(hào)內(nèi)的內(nèi)容為 注釋
    
    
image-20200426140739260
  • 3、推送到遠(yuǎn)程的服務(wù)器

    git remote add origin https://github.com/topljd/blog.git    遠(yuǎn)程連接倉(cāng)庫(kù)
    git push -u origin master        推送到服務(wù)器
    
    
    image-20200426142851612
    image-20200426144228997
  • 4、其他

    git log        //查看記錄
    
    
    image-20200426140856069
    git status        //查看當(dāng)前狀態(tài)
    
    
    image-20200426141028278

二、在github上創(chuàng)建服務(wù)器

github地址,注冊(cè)賬號(hào) 地址:https://github.com

  • 創(chuàng)建倉(cāng)庫(kù)

    //create a new repository on the command line
    git init        創(chuàng)建git項(xiàng)目
    git add readme.txt        將版本說明添加到 暫存庫(kù)
    git commit -m 'first commit'    提交到版本庫(kù),后面的為說明注釋
    git remote add origin https://github.com/topljd/blog.git    遠(yuǎn)程連接倉(cāng)庫(kù)
    git push -u origin master        推送到服務(wù)器
    
    
  • git log

    image-20200426144614810

    git log --pretty=online 6a59ff31 //查看版本

    git reset --hard 6a59ff31 //回滾到某個(gè)版本

git使用常見的問題

1、提交不了的時(shí)候,顯示 入校錯(cuò)誤!

failed to push some refs to 'https://github.com/topljd/studynote.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.

答:因?yàn)榇藭r(shí)版本中沒有給readme.txt文件,解決辦法

可以通過如下命令進(jìn)行代碼合并【注:pull=fetch+merge]

git pull --rebase origin master

image-20200426150149617

此時(shí)將會(huì)把庫(kù)里的文件下載到本地當(dāng)前文件夾!

然后在進(jìn)行提交

git push -m study master

2、如何刪除github項(xiàng)目

image-20200426151600800

點(diǎn)擊setting

image-20200426151636390

找到Danger Zone最下買你的delete this repository

image-20200426151733663

確認(rèn)是否真的要?jiǎng)h除!

3、如何刪除github庫(kù)里面的某個(gè)文件

因?yàn)樵趃ithub上不能直接刪除某個(gè)文件,所以必須用git命令去刪除,在上傳的的項(xiàng)目文件里打開git,找到要?jiǎng)h除的文件。202002101581332439512796.png

image-20200426153611443
  • git pull --rebase origin master或者git pull origin master將github上的額文件重新拉下來,其中origin是別名,master是分支。

    image-20200426153850252
  • 然后輸入命令dir查看目錄下的而文件,如下圖:

    image-20200426154032831
  • 再輸入命令git rm -r --cache 202002101581332439512796.png刪除磁盤上的該圖片

    image-20200426154252148
  • 再輸入git commit -m '刪除了202002101581332439512796.png'提交添加說明如下圖:

    image-20200426154514866
  • 最后輸入git push -u origin master更新github倉(cāng)庫(kù),如下圖:

    image-20200426154659467
    image-20200426154725963

    文件已經(jīng)被刪除了!

    4、當(dāng)出現(xiàn)master warning: LF will be replaced by CRLF in www/css/style.css.>

    git config --global core.autocrlf false
    
    

    一般的還是遠(yuǎn)程倉(cāng)庫(kù)中的文件與本地的文件不一樣,需要先將遠(yuǎn)程倉(cāng)庫(kù)中的代碼拉去到本地種!

    5、創(chuàng)建分支

    $ git checkout -b iss53
    Switched to a new branch "iss53"
    上面是下面的簡(jiǎn)寫
    $ git branch iss53
    $ git checkout iss53
    
    

6、出現(xiàn)上傳不了錯(cuò)誤提示如下:

? error: cannot pull with rebase: You have unstaged changes.? error: please commit or stash them.

解決辦法:

1.git pull –rebase 理解

這個(gè)命令做了以下內(nèi)容: a.把你 commit 到本地倉(cāng)庫(kù)的內(nèi)容,取出來放到暫存區(qū)(stash)(這時(shí)你的工作區(qū)是干凈的) b.然后從遠(yuǎn)端拉取代碼到本地,由于工作區(qū)是干凈的,所以不會(huì)有沖突 c.從暫存區(qū)把你之前提交的內(nèi)容取出來,跟拉下來的代碼合并

所以 rebase 在拉代碼前要確保你本地工作區(qū)是干凈的,如果你本地修改的內(nèi)容沒完全 commit 或者 stash,就會(huì) rebase 失敗。

2.還是要聽 git 提示的話,要理智,有什么不清楚的,就輸入 git status 根據(jù)人家提示的來,該提交的提交,stash 的 stash。

3.刪除文件后需要 git add -A, 光 git add. 不行,區(qū)別如下:

git add 的幾種參數(shù)區(qū)別
git add -A 保存所有的修改
git add . 保存新的添加和修改,但是不包括刪除
git add -u 保存修改和刪除,但是不包括新建文件。

7、切換分支的時(shí)候里面本地文件夾中的文件也會(huì)發(fā)生變化

image-20200503141850437
image-20200503141938653

8、名字和分支的用法

image-20200506134847288

9、git刪除本地分支、刪除遠(yuǎn)程分支

1、查看所有分支

git branch -a

$ git branch -a
  coding-pages
  gh-pages
* master    # * 表示當(dāng)前分支
# 下面是遠(yuǎn)程分支
  remotes/origin/gh-pages
  remotes/origin/master
  remotes/origin_coding/coding-pages
  remotes/origin_coding/master
  remotes/origin_gitee/master
  ########
  $ git branch -r #查看遠(yuǎn)程分支
  origin/gh-pages
  origin/master
  origin_coding/coding-pages
  origin_coding/master
  origin_gitee/master

2、查看當(dāng)前所在分支

git branch

3、刪除本地的XXX分支

git branch -d XXX

4、刪除遠(yuǎn)程的XXX分支

git push origin --delete XXX遠(yuǎn)程的話一般是雙-

10、拉取遠(yuǎn)程分支并創(chuàng)建本地分支

1、git checkout -b 本地分支名x origin/遠(yuǎn)程分支名x

使用該方式會(huì)在本地新建分支x,并自動(dòng)切換到該本地分支x。

2、git fetch origin 遠(yuǎn)程分支名x:本地分支名x

使用該方式會(huì)在本地新建分支x,但是不會(huì)自動(dòng)切換到該本地分支x,需要手動(dòng)checkout。

3、git branch -vv查看本地分支與遠(yuǎn)程分支映射關(guān)系

$ git branch -vv
  coding-pages df545e4 [origin/coding-pages: gone] 刪除所有pages文件
  gh-pages     6d2697e [origin/gh-pages] 2020年5月7日10:42:39
* master       cdb916a [origin_coding/master: ahead 1] 2020年5月7日10:26:37 提交ignore

4、git branch -u origin/addFilegit branch --set-upstream-to origin/addFile建立當(dāng)前分支與遠(yuǎn)程分支的映射。

image-20200507111308340

查看當(dāng)前本地分支與遠(yuǎn)程分支映射關(guān)系結(jié)果如下:

image-20200507111343848

此時(shí)再次拉取,成功信息如下:

git pull

Already up-to-date

再次推送,成功信息如下:

git push

Everything up-to-date

5、撤銷本地分支與遠(yuǎn)程分支的映射關(guān)系

git branch --unset-upstream使用git branch -vv查看映射關(guān)系

image-20200507111754906

6、可以本地分支A名和遠(yuǎn)程分支B名建立映射關(guān)系

image-20200507112009696

并且此時(shí)可以把本地分支A提交到分支B分支中去:

image-20200507112053057

11、failed to push some refs 當(dāng)push上傳不了出現(xiàn)如下提示的時(shí)候

image-20200510204125869

解決方案:遠(yuǎn)程倉(cāng)庫(kù)與本地倉(cāng)庫(kù)代碼不一樣,上傳不了!(遠(yuǎn)程倉(cāng)庫(kù)有其他的文件)

方法:1、git pull origin master將遠(yuǎn)程文件拉取到本地。再次執(zhí)行git add .等系列動(dòng)作上傳。此時(shí)一切正常!

方法2:強(qiáng)推;在本地回滾后,遠(yuǎn)程上面依然是之前的版本,用git push -f origin master將本地庫(kù)強(qiáng)制推送到遠(yuǎn)程,這時(shí)遠(yuǎn)程庫(kù)也已經(jīng)回滾到之前的提交了。

12、上傳時(shí)出現(xiàn)fatal: 拒絕合并無關(guān)的歷史 ```shell 本地初始化的項(xiàng)目 與 github 版本不一致, 導(dǎo)致無法提交

$ git pull origin master

  • branch master -> FETCH_HEAD fatal: 拒絕合并無關(guān)的歷史 解決方法

在pull 時(shí)候, 添加–allow-unrelated-histories參數(shù) 即可.

$ git pull origin master --allow-unrelated-histories
來自 https://github.com/itaken/python-login-demo

  • branch master -> FETCH_HEAD Merge made by the 'recursive' strategy. LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE ```

蘋果電腦,上傳到github的時(shí)候,會(huì)發(fā)現(xiàn)很多._的文件?

答:其實(shí)這些是因?yàn)榇疟P的格式不是afhs格式自動(dòng)生成的隱藏文件。

對(duì)于clone的倉(cāng)庫(kù)

1、執(zhí)行 git clone 地址

2、正常git add . 就可以了

13、git提交的時(shí)候log日志出現(xiàn)亂碼

image-20200604170030111

解決辦法:

找到自己的 git 安裝目錄,在該路徑下執(zhí)行以下3條設(shè)置命令, 分別設(shè)置 提交文件、界面、提交日志 這三者的編碼格式。


git config --global i18n.commitencoding utf-8

git config --global gui.encoding utf-8 

git config --global i18n.logoutputencoding utf-8
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容