• 利用Git进行协作


    1 使用分支

    在协作环境下,几个开发者共享并且更改同一份源代码是很常见的。一些开发者可能在修复bugs,另一些开发者可能在开发新特征。同时进行这些事时,需要有一个系统来管理同一份源码的不同版本。

    分支允许每个开发人员从原始代码库创建分支,在分支上进行工作允许我们将其他工作隔离开。还可以帮助Git在后续轻松合并版本。

    1.1 Git 分支是什么

    Git分支本质上是一条独立的开发线。在创建新功能或者修复bug时我们可以利用分支,因为分支可以将我们的工作与团队中其他成员的工作隔离开来。

    在这里插入图片描述
    A git branch is an independent line of development taken from the same source code.

    不同的分支可以合并到任何一个分支中,只要它们属于同一个库。

    下图说明了如何使用分支并行进行开发:

    在这里插入图片描述
    Multiple development projects taking place using the same source code.

    分支使我们能够将自己的工作与其他工作隔离开来。主分支或其他分支中的更改不会影响我们的分支,除非我们决定从这些分支中提取最新的更改。

    为每个任务创建一个新分支是很常见的做法,这种方法使其他人能够轻松识别更改的内容,并使回溯变得简单。

    1.2 创建一个分支

    创建一个分支不会改变库本身,it simply points out the commit

    例如,我们可以通过如下的分支命令创建一个称为“issue1”的分支:

    git branch issue1
    
    • 1

    下图显示了创建分支时发生的情况。库是相同的,but a new pointer is added to the current commit.

    在这里插入图片描述

    2 切换分支

    git checkout命令允许我们通过更新工作树中的文件来切换分支。

    我们可以将其视为在不同工作区之间切换的一种方式。

    2.1 Git HEAD

    HEAD用于表示分支的当前快照。对于一个新的库,Git在默认情况下将HEAD指向master分支。更改HEAD指向的位置将更新当前活动分支。

    注意~^符号are used to point to a position relative to a specific commit. The symbols are used together with a commit reference, typically HEAD or a commit hash.

    ~ refers to the th grandparent. HEAD~1 refers to the commit’s first parent. HEAD~2 refers to the first parent of the commit’s first parent.

    ^ refers to the the th parent. HEAD^1 refers to the commit’s first parent. HEAD^2 refers to the commit’s second parent. A commit can have two parents in a merge commit.

    在这里插入图片描述

    2.2 Git 隐藏

    当我们切换到工作树中有未提交更改(或添加了新文件)的另一个分支时,这些未提交的更改也将被携带到我们切换到的新分支中。Changes that you commit will be committed to the newly switched branch.

    但是,如果Git发现新切换分支中的文件与上一个分支的未提交更改之间存在冲突,那么Git不允许我们切换到另一个分支。在切换分支之前,必须先提交或隐藏这些更改。

    You can think of stash as a drawer to store uncommitted changes temporarily. Stashing allows you to put aside the “dirty” changes in your working tree and continue working on other things in a different branch on a clean slate.

    Uncommitted changes that are stored in the stash can be taken out and applied to the original branch and other branches as well.

    3 远端分支

    Although Git is local on your computer, you can also have remote copies of a repository. Remote repositories can be on a private central server or even simply on a coworker’s computer.

    You can also have a remote repository hosted using an online service–such as Backlog.

    You can retrieve others’ changes to the repository or move your local copy of a repository to the remote server.

    4 Pull 远端分支

    我们可以使用git pull命令将远端库中的最新更改应用于本地库。

    例如,假设远端分支是本地分支的upstream,那么远端分支(origin/master)会包括所有属于本地分支(master)的修改:

    在这里插入图片描述
    在这种情况下,如果我们要应用merge将远端分支(origin/master)合并到本地分支(master)中,这将会是一个fast-forward merge
    在这里插入图片描述
    However, if there are changes in the local master branch that are not present in the remote origin/master branch, the git pull command will execute a merge and create a merge commit that ties those changes together.
    在这里插入图片描述
    When a pull is executed, a merge commit will be automatically created in the local repository. If there is a conflict, you will have to resolve the conflict and commit the merge manually.

    在这里插入图片描述

    5 获取远端分支

    当我们执行pull命令时,远端分支中的changes会自动合并到我们的当前分支中。如果我们想获取远端的changes到那时又不想合并到当前的分支中,我们可以执行git fetch命令。

    fetch会下载那些并不存在于我们分支中的changes。 The FETCH_HEAD ref can be used to track the fetched changes from the remote repository.

    The revision history will look like below when both the remote and local branch contain different descendants.

    在这里插入图片描述
    Once changes are fetched, you can apply those changes to your local repository by merging in FETCH_HEAD or by executing a pull.

    在这里插入图片描述
    Once FETCH_HEAD has been merged, the revision history will yield the same result as a git pull operation. Pull is essentially a simultaneous execution of fetch and merge operations.

  • 相关阅读:
    在浏览器页面提交信息和上传文件
    Oracle数据库ORA-12520问题处理
    汉服新闻查询易语言代码
    合宙esp32 环境搭建和使用方法
    Redis 排查大 key 的3种方法,优化必备
    RocketMQ回顾整理
    Java SE 学习笔记(十四)—— IO流(3)
    力扣刷题记录163.1-----684.冗余连接
    MySQL的select语句
    atof函数的实现
  • 原文地址:https://blog.csdn.net/kking_edc/article/details/126124038