码迷,mamicode.com
首页 > 其他好文 > 详细

Github教程(1)

时间:2015-12-15 21:01:17      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

Git基本命令

 

git diff:查看工作树,暂存区,最新提交之间的差别

举例:

在README.MD中增加一行

## This is the subtitle

执行git diff 命令,查看当前工作树暂存区的区别。

$ git diff

diff --git a/README.md b/README.md

index fec5601..203cf31 100644

--- a/README.md

+++ b/README.md

@@ -1 +1,2 @@

# Hello

+## This is the subtitle

 

说明:"+"表示新添加的行,"-"表示删除的行,由结果可知:增加了:## This is the subtitle

 

执行git add README.md 将README.md文件加入暂存区

执行git add HEAD 可以查看暂存区和最新提交的差别。

$ git diff HEAD

diff --git a/README.md b/README.md

index fec5601..203cf31 100644

--- a/README.md

+++ b/README.md

@@ -1 +1,2 @@

# Hello

+## This is the subtitle

 

执行git commit –m "this is your description" 将文件提交。

 

git branch:显示分支列表,并标识当前所在分支

$ git branch

* master

 

git checkout –b:创建并且切换分支

$ git checkout -b branch-a

M README.md

Switched to a new branch ‘branch-a‘

 

此时再运行git branch,显示当前分支为branch-a

$ git branch

* branch-a

Master

在branch-a这个分支操作并提交代码,代码回提交到branch-a这个分支而不会提交到master分支。

 

举例:

在branch –a这个分支中修改README.md文件,增加一行:## this is branch-a 并提交,

$ git diff

diff --git a/README.md b/README.md

index fec5601..803aa74 100644

--- a/README.md

+++ b/README.md

@@ -1 +1,4 @@

# Hello

+## This is the subtitle

+

+## this is branch-a

 

切换至master分支

$ git checkout master

Switched to branch ‘master‘

Your branch is up-to-date with ‘origin/master‘.

 

查看master中的README.md文件,无增加行。

$ cat README.md

# Hello

 

git merge:合并分支

首先我们切换回master分支,

$ git checkout master

Switched to branch ‘master‘

Your branch is up-to-date with ‘origin/master‘.

 

然后,合并master和branch-a这个分支

执行:git merge --no-ff branch-a

系统提示合并之前需要输入一些描述信息,按ESC并输入 :wq退出即可,

$ git merge --no-ff branch-a

Merge made by the ‘recursive‘ strategy.

README.md | 3 +++

1 file changed, 3 insertions(+)

 

git log:查看历史提交记录

commit 182053ec7e8683e381996c683a7b04f43be57bd3

Merge: b1444e3 2806d72

Author: zenghui <zengh927@163.com>

Date: Tue Dec 15 19:31:02 2015 +0800

 

Merge branch ‘branch-a‘

 

commit 2806d720ed728ca55cc32c3091879eae8c8b5b5e

Author: zenghui <zengh927@163.com>

Date: Tue Dec 15 19:24:27 2015 +0800

 

add branch-a

 

commit b1444e36a2df109ce5414f5f00fd1ea97f3c1492

Author: zenghui <zengh927@163.com>

Date: Mon Dec 14 21:40:28 2015 +0800

 

Add T.java File

 

commit 0f2c6b68e711d74e35490bec6e6d8ab01e6a29d9

Author: GreyZeng <410486047@qq.com>

Date: Mon Dec 14 21:25:15 2015 +0800

 

git reset --hard + 哈希值:回溯到指定版本

执行:git reset --hard b1444e36a2df109ce5414f5f00fd1ea97f3c1492 回溯到branch-a分支创建之前。

$ git reset --hard b1444e36a2df109ce5414f5f00fd1ea97f3c1492

HEAD is now at b1444e3 Add T.java File

 

处理冲突

举例:

在master的README.md中增加一行:## FROM MASTER并提交

合并master和branch-a分支

$ git merge --no-ff branch-a

Auto-merging README.md

CONFLICT (content): Merge conflict in README.md

Automatic merge failed; fix conflicts and then commit the result.

显示冲突

用编辑器打开README.md文件发现:

# Hello

<<<<<<< HEAD

## FROM MASTER

=======

## This is the subtitle

 

## this is branch-a

>>>>>>> branch-a

此时,只要手动修改一下文件:

# Hello

## FROM MASTER

## This is the subtitle

 

## this is branch-a

再次执行git add 和 git commit 即可

 

git push:推送至远程仓库

推送master到远程仓库:

$ git push -u origin master

Counting objects: 9, done.

Delta compression using up to 4 threads.

Compressing objects: 100% (8/8), done.

Writing objects: 100% (9/9), 889 bytes | 0 bytes/s, done.

Total 9 (delta 0), reused 0 (delta 0)

To https://github.com/GreyZeng/Hello.git

b1444e3..6840c0a master -> master

Branch master set up to track remote branch master from origin.

 

也可以推送某个分支到远程仓库

比如:推送branch-a到远程仓库,需要执行以下命令:

$ git checkout branch-a

Switched to branch ‘branch-a‘

 

$ git push -u origin branch-a

Total 0 (delta 0), reused 0 (delta 0)

To https://github.com/GreyZeng/Hello.git

* [new branch] branch-a -> branch-a

Branch branch-a set up to track remote branch branch-a from origin.

 

git clone:获取远程仓库

首先我们切换到其他目录下:

执行git clone https://github.com/GreyZeng/Hello.git

$ git clone https://github.com/GreyZeng/Hello.git

Cloning into ‘Hello‘...

remote: Counting objects: 15, done.

remote: Compressing objects: 100% (12/12), done.

remote: Total 15 (delta 0), reused 12 (delta 0), pack-reused 0

Unpacking objects: 100% (15/15), done.

Checking connectivity... done.

从而获取到远程仓库

 

同时,我们可以从远程仓库获取之前推送的branch-a这个分支

$ git checkout -b branch-a origin/branch-a

Branch branch-a set up to track remote branch branch-a from origin.

Switched to a new branch ‘branch-a‘

 

git pull:获取最新的远程仓库:

假如有人在master分支上push了新的代码,本地需要同步最新代码

$ git pull origin master

remote: Counting objects: 3, done.

remote: Compressing objects: 100% (3/3), done.

remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0

Unpacking objects: 100% (3/3), done.

From https://github.com/GreyZeng/Hello

* branch master -> FETCH_HEAD

6b04921..c2c9aa0 master -> origin/master

Updating 6b04921..c2c9aa0

Fast-forward

README.md | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)

Github教程(1)

标签:

原文地址:http://www.cnblogs.com/greyzeng/p/5049400.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!