Git(2)---git fork 涉及分支,及其他git命令

Git clone只能clone遠(yuǎn)程庫(kù)的master分支,無(wú)法clone所有分支

$ git clone git@github.com:special-lily/dingding.git
Cloning into 'dingding'...
remote: Counting objects: 52, done.
remote: Compressing objects: 100% (47/47), done.
remote: Total 52 (delta 4), reused 52 (delta 4), pack-reused 0
Receiving objects: 100% (52/52), 1.20 MiB | 308.00 KiB/s, done.
Resolving deltas: 100% (4/4), done.

指定用戶名密碼進(jìn)行clone:git clone https://用戶名:密碼@github.com/special-lily/dingding.git

$ git clone https://username:password@repo.path.git
Cloning into 'dingding'...
remote: Counting objects: 74, done.
remote: Compressing objects: 100% (16/16), done.
remote: Total 74 (delta 10), reused 15 (delta 6), pack-reused 52
Unpacking objects: 100% (74/74), done.

克隆下來(lái)之后 打開(kāi)項(xiàng)目文件夾

$ cd dingding

查看所有分支 前面帶*號(hào)的代表你當(dāng)前工作目錄所處的分支

$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/gh-pages
  remotes/origin/master

查看各個(gè)分支當(dāng)前所指的對(duì)象。 提供這一功能的參數(shù)是 --decorate。(不明)

$ git log --oneline --decorate
3611f31 (HEAD -> master, origin/master, origin/HEAD) Initial commit

創(chuàng)建本地分支,新分支創(chuàng)建后不會(huì)自動(dòng)切換為當(dāng)前分支

$ git branch dev 

查看所有分支, 上條命令創(chuàng)建的是本地分支

$ git branch -a
  dev
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/gh-pages
  remotes/origin/master

查看遠(yuǎn)程分支

$ git branch -r
  origin/HEAD -> origin/master
  origin/dev
  origin/gh-pages
  origin/master

查看本地分支 前面帶*號(hào)的代表你當(dāng)前工作目錄所處的分支

$ git branch
  dev
* master

只把部分修改的文件添加到暫存區(qū)

$ git add index-dev.html

刪除本地dev分支

$ git branch -d dev
error: Cannot delete branch 'dev' checked out at 'E:/dingding-lijian/dingding'

刪除失敗 因?yàn)楝F(xiàn)在正在處于檢出狀態(tài), 切換分支到master

$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

再次執(zhí)行刪除分支

$ git branch -d dev
error: The branch 'dev' is not fully merged.
If you are sure you want to delete it, run 'git branch -D dev'.

刪除失敗 因?yàn)閐ev分支沒(méi)有被merge,如果仍要?jiǎng)h除就使用git branch -D dev

刪除本地分支

$ git branch -D dev
Deleted branch dev (was 7a25f30).

把本地分支代碼更新到遠(yuǎn)程分支,git push origin 本地分支名:遠(yuǎn)程分支名

$ git push origin dev:dev
Total 0 (delta 0), reused 0 (delta 0)
To github.com:special-lily/dingding.git
   3611f31..7a25f30  dev -> dev

創(chuàng)建遠(yuǎn)程分支:把本地分支推到遠(yuǎn)程

$ git push origin dev
Total 0 (delta 0), reused 0 (delta 0)
To github.com:special-lily/dingding.git
 * [new branch]      dev -> dev

遠(yuǎn)程分支名不存在 ,就會(huì)自動(dòng)創(chuàng)建一個(gè)新的遠(yuǎn)程分支

$ git push origin dev:dev2
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 441 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:special-lily/dingding.git
 * [new branch]      dev -> dev2

刪除遠(yuǎn)程分支

$ git branch -r -d origin/dev2
Deleted remote-tracking branch origin/dev2 (was 7a25f30).

如果本地分支名為空則刪除:右側(cè)的遠(yuǎn)程分支

$ git push origin :dev
To github.com:special-lily/dingding.git
 - [deleted]         dev

從遠(yuǎn)程分支創(chuàng)建本地新分支 并檢出:在遠(yuǎn)程的gh-pages分支上創(chuàng)建本地分支名為dev-gh分支,并切換到本地的dev-gh分支

$ git checkout -b dev-gh origin/gh-pages
Branch dev-gh set up to track remote branch gh-pages from origin.
Switched to a new branch 'dev-gh'

在當(dāng)前本地分支上 創(chuàng)建新分支

$ git checkout -b dev-gh2
Switched to a new branch 'dev-gh2'

用gitk來(lái)查看commit的結(jié)果,你會(huì)看到它有兩個(gè)父分支:一個(gè)指向當(dāng)前的分支,另外一個(gè)指向剛才合并進(jìn)來(lái)的分支

$ gitk

拉取遠(yuǎn)程分支最新代碼

$ git pull origin gh-pages:dev-gh2
Already up-to-date.

也可以直接執(zhí)行 git pull

$ git pull
Warning: Permanently added the RSA host key for IP address '192.30.253.113' to the list of known hosts.
From github.com:special-lily/dingding
 * [new branch]      dev2       -> origin/dev2
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Auto-merging css/index.css
CONFLICT (content): Merge conflict in css/index.css
Automatic merge failed; fix conflicts and then commit the result.

自行解決沖突 并 git add , git commit

把dev分支與當(dāng)前分之合并

$ git merge dev
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

查看某個(gè)文件中部分代碼由誰(shuí)修改過(guò)

$ git blame css/index.css
fc6439f5 (Datura900607 2016-07-26 21:03:05 +0800 194)   width: 100%;
fc6439f5 (Datura900607 2016-07-26 21:03:05 +0800 195)   position:absolute;
fc6439f5 (Datura900607 2016-07-26 21:03:05 +0800 196)   left: 0;top: 800px;
fc6439f5 (Datura900607 2016-07-26 21:03:05 +0800 197)   text-align: center;
0c50d7bc (special-lily 2017-07-20 17:31:01 +0800 198)   color: #e4b9c0;
0c50d7bc (special-lily 2017-07-20 17:31:01 +0800 199)   background-color: #ffffff;
fc6439f5 (Datura900607 2016-07-26 21:03:05 +0800 200) }
(END)

分區(qū)塊暫存

$ git add -p index.html
diff --git a/index.html b/index.html
index 9d084ea..5f83bea 100644
--- a/index.html
+++ b/index.html
@@ -109,10 +109,11 @@
        </section>
        <article class="article_1">
                <div class="container">
+                       <h1>éé??o????éèo??é??è′1?§??¨????13?°1</h1>
+                       <h1>éé??o????éèo??é??è′1?§??¨????13?°2</h1>
+                       <h1>éé??o????éèo??é??è′1?§??¨????13?°3</h1>
                        <h1>éé??o????éèo??é??è′1?§??¨????13?°</h1>
                        <h3>?¨?o??§??¨????£??éé???o????a?·¥???1?? (??é¤ )</h3>
-                       <h1>éé??o????éèo??é??è′1?§??¨????13?°</h1>
-                       <h3>?¨?o??§??¨????£??éé???o????a?·¥???1?? (?¨dev ??ˉ??-?·???????1)</h3>
                </div>
        </article>
 </body>
Stage this hunk [y,n,q,a,d,/,s,e,?]? s
Split into 2 hunks.
@@ -109,5 +109,8 @@
        </section>
        <article class="article_1">
                <div class="container">
+                       <h1>éé??o????éèo??é??è′1?§??¨????13?°1</h1>
+                       <h1>éé??o????éèo??é??è′1?§??¨????13?°2</h1>
+                       <h1>éé??o????éèo??é??è′1?§??¨????13?°3</h1>
                        <h1>éé??o????éèo??é??è′1?§??¨????13?°</h1>
                        <h3>?¨?o??§??¨????£??éé???o????a?·¥???1?? (??é¤ )</h3>
Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]? y
@@ -112,7 +115,5 @@
                        <h1>éé??o????éèo??é??è′1?§??¨????13?°</h1>
                        <h3>?¨?o??§??¨????£??éé???o????a?·¥???1?? (??é¤ )</h3>
-                       <h1>éé??o????éèo??é??è′1?§??¨????13?°</h1>
-                       <h3>?¨?o??§??¨????£??éé???o????a?·¥???1?? (?¨dev ??ˉ??-?·???????1)</h3>
                </div>
        </article>
 </body>
Stage this hunk [y,n,q,a,d,/,K,g,e,?]? n

保存修改的代碼,不提交

$ git stash
Saved working directory and index state WIP on dev-gh2: 0c50d7b css add
HEAD is now at 0c50d7b css add

查看保存過(guò)的列表

$ git stash list
stash@{0}: WIP on dev-gh2: 0c50d7b css add

清空stash 列表

$ git stash clear

把保存的變更應(yīng)用進(jìn)來(lái)

$ git stash apply
On branch dev-gh2
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   index.html

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

查看commit 列表

$ git log
commit 508bb26104fff8ef0958e5850746f5cf4dcbfc9f
Author: special-lily <special_yjl@163.com>
Date:   Fri Jul 21 11:04:44 2017 +0800

    --soft head-2

commit 258618198b8afa0b1ab3abef3da83eb7df5e932c
Author: special-lily <special_yjl@163.com>
Date:   Fri Jul 21 10:58:20 2017 +0800

    first--

commit 0c50d7bc47293439ce799b1c051bf28f3e28937d
Author: special-lily <special_yjl@163.com>
Date:   Thu Jul 20 17:31:01 2017 +0800

    css add

commit 52d0c4435756179a62bb75e16839bdd7d56549ac
Author: Datura900607 <59720607@qq.com>
Date:   Thu Jul 20 17:13:03 2017 +0800

    Update index.html

-p 選項(xiàng)展開(kāi)顯示每次提交的內(nèi)容差異,用 -2 則僅顯示最近的兩次更新:

$ git log -p
commit 258618198b8afa0b1ab3abef3da83eb7df5e932c
Author: special-lily <special_yjl@163.com>
Date:   Fri Jul 21 10:58:20 2017 +0800

    first--

diff --git a/index.html b/index.html
index 9d084ea..cddf06f 100644
--- a/index.html
+++ b/index.html
@@ -109,10 +109,10 @@
        </section>
        <article class="article_1">
                <div class="container">


回退本地分支版本 $ git reset --hard commit的版本號(hào)

$ git reset --hard  258618198b8afa0b1ab3abef3da83eb7df5e932c
HEAD is now at 2586181 first--

下面兩個(gè)命令都是把dev分支與當(dāng)前分之合并:

$ git merge dev
Already up-to-date.

$ git rebase dev
Current branch master is up to date.

不同的是, rebase的log history是線性的

添加標(biāo)簽:git tag 是打標(biāo)簽的命令,-a 是添加標(biāo)簽,其后要跟新標(biāo)簽號(hào),-m 及后面的字符串是對(duì)該標(biāo)簽的注釋

$ git tag -a v1.0.1 -m "dev-gh2---tag"

提交標(biāo)簽到遠(yuǎn)程倉(cāng)庫(kù)
git push origin -tags
注解:就像git push origin master 把本地修改提交到遠(yuǎn)程倉(cāng)庫(kù)一樣,-tags可以把本地的打的標(biāo)簽全部提交到遠(yuǎn)程倉(cāng)庫(kù)。

刪除標(biāo)簽
git tag -d v1.01
注解:-d 表示刪除,后面跟要?jiǎng)h除的tag名字

刪除遠(yuǎn)程標(biāo)簽
git push origin :refs/tags/v1.01
注解:就像git push origin :branch_1 可以刪除遠(yuǎn)程倉(cāng)庫(kù)的分支branch_1一樣, 冒號(hào)前為空表示刪除遠(yuǎn)程倉(cāng)庫(kù)的tag。

查看標(biāo)簽
git tag
或者
git tag -l

最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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