揭开Git分支的面纱

简介: 每一次stage、commit背后,Git都做了些什么?A branch in Git is simply a lightweight movable pointer to one of these commits. The default branch name in Git is master. As you start making commits, you’re given a master branch that points to the last commit you made. Every time you commit, the master branch pointer

揭开Git分支的面纱



Branches in a Nutshell

Branching means you diverge from the main line of development and continue to do work without messing with that main line


Git can change the way that you develop


每一次stage、commit背后,Git都做了些什么?

A branch in Git is simply a lightweight movable pointer to one of these commits. The default branch name in Git is master. As you start making commits, you’re given a master branch that points to the last commit you made. Every time you commit, the master branch pointer moves forward automatically.


How does Git know what branch you’re currently on? It keeps a special pointer called HEAD


1.分支是什么?


Branching means you diverge from the main line of development and continue to do work without messing with that main line


2 揭秘每一次stage、commit背后故事


when you stage:


Staging the files computes a checksum for each one


stores that version of the file in the Git repository (Git refers to them as blobs),


adds that checksum to the staging area


when you create a commit


stores blobs as a tree object

create a commit object

``commit object 存储着对tree object`的引用以及此次提交的其他信息



20200423191648613.png


3 Git中的branch

A branch in Git is simply a lightweight movable pointer to one of these commits.


如下面的testing分支


How does Git know what branch you’re currently on? It keeps a special pointer called HEAD


HEAD指针指向当前所在分支,切换分支,就是将HEAD指针指向该分支



20200423191709234.png


4 Git分支的基本操作

创建分支


git branch 分支名


删除分支


git branch -d 分支名


切换分支


git checkout 分支名


创建并切换分支


git checkout -b 分支名


查看各个分支log


git log --oneline --decorate --graph --all


git log默认是查看当期分支下的log


目录
相关文章
|
28天前
|
开发工具 git
图解Git——分支的新建与合并《Pro Git》
在Git开发中,新建与合并分支是常见的操作。以实际开发为例:为实现新需求创建分支`iss53`进行开发;遇紧急Bug时,切换至线上分支创建`hotfix`修复并合并回线上分支,再切换回`iss53`继续工作。完成`iss53`后,切换到`master`合并。若出现冲突,使用`git status`查看,手动编辑解决冲突后标记为已解决并提交。图形化工具如`git mergetool`也可辅助解决冲突。
44 9
|
28天前
|
开发工具 git 开发者
图解Git——分支简介《Pro Git》
Git 分支是其核心特性之一,允许开发者从主开发线分离工作,避免干扰主线。传统版本控制系统创建分支效率低,而Git的分支创建和切换非常轻量高效。
70 9
|
2天前
|
开发工具 git 开发者
vscode+git解决远程分支合并冲突
通过这些详细步骤,您可以掌握如何使用VSCode和Git高效地解决远程分支合并冲突,提高开发效率和代码质量。希望这些内容对您的学习和工作有所帮助。
125 86
|
28天前
|
存储 项目管理 开发工具
图解Git——分支开发工作流《Pro Git》
分支开发工作流利用Git的分支功能,支持灵活的项目管理。长期分支如`master`和`develop`分别保存稳定和开发中的代码;短期主题分支用于开发单一特性或修复问题,完成后合并到主分支。此模式确保代码稳定性,支持并行开发、便于审查和灵活调整。建议维护明确的长期分支,保持主题分支短小精悍,并定期清理无用分支。配置上可保护关键分支,遵循命名规范。
54 7
|
28天前
|
存储 缓存 Java
图解Git——远程分支《Pro Git》
远程分支是 Git 中用于管理分布式协作的关键概念。远程引用指向远程仓库中的分支和标签,常用 `git ls-remote` 或 `git remote show` 查看。日常开发中,通常使用远程跟踪分支(如 `origin/main`)与远程分支交互,简化远程仓库状态的管理和使用。远程跟踪分支记录远程分支的状态,但本身只读。
27 6
|
4月前
|
开发工具 git
git学习四:常用命令总结,包括创建基本命令,分支操作,合并命令,压缩命令,回溯历史命令,拉取命令
这篇文章是关于Git常用命令的总结,包括初始化配置、基本提交、分支操作、合并、压缩历史、推送和拉取远程仓库等操作的详细说明。
173 1
git学习四:常用命令总结,包括创建基本命令,分支操作,合并命令,压缩命令,回溯历史命令,拉取命令
|
4月前
|
开发工具 git 开发者
关于git 解决分支冲突问题(具体操作,包含截图,教你一步一步解决冲突问题)
本文通过具体操作和截图,详细讲解了如何在Git中解决分支冲突问题,包括如何识别冲突、手动解决冲突代码、提交合并后的代码,以及推送到远程分支。
791 3
关于git 解决分支冲突问题(具体操作,包含截图,教你一步一步解决冲突问题)
|
5月前
|
缓存 开发工具 git
Git创建分支以及合并分支
在Git中,创建分支使用`git branch [branch_name]`,切换分支使用`git checkout [branch_name]`。修改文件后,通过`git add [file]`添加到暂存区,然后`git commit`提交到本地仓库。如果是新建分支的第一次推送,使用`git push origin [branch_name]`推送到远程仓库,之后可以简化为`git push`。合并分支时,使用`git merge [branch_name]`将指定分支的更改合并到当前分支。
143 2
Git创建分支以及合并分支
|
4月前
|
开发工具 git
Git分支使用总结
Git分支使用总结
68 1
|
5月前
|
测试技术 开发工具 git
掌握 Git 分支策略:提升你的版本控制技能
在现代软件开发中,版本控制至关重要,Git 作为最流行的分布式版本控制系统,其分支管理策略对于高效协作和代码维护尤为重要。本文介绍了几种常用的 Git 分支策略,包括主线开发模型、功能分支模型、Gitflow 工作流和 Forking 工作流,并探讨了如何根据项目需求选择合适的分支模型。通过保持 `master` 分支稳定、及时合并清理分支、使用命名规范、利用 Pull Request 进行代码审查及自动化测试等最佳实践,可以显著提升团队协作效率和软件质量。掌握这些策略将帮助开发者更好地管理代码库,加快开发流程。