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

Git clone只能clone远程库的master分支,无法clone所有分支

$ git clone [email protected]: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.

指定用户名密码进行clone:git clone https://用户名:密码@github.com/special-lily/dingding.git

$ git clone https://username:[email protected]
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.

克隆下来之后 打开项目文件夹

$ cd dingding

查看所有分支 前面带*号的代表你当前工作目录所处的分支

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

查看各个分支当前所指的对象。 提供这一功能的参数是 --decorate。(不明)

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

创建本地分支,新分支创建后不会自动切换为当前分支

$ git branch dev 

查看所有分支, 上条命令创建的是本地分支

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

查看远程分支

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

查看本地分支 前面带*号的代表你当前工作目录所处的分支

$ git branch
  dev
* master

只把部分修改的文件添加到暂存区

$ git add index-dev.html

删除本地dev分支

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

删除失败 因为现在正在处于检出状态, 切换分支到master

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

再次执行删除分支

$ 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'.

删除失败 因为dev分支没有被merge,如果仍要删除就使用git branch -D dev

删除本地分支

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

把本地分支代码更新到远程分支,git push origin 本地分支名:远程分支名

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

创建远程分支:把本地分支推到远程

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

远程分支名不存在 ,就会自动创建一个新的远程分支

$ 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

删除远程分支

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

如果本地分支名为空则删除:右侧的远程分支

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

从远程分支创建本地新分支 并检出:在远程的gh-pages分支上创建本地分支名为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'

在当前本地分支上 创建新分支

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

用gitk来查看commit的结果,你会看到它有两个父分支:一个指向当前的分支,另外一个指向刚才合并进来的分支

$ gitk

拉取远程分支最新代码

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

也可以直接执行 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分支与当前分之合并

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

查看某个文件中部分代码由谁修改过

$ 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)

分区块暂存

$ 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 @@
        
        
+

éé??o????éèo??é??è′1?§??¨????13?°1

+

éé??o????éèo??é??è′1?§??¨????13?°2

+

éé??o????éèo??é??è′1?§??¨????13?°3

éé??o????éèo??é??è′1?§??¨????13?°

?¨?o??§??¨????£??éé???o????a?·¥???1?? (??é¤ )

-

éé??o????éèo??é??è′1?§??¨????13?°

-

?¨?o??§??¨????£??éé???o????a?·¥???1?? (?¨dev ??ˉ??-?·???????1)

Stage this hunk [y,n,q,a,d,/,s,e,?]? s Split into 2 hunks. @@ -109,5 +109,8 @@
+

éé??o????éèo??é??è′1?§??¨????13?°1

+

éé??o????éèo??é??è′1?§??¨????13?°2

+

éé??o????éèo??é??è′1?§??¨????13?°3

éé??o????éèo??é??è′1?§??¨????13?°

?¨?o??§??¨????£??éé???o????a?·¥???1?? (??é¤ )

Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]? y @@ -112,7 +115,5 @@

éé??o????éèo??é??è′1?§??¨????13?°

?¨?o??§??¨????£??éé???o????a?·¥???1?? (??é¤ )

-

éé??o????éèo??é??è′1?§??¨????13?°

-

?¨?o??§??¨????£??éé???o????a?·¥???1?? (?¨dev ??ˉ??-?·???????1)

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

查看保存过的列表

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

清空stash 列表

$ git stash clear

把保存的变更应用进来

$ git stash apply
On branch dev-gh2
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." 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 
Date:   Fri Jul 21 11:04:44 2017 +0800

    --soft head-2

commit 258618198b8afa0b1ab3abef3da83eb7df5e932c
Author: special-lily 
Date:   Fri Jul 21 10:58:20 2017 +0800

    first--

commit 0c50d7bc47293439ce799b1c051bf28f3e28937d
Author: special-lily 
Date:   Thu Jul 20 17:31:01 2017 +0800

    css add

commit 52d0c4435756179a62bb75e16839bdd7d56549ac
Author: Datura900607 <[email protected]>
Date:   Thu Jul 20 17:13:03 2017 +0800

    Update index.html

-p 选项展开显示每次提交的内容差异,用 -2 则仅显示最近的两次更新:

$ git log -p
commit 258618198b8afa0b1ab3abef3da83eb7df5e932c
Author: special-lily 
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 @@
        
        

回退本地分支版本 $ git reset --hard commit的版本号

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

下面两个命令都是把dev分支与当前分之合并:

$ git merge dev
Already up-to-date.

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

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

添加标签:git tag 是打标签的命令,-a 是添加标签,其后要跟新标签号,-m 及后面的字符串是对该标签的注释

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

提交标签到远程仓库
git push origin -tags
注解:就像git push origin master 把本地修改提交到远程仓库一样,-tags可以把本地的打的标签全部提交到远程仓库。

删除标签
git tag -d v1.01
注解:-d 表示删除,后面跟要删除的tag名字

删除远程标签
git push origin :refs/tags/v1.01
注解:就像git push origin :branch_1 可以删除远程仓库的分支branch_1一样, 冒号前为空表示删除远程仓库的tag。

查看标签
git tag
或者
git tag -l

你可能感兴趣的:(Git(2)---git fork 涉及分支,及其他git命令)