Git分支Branch使用指南

引言: Git由于其良好的分布式特性,被广为采用,本文将综述其核心的关键指令。

创建Branch

git checkout -b ‘branch name’

查看远程分支

git branch -a

Options:

-a 查看所有的branch 
-r 查看远程的branch 
-l 查看本地的brnach

查看本地分支branch

git branch

切换branch

git checkout ‘branch_name’

删除本地branch

git branch -D br_name

删除远程branch

git push origin :br (origin 后面有空格)

提交指令

新增文件到Git管理之下

git add xxx.java

提交到本地的Repository

git commit -m ‘comment here’ xxx.java ….

-m : 这里主要是提交代码变化的若干注释 
-a: 指一次提交所有的变化文件列表

push指令

推送到远程主机的master

git push origin master

将当前分支推送到远程Repository

git push origin

将本地所有的branch推送到服务器上

git push –all origin

fetch指令

含义: 将远程的代码下载到本地,不进行merge

git fetch origin master

查看本地master与远程master之间的差别 
git log -p master..origin/master

合并代码 
git merge origin/master

另外一种更为明确的做法是当远程的代码下载到本地作为一个branch,然后合并

git fetch origin master:t-branch 
git diff t-branch 
git merge t-branch

Pull 指令

将远程的代码下载到本地,并自动进行合并

git pull origin master

一般情况下,推荐使用fetch,根据实际情况决定是否与远程 master代码进行合并。

Tag指令

创建Tag

git tag -a ‘tag_name_v0.1.2’ -m ‘comment message’

查询当前所有的tag

git tag

按照模式匹配来查询Tags

git tag -l ‘v0.1.*’

将当前的特定tag推送到远程

git push origin tag_name

将当前所有的tag都推送到远程

git push origin –all tags

Merge指令

切换到master branch

git checkout master 
将xxx_branch合并到master上 
git merge xxx_branch

查看git master信息

git remote show origin

* remote origin
  Fetch URL: http://source.xx.com/app/xx-ImageService.git
  Push  URL: http://source.xx.com/app/xx-ImageService.git
  HEAD branch: master
  Remote branch:
    master tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (fast-forwardable)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

总结

这里的总结无法一一覆盖所有的用法,更多详细的用法,可以查看git help,获取更多信息。

猜你喜欢

转载自my.oschina.net/architectliuyuanyuan/blog/1622849