有关使用 Git 的问题
关于 Git 命令的指南有很多,但我还没有看到很多解释开发人员在日常工作中实际如何使用它的指南。 我了解推、拉、提交等基础知识...但我不明白何时使用分支。
在本地存储库上:
- 主分支
您应该为每个分支创建一个新的克隆吗?
什么时候将本地分支与本地主分支合并?
谢谢。
There are a lot of guides on Git commands but I haven't seen many that explain how developers actually use it on a day-to-day basis. I understand the basics of push, pull, commit, etc... but I don't understand when to use branches.
On the local repo:
Should you create a new branch for every set of changes or is okay to work on the master branch?
Should you create a new clone for every branch?
When do you merge your local branches with your local master?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个品味问题。 您应该为您实现的每个主要功能创建一个分支(“主题分支”),从而能够在原始主分支上进行错误修复。 对于小型项目,在 master 分支上工作是可以的。
例如,如果您计划进行重大重新设计,跨越多个文件或在较长时间内对其进行处理,那么您绝对应该为其创建一个主题分支。 特别是如果您计划在两者之间使用“不稳定”提交,这是值得鼓励的。
也就是说,大多数时候在不同的分支上工作可能是合理的,因为可能很难提前知道某个主题需要多少工作。
不,你为什么要这么做? 只需创建一个新分支并使用 git checkout即可切换分支。
提示:了解 git stash 来临时“隐藏”所有本地修改,在使用分支时非常方便。
一旦“主题”完成,您应该将其与主主题合并。 之后,您可以删除主题分支。
您可以使用
git pull
、git push
或通过在中定义相应的
文件。ref
来指定哪个本地分支与哪个远程分支同步。 >.git/config当您想要在多台计算机或用户上共享分支时,您需要远程分支。
It's a matter of taste. You should create a branch for every major feature you implement ("topic branches"), thus beeing able to do bugfixes on the original master branch. For small projects, it is OK to work on the master branch.
For example, if you plan to do a major redesign, spanning multiple files or working on it over a longer period of time, you should definitely create a topic branch for it. Especially if you plan to use "unstable" commit in between, which is encouraged.
That said, it might be reasonable to work on a different branch most of the time since it might be hard to know in advance how much work a topic will need.
No. Why should you? Just create a new branch and use
git checkout <branchname>
to switch branches.Hint: Learn about
git stash
to temporarily "stash away" all local modifications, it is extremely handy when working with branches.Once the "topic" is finished you should merge it with the master. After that, you may delete the topic branch.
You can specify which local branch to synchronize with which remote branch using
git pull
,git push
or by defining the correspondingref
s in the.git/config
file.You need remote branches when you want to share a branch over multiple machines or users.
当我要做一些相关的更改时,我会创建一个新分支,我可能决定不保留这些更改,但在处理这些更改时会撕裂并破坏主分支。 特别是如果我想一路进行多次签到。
例如,假设我有一个简单的图形计算器应用程序,我想添加括号和运算顺序,但我不确定它是否会按照我想要的方式工作。 我会创建一个新分支,随时检查我的更改,如果我喜欢最后的结果,那么我将与 Master 合并。
如果我在工作时有人要求对已发布版本(基于 master 构建)进行错误修复,这尤其好。 我可以轻松切换到 master、修复错误、重建、重新发布,然后切换回我的开发分支。
我不建议为每个分支创建一个新的克隆,除非您不想在切换之前检查本地更改。 但即便如此,您也可以在切换分支、执行工作和切换回来时使用“git stash”命令来保存更改。
当您能够成功构建并且您认为您已经实现了您想要的功能时,您应该将本地更改与主分支合并。 不要破坏主分支。
您可以通过创建本地分支并在签出该本地分支时推送来创建远程分支。 仅当您想要备份或者其他人可能想要查看它时,您才应该推送新分支来远程创建它。 如果您只是在本地使用某个功能,您可能不想推送它。
相对于项目而言。 让你的提交消息尽可能清晰,但尝试写一个简洁的摘要行,然后留一个空行,然后输入有关你的提交的详细信息。 Git 是基于树的,而不是基于文件的,因此如果某个功能或错误修复涉及到一堆文件,请使用相同的消息立即将它们全部签入。
I create a new branch when I will be doing a number of related changes that I may decide not to keep, but would tear apart and break the master branch while I'm working on them. Especially if I want to make multiple checkins along the way.
For example, let's say that I have a simple graphical calculator application and I want to add parenthesis and order of operations, but I'm not sure whether it'll work the way I'm thinking about doing it. I'd create a new branch, checking in my changes as I go, and if I like the result at the end then I'll merge with Master.
This is especially good if I've got people asking for bugfixes to the released version (built off of master) while I'm working. I can easily switch to master, fix a bug, rebuild, re-release, and switch back to my development branch.
I would not suggest creating a new clone for each branch unless you don't want to check local changes in before switching. But even then you can use the "git stash" command to save your changes while you switch branches, do work, and switch back.
You should merge your local changes with the master branch when you are able to build successfully and you think you've implemented the feature you were aiming for. Don't break the master branch.
You create a remote branch by creating a local branch and pushing while you have that local branch checked out. You should only push a new branch to create it remotely if you want a backup of it or if other people may want to check it out. If you are just playing with a feature locally, you probably don't want to push it.
Relative to the project. Make your commit messages as clear as you can, but try to do write a concise summary line, then leave a blank line, then put details about your commit. Git is tree-based, rather than file-based, so if a feature or bug-fix touched a bunch of files, check them all in at once with the same message.
当不同的功能集并行开发时,它有助于拥有不同的分支
不会。廉价的本地分支是分支的众多优点之一。
完成分支中预期的功能后,将其合并到主分支。 这时,最好将其克隆到其他地方。 (github?)
It helps to have different branches when development of different feature set happens in parallel
No. Cheap local branching is one of the many advantages of branching.
After you complete the functionality intended in the branch, merge it to the master. At this time, preferably clone it else where. (github?)
由于我只有一个人,而且我更喜欢打开有限数量的 IDE 实例,因此我倾向于只保留存储库的一个克隆,并且只更改该克隆的内容。
这意味着每次更改结账内容时,我通常都必须运行某种干净的构建。
最常见的使用模式之一是使用“功能分支”,您可以在单独的分支中开发不同的功能。 然后,您可以拥有一个或多个集成分支(= 版本),并将这些功能合并到其中。
Since I am only one person and I prefer to keep a limited number of instances of my IDE open, I tend to keep only one clone of the repository, and just change the contents of that clone.
This means I usually have to run some kind of clean build every time I change the contents of my checkout.
One of the most common usage patterns is to use "feature branches" where you develop distinct features in separate branches. Then you can have one or more integration branches (= releases), that you merge these features into.