<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          git 快速入門及常見用法

          共 7666字,需瀏覽 16分鐘

           ·

          2020-09-18 14:08

          點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號”

          優(yōu)質(zhì)文章,第一時(shí)間送達(dá)

          ? 作者?|??鋼鐵俠的知識庫?

          來源 |? urlify.cn/QBzeQv

          66套java從入門到精通實(shí)戰(zhàn)課程分享

          身為技術(shù)人員,都知道Git是干嘛的。從服務(wù)端角度它是代碼倉庫,可以多人協(xié)作、版本控制、高效處理大型或小型項(xiàng)目所有內(nèi)容;從客戶端講,它能夠方便管理本地分支、且與服務(wù)端代碼的同步,從拉取、合并、提交等等管理分支都靠它!

          Git輕量、易于學(xué)習(xí),如果不用搭建和維護(hù)代碼倉庫的話(運(yùn)維職責(zé)),只要掌握幾個(gè)git常用命令即可在工作中輕松應(yīng)對。

          下面簡單介紹幾個(gè)概念,同時(shí)列出工作中常用命令:

          主要概念

          快速入門,弄明白以下幾個(gè)概念即可:

          • 工作區(qū)(Working Directory):就是你在電腦里能看到的目錄,或克隆(clone)下來的目錄;

          • 版本庫(Repository):工作區(qū)里面有一個(gè)隱藏目錄.git,這個(gè)不是工作區(qū),而是Git的版本庫;

          • 暫存區(qū)(stage):版本庫中有一個(gè)叫stage的暫存區(qū),git add可以把要提交的內(nèi)容放到暫存區(qū);

          • 主分支(master):版本庫還有一個(gè)叫master的主分支,git commit把暫存區(qū)所有內(nèi)容提交到當(dāng)前分支;

          主要用法

          工作中,一般我們提交代碼只要四步:

          • 第一步,git pull?拉取代碼,提交代碼前確保和服務(wù)端倉庫一致,避免沖突;

          • 第二步,git add ./your_file.txt?把文件添加進(jìn)去,實(shí)際就是從工作區(qū)提交到暫存區(qū);

          • 第三步,git commit -m 'first commit'提交更改,再把暫存區(qū)所有內(nèi)容提交到當(dāng)前分支(默認(rèn)master);

          • 第四步,git push [remoteName]推送到遠(yuǎn)程倉庫,也就是推到服務(wù)端,這樣別人就能拉取pull你的代碼;

          常見用法

          平時(shí)工作也就用到上面四個(gè)步驟,當(dāng)然了凡事有例外,下面說幾個(gè)例外的處理辦法:

          一、checkout切換分支

          git checkout :切換到你需要的分支(dev、hotfix)

          git checkout -b :如果沒有分支,加上-b參數(shù)表示創(chuàng)建并切換;

          參考鏈接:https://git-scm.com/docs/git-checkout

          二、git提交后撤銷問題

          撤銷得分三種情況:

          • 第一,已經(jīng)修改文件但未執(zhí)行git add的撤銷方法;
            我故意在
            .gitignore文件修改之后且沒有git add,直接通過git checkout -- 撤銷;
            但是此命令不會(huì)撤銷新建的文件,因?yàn)樾陆ㄎ募€沒加入到Git管理系統(tǒng)中,
            所以git是未知的,自己手動(dòng)刪除就好了。

          λ?git?status
          On?branch?master
          Your?branch?is?up-to-date?with?'origin/master'.
          nothing?to?commit,?working?tree?clean

          D:\learning\git\work?(master?->?origin)
          λ?git?status
          On?branch?master
          Your?branch?is?up-to-date?with?'origin/master'.
          Changes?not?staged?for?commit:
          ??(use?"git?add?..."?to?update?what?will?be?committed)
          ??(use?"git?checkout?--?..."?to?discard?changes?in?working?directory)

          ????????modified:???.gitignore

          no?changes?added?to?commit?(use?"git?add"?and/or?"git?commit?-a")

          D:\learning\git\work?(master?->?origin)
          λ?git?checkout?--?.gitignore

          D:\learning\git\work?(master?->?origin)
          λ?git?status
          On?branch?master
          Your?branch?is?up-to-date?with?'origin/master'.
          nothing?to?commit,?working?tree?clean

          擴(kuò)展:
          命令
          git checkout -- .gitignore意思就是,把.gitignore文件在工作區(qū)的修改全部撤銷,這里有兩種情況:
          一種是.gitignore自修改后還沒有被放到暫存區(qū),現(xiàn)在撤銷修改就回到和版本庫一模一樣的狀態(tài);
          一種是.gitignore已經(jīng)添加到暫存區(qū),又作了修改,現(xiàn)在,撤銷修改就回到添加到暫存區(qū)后的狀態(tài)。
          總之,就是讓這個(gè)文件回到最近一次git commit或git add時(shí)的狀態(tài)。
          參考鏈接:https://www.liaoxuefeng.com/wiki/896043488029600/897889638509536

          • 第二,已經(jīng)修改文件且git add的撤銷方法
            需要先執(zhí)行
            git reset .gitignore撤銷到未git add狀態(tài),再執(zhí)行第一步即可。

          λ?git?add?.gitignore

          D:\learning\git\work?(master?->?origin)
          λ?git?status
          On?branch?master
          Your?branch?is?up-to-date?with?'origin/master'.
          Changes?to?be?committed:
          ??(use?"git?reset?HEAD?..."?to?unstage)

          ????????modified:???.gitignore


          D:\learning\git\work?(master?->?origin)
          λ?git?reset?.gitignore
          Unstaged?changes?after?reset:
          M???????.gitignore

          D:\learning\git\work?(master?->?origin)
          λ?git?status
          On?branch?master
          Your?branch?is?up-to-date?with?'origin/master'.
          Changes?not?staged?for?commit:
          ??(use?"git?add?..."?to?update?what?will?be?committed)
          ??(use?"git?checkout?--?..."?to?discard?changes?in?working?directory)

          ????????modified:???.gitignore

          no?changes?added?to?commit?(use?"git?add"?and/or?"git?commit?-a")

          D:\learning\git\work?(master?->?origin)
          λ?git?checkout?--?.gitignore

          D:\learning\git\work?(master?->?origin)
          λ?git?status
          On?branch?master
          Your?branch?is?up-to-date?with?'origin/master'.
          nothing?to?commit,?working?tree?clean
          • 第三,Git已經(jīng)commit如何撤銷:
            通過
            git reset --hard commitid直接回到未修改狀態(tài)。

          λ?git?add?.gitignore
          λ?git?commit?-m?"test"
          #(省略無用部分)
          λ?git?status
          On?branch?master
          Your?branch?is?ahead?of?'origin/master'?by?1?commit.
          ??(use?"git?push"?to?publish?your?local?commits)
          nothing?to?commit,?working?tree?clean

          D:\learning\git\work?(master?->?origin)
          λ?git?log
          commit?b7de9378f39834dbc8304d4a8d30f39a4003c673?(HEAD?->?master)
          Author:?test?<test@163.com>
          Date:???Mon?Sep?14?02:59:02?2020?+0800

          ????test

          commit?b3ed1078e543cdb26b984dac584df9db7553d506?(origin/master,?origin/HEAD)
          Author:?test?<test@163.com>
          Date:???Mon?Sep?14?02:39:54?2020?+0800

          ????09142020
          D:\learning\git\work?(master?->?origin)
          λ?git?reset?--hard?b3ed1078e543cdb26b984dac584df9db7553d506
          HEAD?is?now?at?b3ed107?09142020

          D:\learning\git\work?(master?->?origin)
          λ?git?log
          commit?b3ed1078e543cdb26b984dac584df9db7553d506?(HEAD?->?master,?origin/master,?origin/HEAD)
          Author:?test?<test@163.com>
          Date:???Mon?Sep?14?02:39:54?2020?+0800

          ????09142020
          ????
          D:\learning\git\work?(master?->?origin)
          λ?git?status
          On?branch?master
          Your?branch?is?up-to-date?with?'origin/master'.
          nothing?to?commit,?working?tree?clean

          三、git stash保存和恢復(fù)工作區(qū)內(nèi)容

          git stash可以將你已經(jīng)修改,但不想提交(git push)的代碼臨時(shí)保存到堆棧中,也就是回歸到你git pull時(shí)的狀態(tài)。然后就能隨意切換分支救火,完成后切換回來再git push pop即可恢復(fù)之前的修改內(nèi)容。stash不僅可以恢復(fù)到原先開發(fā)的分支,也可以恢復(fù)到其他任意指定的分支上(可跨分支)。

          • git stash
            保存當(dāng)前工作進(jìn)度,會(huì)把暫存區(qū)和工作區(qū)的改動(dòng)保存起來。
            執(zhí)行完之后再
            git status,會(huì)發(fā)現(xiàn)當(dāng)前是一個(gè)干凈的工作區(qū),沒有任何改動(dòng)。
            使用git stash save 'message...'可以添加一些注釋

          • git stash list
            顯示保存進(jìn)度的列表。也就意味著,git stash命令可以多次執(zhí)行。

          • git stash pop
            恢復(fù)最新的進(jìn)度到工作區(qū)。git默認(rèn)會(huì)把工作區(qū)和暫存區(qū)的改動(dòng)都恢復(fù)到工作區(qū)。

          • git stash pop stash@{stash_id}
            恢復(fù)指定的進(jìn)度到工作區(qū)。stash_id通過git stash list命令得到的
            通過git stash pop命令恢復(fù)進(jìn)度后,會(huì)刪除當(dāng)前進(jìn)度。

          • git stash drop stash@{stash_id}
            可以使用git stash drop命令,后面可以跟stash_id
            或使用git stash clear命令,刪除所有緩存的stash

          • git stash show
            查看堆棧中最新保存的stash和當(dāng)前目錄的差異

          舉個(gè)例子

          1. 修改.gitignore文件,新建test.txt文件。再執(zhí)行stash發(fā)現(xiàn)新增文件不會(huì)被存儲(chǔ);

          2. git add之后再stash發(fā)現(xiàn)工作區(qū)是干凈的;

          3. 說明沒有在git版本控制(git add)中的文件,不能被git stash 保存;

          4. 最后通過git stash pop恢復(fù)。

          λ?git?status
          On?branch?master
          Your?branch?is?up?to?date?with?'origin/master'.

          Changes?not?staged?for?commit:
          ??(use?"git?add?..."?to?update?what?will?be?committed)
          ??(use?"git?restore?..."?to?discard?changes?in?working?directory)
          ????????modified:???.gitignore

          Untracked?files:
          ??(use?"git?add?..."?to?include?in?what?will?be?committed)
          ????????test.txt

          no?changes?added?to?commit?(use?"git?add"?and/or?"git?commit?-a")
          D:\learning\git\timed_tasks?(master?->?origin)
          λ?git?stash
          Saved?working?directory?and?index?state?WIP?on?master:?542a055?create?.gitignore

          D:\learning\git\timed_tasks?(master?->?origin)
          λ?git?status
          On?branch?master
          Your?branch?is?up?to?date?with?'origin/master'.

          Untracked?files:
          ??(use?"git?add?..."?to?include?in?what?will?be?committed)
          ????????test.txt

          nothing?added?to?commit?but?untracked?files?present?(use?"git?add"?to?track)

          D:\learning\git\timed_tasks?(master?->?origin)
          λ?git?add?test.txt

          D:\learning\git\timed_tasks?(master?->?origin)
          λ?git?stash
          Saved?working?directory?and?index?state?WIP?on?master:?542a055?create?.gitignore

          D:\learning\git\timed_tasks?(master?->?origin)
          λ?git?stash?list
          stash@{0}:?WIP?on?master:?542a055?create?.gitignore
          stash@{1}:?WIP?on?master:?542a055?create?.gitignore
          stash@{2}:?WIP?on?(no?branch):?542a055?create?.gitignore

          D:\learning\git\timed_tasks?(master?->?origin)
          λ?git?status
          On?branch?master
          Your?branch?is?up?to?date?with?'origin/master'.

          nothing?to?commit,?working?tree?clean
          D:\learning\git\timed_tasks?(master?->?origin)
          λ?git?stash?show
          ?test.txt?|?0
          ?1?file?changed,?0?insertions(+),?0?deletions(-)

          D:\learning\git\timed_tasks?(master?->?origin)
          λ?git?stash?pop
          On?branch?master
          Your?branch?is?up?to?date?with?'origin/master'.

          Changes?to?be?committed:
          ??(use?"git?restore?--staged?..."?to?unstage)
          ????????new?file:???test.txt

          Dropped?refs/stash@{0}?(b69da2894d5e7f511be18277c5a0cd4582fbf453)

          D:\learning\git\timed_tasks?(master?->?origin)
          λ?git?status
          On?branch?master
          Your?branch?is?up?to?date?with?'origin/master'.

          Changes?to?be?committed:
          ??(use?"git?restore?--staged?..."?to?unstage)
          ????????new?file:???test.txt

          Tip:如果你修改的所有文件都不想要了怎么辦?可通過git stash清空,懂吧?

          λ?git?status
          On?branch?master
          Your?branch?is?up?to?date?with?'origin/master'.

          Changes?to?be?committed:
          ??(use?"git?restore?--staged?..."?to?unstage)
          ????????new?file:???test.txt

          D:\learning\git\timed_tasks?(master?->?origin)
          λ?git?stash
          Saved?working?directory?and?index?state?WIP?on?master:?542a055?create?.gitignore

          D:\learning\git\timed_tasks?(master?->?origin)
          λ?git?stash?clear

          D:\learning\git\timed_tasks?(master?->?origin)
          λ?git?status
          On?branch?master
          Your?branch?is?up?to?date?with?'origin/master'.

          nothing?to?commit,?working?tree?clean





          粉絲福利:108本java從入門到大神精選電子書領(lǐng)取

          ???

          ?長按上方鋒哥微信二維碼?2 秒
          備注「1234」即可獲取資料以及
          可以進(jìn)入java1234官方微信群



          感謝點(diǎn)贊支持下哈?

          瀏覽 54
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚洲美女操B | 天天操天天摸天天日天天爱 | 91九九 | 波多野结衣视频在线看 | 91操碰|