Git基本操作3

衔接上文,本文主要讲分支操作和日志操作


日志操作

当发现程序有bug或其他问题时,可以使用日志中的信息回退到以前提交的版本

修改web1.html从


	
		This is web page one
	


	
		This is web page one
		
And I will altrnate it

逐步提交到本地仓库

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git add web1.html

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git commit -m "Version two"
[master 5751ce7] Version two
 1 file changed, 2 insertions(+)

使用git log可以看到有两次不同的提交

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git log
commit 5751ce772771715a5673730cd7b45092eba78cda (HEAD -> master)
Author: Jason 
Date:   Wed Mar 14 11:52:31 2018 -0700

    Version two

commit 0ed3e441adc41f2edfe14e4361af84134c4ececc
Author: Jason 
Date:   Wed Mar 14 01:08:48 2018 -0700

    Version one

可以用git check 来回退到之前的版本

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git checkout 0ed3e441adc41f2edfe14e4361af84134c4ececc
Note: checking out '0ed3e441adc41f2edfe14e4361af84134c4ececc'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b 

HEAD is now at 0ed3e44... Version one

用文件编辑器打开文件会发现文件会变回Version one的状态


	
		This is web page one
	

日志也会回滚到Version one的状态

hp@pc MINGW64 ~/Desktop/Blog/Git test ((0ed3e44...))
$ git log
commit 0ed3e441adc41f2edfe14e4361af84134c4ececc (HEAD)
Author: Jason 
Date:   Wed Mar 14 01:08:48 2018 -0700

    Version one


分支管理

git的分支如同一棵树,每个分支相当于一个子的本地仓库,互相之间的版本都不相同。其中Master为主要的分支



查看分支

使用git branch可以查看当前分支信息,带*号的是当前所处的分支,上一小节中回退到日志的版本会导致新的分支产生。

hp@pc MINGW64 ~/Desktop/Blog/Git test ((0ed3e44...))
$ git branch
* (HEAD detached at 0ed3e44)
  master

跳转分支

使用git checkout 可以转到其他分支

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git checkout master

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git branch
* master

如果是自动创建的分支(如上一节回退到以往日志版本)发生修改并且已经提交到了本地仓库

hp@pc MINGW64 ~/Desktop/Blog/Git test ((3dc4dd7...))
$ git log
commit 3dc4dd7c709dbcfe1c158d29e5bd665c5ec1580f (HEAD)
Author: Jason 
Date:   Wed Mar 14 12:35:22 2018 -0700

    Revision one

commit 0ed3e441adc41f2edfe14e4361af84134c4ececc
Author: Jason 
Date:   Wed Mar 14 01:08:48 2018 -0700

    Version one

则转到别的分支会提示用户保存, 同时给出一个id号码用于保存当前分支

hp@pc MINGW64 ~/Desktop/Blog/Git test ((3dc4dd7...))
$ git checkout master
Warning: you are leaving 1 commit behind, not connected to
any of your branches:

  3dc4dd7 Revision one

If you want to keep it by creating a new branch, this may be a good time
to do so with:

 git branch  3dc4dd7

Switched to branch 'master'

创建分支

使用 git branch 可以创建新的分支,新分支是当前分支的一个拷贝

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git branch br1

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git branch
  br1
* master

使用系统提供的保存号码(上一小节删除分支给出)可以重新创建该分支,相当于保存了下来

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git branch rev1 3dc4dd7

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git branch
  br1
* master
  rev1

重命名分支

使用git branch -m name1 name2可以将name1的分支更名为name2

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git branch -m br1 br2

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git branch
  br2
* master
  rev1

删除分支

使用git branch -D 可以将指定名字的分支删除

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git branch -D br2
Deleted branch br2 (was 5751ce7).


合并分支

使用git merge 将指定名字的分支合并到当前分支

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git merge rev1
Auto-merging web1.html
Merge made by the 'recursive' strategy.
 web1.html | 1 +
 1 file changed, 1 insertion(+)

中途系统会提示输入备注信息



上一篇:Git基本操作2

你可能感兴趣的:(Git)