git杂记-分支简介

  1. 分支创建

    //只创建分支不切换;
    $ git branch testing
    
    //创建并切换分支$ git checkout -b iss53
  2. 查看各个分支的指向对象

    $ git log --oneline --decorate
    f30ab (HEAD, master, testing) add feature #32 - ability to add new
    34ac2 fixed bug #1328 - stack overflow under certain conditions
    98ca9 initial commit of my project
    
    //当前 “master” 和 “testing” 分支均指向校验和以 f30ab 开头的提交对象。
  3. 分支切换

    $ git checkout testing
  4. 查看项目交叉历史

    $ git log --oneline --decorate --graph --all
    * c2b9e (HEAD, master) made other changes
    | * 87ab2 (testing) made a change
    |/
    * f30ab add feature #32 - ability to add new formats to the
    * 34ac2 fixed bug #1328 - stack overflow under certain conditions
    * 98ca9 initial commit of my project
  5. 合并分支,在当前分支合并某某分支

    1:如果当前分支是合并目标分支的直接祖先,就可以使用快速合并,当前分支的指针直接移动到目标分支;  例如:master分支合并hotfix分支
    $ git checkout master
    $ git merge hotfix
    Updating f42c576..3a0874c
    Fast-forward
     index.html | 2 ++
     1 file changed, 2 insertions(+)
    
    2:如果当前分支并不是合并目标分支的直接祖先,Git不得不做一些额外的工作。出现这种情况的时候,Git会使用两个分支的末端所指的快照(C4 和 C5)以及这两个分支的工作祖先(C2),做一个简单的三方合并。 例如:master分支合并iss53分支


    
    
  6. 删除分支

    //删除hotfix分支
    $ git branch -d hotfix
    Deleted branch hotfix (3a0874c).
  7. 合并的冲突处理方案

    //合并分支时产生冲突,指示index.html文件有冲突
    $ git merge iss53
    Auto-merging index.html
    CONFLICT (content): Merge conflict in index.html
    Automatic merge failed; fix conflicts and then commit the result.
    //观察文件状态
    $ git status
    On branch master
    You have unmerged paths.
      (fix conflicts and run "git commit")
    
    Unmerged paths:
      (use "git add <file>..." to mark resolution)
    
        both modified:      index.html
    
    no changes added to commit (use "git add" and/or "git commit -a")
    //使用工具打开冲突的文件
    
    <<<<<<< HEAD:index.html
    <div id="footer">contact : [email protected]</div>
    =======
    <div id="footer">
     please contact us at [email protected]
    </div>
    >>>>>>> iss53:index.html
    //把文件手工修改为我们想要的内容
    <div id="footer">
    please contact us at [email protected]
    </div>
    
    //重新添加到暂存库
    git add index.html
    
    //重新添加到版本库
    git commit -m ‘解决冲突文件index.html‘
  8. 查看所有的分支列表

    $ git branch
      iss53
    * master
      testing
    
    *代表的是当前的分支
  9. 查看每个分支的最后一次提交

    $ git branch -v
      iss53   93b412c fix javascript issue
    * master  7a98805 Merge branch ‘iss53‘
      testing 782fd34 add scott to the author list in the readmes
  10. 筛选哪些分支已经合并到当前分支,哪些分支没有合并到当前分支

    //查看哪些分支已经合并到当前分支;      使用git branch -d iss53可以正常删除已合并的分支;
    $ git branch --merged
      iss53
    * master
    
    //查看哪些分支目前还没有合并到当前分支;使用git branch -d testing不能正常删除没有被合并的分支,需用git branch -D testing强制删除
    $ git branch --no-merged
      testing
  11. 分支开发工作流
    1. 长期分支
    2. 特性分支
  12. 远程分支
    1. 远程分支的存在方式

      • 以 (remote)/(branch) 形式命名,例如: origin/master
    2. 克隆之后的服务器与本地仓库
      • 系统一个本地分支会对应一个远程分支,默认状况下会一一对应。
    3. 更新本地仓库的远程分支

      //远程主机的所有更新全部拉取到本地中;只拉取,不会自动合并;相当于origin/master只读分支不断向前走动
      $ git fetch <远程主机名>
      
      //只拉取特定的分支
      $ git fetch <远程主机名> <分支名>
      
      //拉取并自动合并$ git pull <远程主机名>
    4. 推送分支

      //推送serverfix分支
      $ git push origin serverfix
      Counting objects: 24, done.
      Delta compression using up to 8 threads.
      Compressing objects: 100% (15/15), done.
      Writing objects: 100% (24/24), 1.91 KiB | 0 bytes/s, done.
      Total 24 (delta 2), reused 0 (delta 0)
      To https://github.com/schacon/simplegit
       * [new branch]      serverfix -> serverfix
    5. 在远程分支的基础上进行工作
      1. 合并远程分支

        git merge origin/serverfix
      2. 在远程分支上创建一个分支

        $ git checkout -b serverfix origin/serverfix
        Branch serverfix set up to track remote branch serverfix from origin.
        Switched to a new branch ‘serverfix‘
    6. 跟踪分支

      //当运行fetch的时候,本地分支 sf(新建) 会自动从 origin/serverfix 拉取。$ git checkout -b sf origin/serverfix
      Branch sf set up to track remote branch serverfix from origin.
      Switched to a new branch ‘sf‘
      
      //设置本地已有的分支(当前分支)跟踪刚刚拉取下来的一个分支$ git branch -u origin/serverfix
    7. 列出所有的跟踪分支,并指出本地分支是否是领先、落后。

      $ git branch -vv
        iss53     7e424c3 [origin/iss53: ahead 2] forgot the brackets  //超前两个提交
        master    1ae2a45 [origin/master] deploying index fix          //与远程分支同步
      * serverfix f8674d9 [teamone/server-fix-good: ahead 3, behind 1] this should do it  //超前三个提交,落后一个提交
        testing   5ea463a trying something new                         //没有跟踪任何远程分支;
    8. 删除远程分支

      $ git push origin --delete serverfix
      To https://github.com/schacon/simplegit
       - [deleted]         serverfix
时间: 2024-08-23 12:50:25

git杂记-分支简介的相关文章

git常用命令及分支简介

1.git基本命令 1)git add 将想要快照的内容写入缓存区 2)git status -s "AM" 状态的意思是,这个文件在我们将它添加到缓存之后又有改动 3)git commit -m '第一次版本提交' -m选项添加备注信息 4)git clone url 使用 git clone 拷贝一个 Git 仓库到本地 5)git diff 查看执行 git status 的结果的详细信息 尚未缓存的改动:git diff 查看已缓存的改动: git diff --cached

git入门-分支

原地址:http://codingnow.cn/git/228.html1. git分支简介 使用分支可以让你从开发主线上分离开来,然后在新的分支上解决特定问题,同时不会影响主线.像其它的一些版本控制系统,创建分支需要创建整个源代码目录的副本.而Git 的分支是很轻量级的,因为Git保存的不是文件差异,而是一系列文件快照.在Git提交时,会保存一个commit对象,该对象包含一个指向暂存内容快照的指针,Git 会先计算每一个子目录的校验和( SHA-1 哈希字串),然后在 Git 仓库中将这些目

好代码是管出来的——Git的分支工作流与Pull Request

上一篇文章介绍了常用的版本控制工具以及git的基本用法,从基本用法来看git与其它的版本控制工具好像区别不大,都是对代码新增.提交进行管理,可以查看提交历史.代码差异等功能.但实际上git有一个重量级的功能“分支”,git的分支与其它工具的分支不同,git分支的操作完全在本地进行,所以可以快速的创建和切换. 版本控制工具除了对代码进行管理外,实际上它还影响了整个软件编码的工作流程,git因为其分支特性使得开发流程发生了变化,本文将从以下几点来介绍分支和git的工作流程: 版本控制管理分支简介 G

git branch 分支和分支合并

一般一个项目有一个默认的分支 master 主分支,然后可以有许多个分支,在别的分支上的操作不会影响到主分支.使用git branch查看当前多多少分支以及当前处于哪个分支上:执行git branch 分支名称  创建分支:执行 git checkout 分支名称 切换当前分支. 使用 git branch -d 分支名称 删除分支 使用git branch -m 当前分支名称 新分支名称   修改分支名称 [email protected] MINGW64 /c/laoni/PycharmPr

Git 学习笔记&lt;简介与安装&gt; (一)

Git,开源中国以及GitHub所使用的系统, Is A 一个分布式版本控制系统 Be Used to 为团队合作写代码提供方便的管理系统.几乎满足你所有关于合作写代码的幻想. Has 本地端:工作区.版本库 (版本库还含有一个暂存区) 远程仓库:版本库(用来储存版本库的服务器) How To Install Linux: 首先,先输入git,看看是否安装Git: $ gitThe program 'git' is currently not installed. You can install

git 操作分支

1. git 查看本地分支:git branch 2. git 查看所有分支:git branch -a 3. git 新建本地分支:git branch branchName 4. git 新建分支并切换: git checkout -b branchName 5. git 删除本地分支:git branch -d branchName 6. git 批量删除匹配到的本地分支: git branch |grep 'branchName' |xargs git branch -d 7. git

git创建分支并提交项目

git 创建分支, 切换分支, 合并分支, 删除分支及提交[commit提交到本地仓库push名利提交到远程服务器], 检出[pull], 冲突修改, 本地仓库同步远程服务器[pul和push命令l]操作 开发中常用操作 删除文件:git rm 文件名[删除本地git仓库文件, 提交后远程服务器上的文件才会消失] 查看状态:git status 添加记录:git add 文件名 或 git add . [将文件提交到远程git服务器上] 添加描述:git commit -m "描述或备注类似sv

git 远程分支创建与推送

git 远程分支创建与推送 原文地址:http://hi.baidu.com/lingzhixu/blog/item/4a9b830bb08a329fe850cd5b.html 本地分支的创建 本地分支的来源为执行git checkout -b <branch name> 的那个分支 例如现在有两个分支,master和b1 master 分支下有一个commit: commit1: add test1.c b1分支下有两个commit: commit2: add test2.c commit1

Git管理分支

管理分支:git branch 直至现在为止,我们的项目版本库一直都是只有一个分支 master.在 git 版本库中创建分支的成本几乎为零,所以,不必吝啬多创建几个分支.下面列举一些常见的分支策略,仅供大家参考: * 创建一个属于自己的个人工作分支,以避免对主分支 master 造成太多的干扰,也方便与他人交流协作. * 当进行高风险的工作时,创建一个试验性的分支,扔掉一个烂摊子总比收拾一个烂摊子好得多. * 合并别人的工作的时候,最好是创建一个临时的分支,关于如何用临时分支合并别人的工作的技