How To Use Git Branches

本文介绍如何使用Git创建和管理分支,包括创建master和develop两个分支,并演示如何将develop分支的更改合并到master分支中。

This article is the third installment in the "Using Git" series. It assumes that you have read both theinstallation article and the article on how to use git effectively.

In the world of version control systems, GIT is arguably one of the best in terms of flexbility. It's very easy to learn the syntax and to figure out how git can best serve your workflow and your environment.

This tutorial will teach you how to create two branches (master and develop) and how to merge code from the development stage to production.

A branch, at its core, is a unique series of code changes with a unique name. Each repository can have one or more branches.

By default, the first branch is called "master".

Viewing branches

Prior to creating new branches, we want to see all the branches that exist. We can view all existing branches by typing the following:

git branch -a

Adding the "-a" to the end of our command tells GIT that we want to see all branches that exist, including ones that we do not have in our local workspace.

The output will look similiar to the following:

* master
  remotes/origin/master

The asterisk next to "master" in the first line of the output indicates that we are currently on that branch. The second line simply indicates that on our remote, named origin, there is a single branch, also called master.

Now that we know how to view branches, it time create our first one.

Creating branches

As stated in the beginning of this article, we want to have a development and a production setup for our coding environment.

We are going to treat the default "master" branch as our production and therefore need to create a single branch for development, or pre-production.

To create a new branch, named develop, type the following:

git checkout -b develop

Assuming we do not yet have a branch named "develop", the output would be as follows:

Switched to a new branch 'develop'

In the case of a branch by that name already existing, GIT would tell us so:

fatal: A branch named 'develop' already exists.

You can switch back and forth between your two branches, by using the git checkout command:

git checkout master

or

git checkout develop

Assuming the branch that you are trying to switch to exists, you'll see output similiar to the following:

Switched to branch 'master'

If you try to switch to a branch that doesn't exist, such as

git checkout nosuchbranch

Git will tell you:

error: pathspec 'nosuchbranch' did not match any file(s) known to git.

Now that we have multiple branches, we need to put them to good use. In our scenario, we are going to use our "develop" branch for testing out our changes and the master branch for releasing them to the public.

To illustrate this process, we need to switch back to our develop branch:

git checkout develop

Making changes to our develop branch

On this branch, we are going to create a new blank file, named "develop". Until we merge it to the master branch (in the following step), it will not exist there.

touch develop 

Just as in the previous tutorial, we need to tell git that we want to track this new file.

We can add the "develop" file, by typing:

git add develop 

The above set of commands will create a blank file, named "develop", and add it to GIT.

We also need to commit this file, which will attach this file to the branch we're currently on, which is "develop".

git commit -m "develop file" develop 

This file now exists on the develop branch; as we're about to find out, it doesn't exist on the master branch.

First, we are going to confirm that we are currently on the develop branch. We can do this by typing the following:

git branch 

The output should appear similar to the following:

* develop
  master

We learned earlier that the asterisk next to the branch name indicates that we are currently on that branch.

Running the "ls" command will show us that the two files exist:

ls

The output will show us that both of our files, respectively named "file" and "develop", are found:

develop file

Merging code between branches

The interesting part comes after we switch back to our master branch, which we can do with the git checkout command:

git checkout master

To ensure that we are on the master branch, we can run type the following:

git branch 

The output will tell us which branch we are one, indicated by the asterisk.

  develop 
* master

Running "ls" again, it appears that our new file is missing.

file

It's not missing - it's on our develop branch and we are on our master branch.

In our scenario, this file represents any change to any file (or a whole new file) that has passed all testing on our development branch,and is ready to be in production. The process of moving code between branches (often from development to production) is known as merging.

It is important to remember when merging, that we want to be on the branch that we want to merge to.

In this case, we want to merge from our develop branch, where the "develop" file exists, to our master branch.

Keeping that in mind, considering that we are already on the master branch, all we have to do is run the merge command.

One of the options that we can pass to the merge command, namely "--no-ff", means we want git to retain all of the commit messages prior to the merge. This will make tracking changes easier in the future.

To merge the changes from the develop branch to the master branch, type the following:

git merge develop --no-ff

The output of the command will be similiar to the following:

Merge made by the 'recursive' strategy.
 0 files changed
 create mode 100644 develop

Running the ls command again will confirm that our "develop" file is now on our master branch.

develop file

The last thing we now need to do, to make this change on our remote server is to push our changes, which we can do with the help of the git push command.

git push

You will see output similar to following, confirming that your the merge from your develop branch to the master branch on your remote server:

Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 332 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
To ssh://git@git.domain.tld/repository
   9af2dcb..53649cf  master -> master

Conclusion

By following the above tutorial, you should have a working dual-branch workflow setup and hopefully a working understanding about how branching works in GIT. Let us know what you think in the comments!

源码地址: https://pan.quark.cn/s/a4b39357ea24 欧姆龙触摸屏编程软件MPTST 5.02是专门为欧姆龙品牌的工业触摸屏而研发的编程解决方案,它赋予用户在直观界面上构建、修改以及排错触摸屏应用程序的能力。 该软件在工业自动化领域具有不可替代的地位,特别是在生产线监视、设备操控以及人机互动系统中发挥着核心作用。 欧姆龙MPTST(Machine Process Terminal Software Touch)5.02版本配备了多样化的功能,旨在应对不同种类的触摸屏项目要求。 以下列举了若干核心特性:1. **图形化编程**:MPTST 5.02采用图形化的编程模式,允许用户借助拖拽动作来设计屏幕布局,设定按钮、滑块、指示灯等组件,显著简化了编程流程,并提升了工作效率。 2. **兼容性**:该软件能够适配欧姆龙的多个触摸屏产品线,包括CX-One、NS系列、NJ/NX系列等,使用户可以在同一个平台上完成对不同硬件的编程任务。 3. **数据通信**:MPTST 5.02具备与PLC(可编程逻辑控制器)进行数据交互的能力,通过将触摸屏作为操作界面,实现生产数据的显示与输入,以及设备状态的监控。 4. **报警与事件管理**:软件中集成了报警和事件管理机制,可以设定多种报警标准,一旦达到预设条件,触摸屏便会展示对应的报警提示,助力操作人员迅速做出响应。 5. **模拟测试**:在设备实际连接之前,MPTST 5.02支持用户进行脱机模拟测试,以此验证程序的正确性与稳定性。 6. **项目备份与恢复**:为了防止数据遗失,MPTST 5.02提供了项目文件的备份及还原功能,对于多版本控制与团队协作具有显著价值。 7. **多语言支持**:针对全球化的应...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值