git 基于发布分支的开发

简介: 创建发布分支: (1) 软件hello-world的1.0发布版本库中有一个里程相对应. /home/jackluo/workspace/user1/workspace/hello-worldgit tag -n1 -l v* (2)基于里程v1.

创建发布分支:

(1) 软件hello-world的1.0发布版本库中有一个里程相对应.

/home/jackluo/workspace/user1/workspace/hello-world
git tag -n1 -l v*

(2)基于里程v1.0创建发布布hello-1.x.

   注:使用了git checkout 命令创建分支,最后一个参数v1.0是新分支 hello-1.x创建的基准点,如果没有里程,使用提交ID也是一样

[root@localhost hello-world]# git tag -n1 -l v*
v1.0            Release 1.0
[root@localhost hello-world]# git checkout -b hello-x.x v1.0
切换到一个新分支 'hello-x.x'

(3)用git rev-parse 命令可以看到hello-1.x分支对应的提交ID和里程v1.0指向的提交一致,但是和master不一样

   提示:因为里程v1.0是一个包含提交说明的里程,因此为了显示其对应的提交ID,使用特别的记法 "v1.0^{}".

[root@localhost hello-world]# git rev-parse hello-x.x v1.0^{} master
d901dd8170f67fec607828905d5fbd91e3272400
d901dd8170f67fec607828905d5fbd91e3272400
733dcf67eba976a61d0dc6396c9d23cb23568591

(4)开发者user1将分支hello-1.x推送到远程 共享版本库,因为开发者user2修改Bug时也要用到该分支

[root@localhost hello-world]# git push origin hello-x.x
Total 0 (delta 0), reused 0 (delta 0)
To /home/jackluo/workspace/repos/hello-world.git
 * [new branch]      hello-x.x -> hello-x.x

(5).开发者user2 从远程共享版本库获取新的分支。

    开发者user2执行git fetch命令,将远程共享版本库的新分支hello-1.x复制到本地引用origin/hello-1.x上.

[root@localhost hello-world]# cd ../../../user2/workspace/hello-world/
[root@localhost hello-world]# git fetch
remote: Counting objects: 22, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 3), reused 1 (delta 1)
Unpacking objects: 100% (4/4), done.
来自 /home/jackluo/workspace/repos/hello-world
 * [新分支]          hello-x.x  -> origin/hello-x.x
   d901dd8..733dcf6  master     -> origin/master

(6).开发者user2切换到hello-x.x分支.

[root@localhost hello-world]# git checkout -b hello-x.x origin/hello-x.x
分支 hello-x.x 设置为跟踪来自 origin 的远程分支 hello-x.x。
切换到一个新分支 'hello-x.x'

(1)编辑文件src/main.c,将"--help"字符串修改为"--help".

[root@localhost hello-world]# cd ../../../user1/workspace/hello-world/
[root@localhost hello-world]# vim src/main.c

(2)开发者user1的改动可以从下面的差异比较中看到.

[root@localhost hello-world]# git diff

(3) 执行提交

[root@localhost hello-world]# git add -u
[root@localhost hello-world]# git commit -m "Fix typo: -help to --help."
[hello-x.x ca43043] Fix typo: -help to --help.
 1 file changed, 2 insertions(+), 2 deletions(-)

(4).推送到远程共享版本库.

[root@localhost hello-world]# git push
warning: push.default 未设置,它的默认值将会在 Git 2.0'matching'
修改为 'simple'。若要不再显示本信息并在其默认值改变后维持当前使用习惯,
进行如下设置:

  git config --global push.default matching

若要不再显示本信息并从现在开始采用新的使用习惯,设置:

  git config --global push.default simple

参见 'git help config' 并查找 'push.default' 以获取更多信息。
('simple' 模式由 Git 1.7.11 版本引入。如果您有时要使用老版本的 Git,
为保持兼容,请用 'current' 代替 'simple' 模式)

Counting objects: 20, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 362 bytes | 0 bytes/s, done.
Total 4 (delta 3), reused 0 (delta 0)

开发者user2工作在发布分支.

 (1)进入开发者user2的工作区,并确保工作在hello-x.x分支中

[root@localhost hello-world]# cd ../../../user2/workspace/hello-world/
[root@localhost hello-world]# git checkout hello-x.x
已经位于 'hello-x.x'

(2)编辑文件 src/mai.c,修改代码中的Bug.

[root@localhost hello-world]# vim src/main.c 
[root@localhost hello-world]# git format-path jx/v1.1..jx/v1.2
git:'format-path' 不是一个 git 命令。参见 'git --help'。

您指的是这个么?
    format-patch
[root@localhost hello-world]# git format-patch jx/v1.1..jx/v1.2
0001-Bugfix-allow-spaces-in-username.patch
[root@localhost hello-world]# patch -p1 < 0001-Bugfix-allow-spaces-in-username.patch
patching file src/main.c
[root@localhost hello-world]# git diff
[root@localhost hello-world]# cd src/
[root@localhost src]# ll
总用量 12
-rw-r--r-- 1 root root 946 1月   2 14:48 main.c
-rw-r--r-- 1 root root 474 12月 30 20:39 Makefile
-rw-r--r-- 1 root root  98 12月 30 20:39 version.h.in
[root@localhost src]# make
version.h.in => version.h
cc    -c -o main.o main.c
cc -o hello main.o
[root@localhost src]# ./hello Jack Luo
Hi, Jack Luo.
(version: v1.0-dirty)
[root@localhost src]# git add -u
[root@localhost src]# git commit -m "Bugfix: allow spaces in username."
[hello-x.x 0ad2a9e] Bugfix: allow spaces in username.
 1 file changed, 8 insertions(+), 1 deletion(-)

开发者user2合并推送

     开发者user2在本地版本完成提交后,

[root@localhost src]# git pull
Already up-to-date.
[root@localhost src]# git log --graph --oneline
[root@localhost src]# cd ..
[root@localhost hello-world]# git checkout master
切换到分支 'master'
您的分支落后 'origin/master'1 个提交,并且可以快进。
  (使用 "git pull" 来更新您的本地分支)
[root@localhost hello-world]# git pull
更新 d901dd8..733dcf6
Fast-forward
 src/Makefile |  3 ++-
 src/main.c   | 41 ++++++++++++++++++++++++++++++++++++-----
 2 files changed, 38 insertions(+), 6 deletions(-)
[root@localhost hello-world]# git pull
Already up-to-date.
[root@localhost hello-world]# git push
Everything up-to-date
[root@localhost hello-world]# git checkout master
已经位于 'master'

发布分支的提交合并到主线.

1.操作

查看分支 hello-1.x的日志,确认要提交的ID.

  从下面的日志中可以看出分支hello-1.x 的最新提交是一个合并提交,可以分别用"hello-1.x^1和"hello-1.x^2"表示

[root@localhost hello-world]# git pull
Already up-to-date.
[root@localhost hello-world]# git log -3 --graph --oneline hello-x.x
*   9cc4e7b Merge branch 'hello-x.x' of /home/jackluo/workspace/repos/hello-worl
|\  
| * ca43043 Fix typo: -help to --help.
* | 0ad2a9e Bugfix: allow spaces in username.

(5)操作发生冲突,通过查看状态可以看到是在文件src/main.c上发生了冲突.

[root@localhost hello-world]# git status
# 位于分支 master
# 您正在做拣选操作。
#   (解决冲突并运行 "git cherry-pick --continue")
#   (使用 "git cherry-pick --abort" 以取消拣选操作)
#
# 未合并的路径:
#   (使用 "git add <file>..." 标记解决方案)
#
#    双方修改:     src/main.c
#
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#    0001-Bugfix-allow-spaces-in-username.patch
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[root@localhost hello-world]# git status
# 位于分支 master

2.冲突发生的原因,通过下面的命令可以看到底是哪些提交引起的冲突.

[root@localhost hello-world]# git log master...hello-x.x^1
commit 0ad2a9e90213aca2d882ba6fa49d4667db5e2106
Author: user2 <user2@sun.ossxp.com>
Date:   Thu Jan 2 14:51:14 2014 +0800

    Bugfix: allow spaces in username.

commit 733dcf67eba976a61d0dc6396c9d23cb23568591
Author: user1 <user1@sun.ossxp.com>
Date:   Thu Jan 2 11:34:45 2014 +0800

    Refactor: use getopt_long for arguments parsing.

3.冲突解决

 

 

目录
相关文章
|
9月前
|
存储 安全 开发工具
深度解决 Git “fatal: refusing to merge unrelated histories” 错误解析什么是历史分支优雅草卓伊凡
深度解决 Git “fatal: refusing to merge unrelated histories” 错误解析什么是历史分支优雅草卓伊凡
772 4
深度解决 Git “fatal: refusing to merge unrelated histories” 错误解析什么是历史分支优雅草卓伊凡
|
5月前
|
存储 缓存 数据处理
71_数据版本控制:Git与DVC在LLM开发中的最佳实践
在2025年的大模型(LLM)开发实践中,数据和模型的版本控制已成为确保项目可重复性和团队协作效率的关键环节。与传统软件开发不同,LLM项目面临着独特的数据版本控制挑战:
|
6月前
|
开发工具 git
Git版本控制工具合并分支merge命令操作流程
通过以上步聚焦于技术性和操作层面指南(guidance), 可以有效管理项目版本控制(version control), 并促进团队协作(collaboration).
1422 15
|
开发工具 git
图解Git——分支的新建与合并《Pro Git》
在Git开发中,新建与合并分支是常见的操作。以实际开发为例:为实现新需求创建分支`iss53`进行开发;遇紧急Bug时,切换至线上分支创建`hotfix`修复并合并回线上分支,再切换回`iss53`继续工作。完成`iss53`后,切换到`master`合并。若出现冲突,使用`git status`查看,手动编辑解决冲突后标记为已解决并提交。图形化工具如`git mergetool`也可辅助解决冲突。
330 9
|
开发工具 git 开发者
图解Git——分支简介《Pro Git》
Git 分支是其核心特性之一,允许开发者从主开发线分离工作,避免干扰主线。传统版本控制系统创建分支效率低,而Git的分支创建和切换非常轻量高效。
705 9
|
开发工具 git 开发者
vscode+git解决远程分支合并冲突
通过这些详细步骤,您可以掌握如何使用VSCode和Git高效地解决远程分支合并冲突,提高开发效率和代码质量。希望这些内容对您的学习和工作有所帮助。
2963 86
|
12月前
|
人工智能 前端开发 Java
用git rebase命令合并开发阶段中多条commit提交记录
通过 `git rebase`,可以合并多个提交记录,使开发历史更简洁清晰。操作分为 6 步:查看提交历史 (`git log --oneline`)、设置需合并的提交数 (`git rebase -i HEAD~N`)、修改动作标识为 `s`(squash)、保存退出编辑、调整提交信息、强制推送至远程仓库 (`git push -f`)。此方法适合清理本地无关提交,但若有团队协作或冲突风险,需谨慎使用以避免问题。
2028 60
|
存储 项目管理 开发工具
图解Git——分支开发工作流《Pro Git》
分支开发工作流利用Git的分支功能,支持灵活的项目管理。长期分支如`master`和`develop`分别保存稳定和开发中的代码;短期主题分支用于开发单一特性或修复问题,完成后合并到主分支。此模式确保代码稳定性,支持并行开发、便于审查和灵活调整。建议维护明确的长期分支,保持主题分支短小精悍,并定期清理无用分支。配置上可保护关键分支,遵循命名规范。
545 7
|
存储 缓存 Java
图解Git——远程分支《Pro Git》
远程分支是 Git 中用于管理分布式协作的关键概念。远程引用指向远程仓库中的分支和标签,常用 `git ls-remote` 或 `git remote show` 查看。日常开发中,通常使用远程跟踪分支(如 `origin/main`)与远程分支交互,简化远程仓库状态的管理和使用。远程跟踪分支记录远程分支的状态,但本身只读。
359 6
|
开发工具 git
git学习四:常用命令总结,包括创建基本命令,分支操作,合并命令,压缩命令,回溯历史命令,拉取命令
这篇文章是关于Git常用命令的总结,包括初始化配置、基本提交、分支操作、合并、压缩历史、推送和拉取远程仓库等操作的详细说明。
384 1
git学习四:常用命令总结,包括创建基本命令,分支操作,合并命令,压缩命令,回溯历史命令,拉取命令