文档章节

How to Work with Git Branches

Kolosek
 Kolosek
发布于 2018/04/16 16:19
字数 594
阅读 11
Git">收藏 0

In the current era, most software development companies work in a collaborative environment where several developers contribute to the same source code. While some will be fixing bugs the others would be implementing new and different features. The problem raises, how to maintain different versions of the same code base?

This is where the branch function shines! Branch allows each developer to isolate his/her work from others by creating a new branch from the original code base.

What Is a Branch?

Branch is an independent line of development. It works as a pointer to your next commits. Whenever a new branch is created, Git creates a new pointer while keeping the original code base untouched.

When you make your first commit in a repository, Git will automatically create a master branch by default. Every next commit you make will go to the master branch until you decide to create and switch over to another branch.

Creating Branches

Let's start with creating a new branchgit branch hello-world.

git-branch

This only creates the new branch. To start working on it, you will need to switch to the branch with git checkout. Now, you are ready to use standard git add and git commit commands.

You can see two different branches pointing to the same commit. How does Git know which branch is currently checked out? This is where the HEAD pointer comes into play!

git-branch-head-master

HEAD always points to the currently checked out branch or commit. In our case, it is the master. Let's git checkout hello-world and see what happens.

Git-branch-head-hello

As you can see, the HEAD is now pointing to the hello-world branch instead of master. The next step is to modify some files and create a new commit with git commit -m "commit message"

git-branch-new-commit

new commit C5 was created in the branch hello-world. Pointers always move to the latest commit in that branch that we checked out. Changes in the hello-world branch did not affect any other branch. Branching enables you to isolate your work from others.

It is a common practice to create a new branch for each task (e.g. bug fixing, new features etc), which is a good practice because it allows others to easily identify what changes to expect, and also for backtracking purposes to understand why a particular code change is implemented. You can read more at Git Beginner's Guide for Dummies.

Create your own Ruby on Rails project and try it out! Make Rspec tests on a different branch and commit the changes.

Detached HEAD

As we have said before, HEAD always points to the currently checked out branch or commit. Checkout to a commit and see what happens with git checkout C0.

git-branch-checkout-commit

Now, the HEAD is pointing to C0. We are currently checkedout to a remote branch. Is it possible to create a new commit while checkout to one? Time to find it out! git commit -m "commit message"

git-branch-head-detached

The HEAD is detached and moves together with each new commit created. The newly created commit C6 is pointing to C0, that is now acting like a branch, but it is not.

Commits that are not reachable by any branch or tag will be garbage collected and removed from the repository after 30 days.

To avoid this, we simply need to create a new branch for the newly created commit and checkout to it. git checkout -b hotfix C6.

git-branch-hotfix

Always use branches when you are solving new problems to avoid disturbing your co-worker's features!

This post is originally published on Kolosek Blog

© 著作权归作者所有

Kolosek
粉丝 0
博文 29
码字总数 20461
作品 0
塞尔维亚
CEO
私信 提问
git pull 没有想象中的那样怕怕啊-实战解决冲突

NAME git-pull - Fetch from and integrate with another repository or a local branch SYNOPSIS git pull [options] [<repository> [<refspec>…]] DESCRIPTION Incorporates changes fro......

Oscarfff
2016/07/25
4.5K
0
SmartGit 8.1 preview 4 发布,Git 客户端

SmartGit 8.1 preview 4 发布了。 SmartGit 是一个 Git 版本控制系统的图形化客户端程序。SmartGit8最重要的特点是,取代了以前的外向观提供了一个集中和紧凑视图到当前分支的日志,它跟踪分...

达尔文
2016/11/19
1K
1
PyCharm 5.1 Beta 发布,Python 集成开发环境

PyCharm 5.1 Beta 发布了 ,PyCharm 是由 JetBrains 打造的一款 Python IDE。 Python相关改进记录如下: Docker Compose support Docker container settings in run configurations Tox sup......

oschina
2016/03/04
1K
4
OSX上搭建一个好用的git(hub)环境

Step 1: Install Git brew install git bash-completion Configure things: git config --global user.name "cyper"git config --global user.email "345343747@qq.com"git config --global ......

cyper
2015/08/15
92
0
$ git reset --hard HEAD~1 出现的异常

$ git reset --hard HEAD~1 Unlink of file 'Hello.java' failed. Should I try again? (y/n) This could mean that another program is using the file, which ispreventing git from "movi......

天天顺利
2016/03/14
124
1

没有更多内容

加载失败,请刷新页面

加载更多

golang-字符串-地址分析

demo package mainimport "fmt"func main() {str := "map.baidu.com"fmt.Println(&str, str)str = str[0:5]fmt.Println(&str, str)str = "abc"fmt.Println(&s......

李琼涛
今天
4
0
Spring Boot WebFlux 增删改查完整实战 demo

03:WebFlux Web CRUD 实践 前言 上一篇基于功能性端点去创建一个简单服务,实现了 Hello 。这一篇用 Spring Boot WebFlux 的注解控制层技术创建一个 CRUD WebFlux 应用,让开发更方便。这里...

泥瓦匠BYSocket
今天
10
0
从0开始学FreeRTOS-(列表与列表项)-3

FreeRTOS列表&列表项的源码解读 第一次看列表与列表项的时候,感觉很像是链表,虽然我自己的链表也不太会,但是就是感觉很像。 在FreeRTOS中,列表与列表项使用得非常多,是FreeRTOS的一个数...

杰杰1号
今天
9
0
Java反射

Java 反射 反射是框架设计的灵魂(使用的前提条件:必须先得到代表的字节码的 Class,Class 类 用于表示.class 文件(字节码)) 一、反射的概述 定义:JAVA 反射机制是在运行状态中,对于任...

zzz1122334
今天
9
0
聊聊nacos的LocalConfigInfoProcessor

序 本文主要研究一下nacos的LocalConfigInfoProcessor LocalConfigInfoProcessor nacos-1.1.3/client/src/main/java/com/alibaba/nacos/client/config/impl/LocalConfigInfoProcessor.java p......

go4it
昨天
9
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部