GIT:分支期间会发生什么?
我的问题真的很无辜。我一直在阅读 git,并且已经了解了诸如推拉和创建分支等基础知识。我的导师告诉我,使用 git 的正确方法是提取资源、创建一个新的本地分支、传输和编辑该本地分支,完成后返回 master 分支并将其与本地分支合并。现在我的问题是,幕后究竟发生了什么?
我的意思是,当 git 创建本地分支时,它是否会创建您可以编辑的主分支的副本?如果存在,这些副本位于哪里?
如果万一我在本地分支上搞砸了,我该如何恢复更改(例如,我想回到拉动主分支时的状态)?这只是返回 master 并删除本地分支的简单问题吗?
我想人们之所以称之为版本控制是因为分支,我认为……代表版本。我说得对吗?
master为什么叫分支?不应该是行李箱吗? 好吧,这只是一个非常愚蠢的问题,忽略#4...哈哈哈!
如果能回答我天真的孩子气的问题,我将不胜感激。 <3 <3 <3
My question is really innocent. I've been reading up on git, and I already know the basics like pushing and pulling and creating branches and so on. My mentor told me that the proper way to work with git is to pull the resources, create a new local branch, transfer and edit that local branch and once you're done, go back to master branch and merge it with your local branch. Now my question is, what exactly happens behind the scene?
I mean, when git creates a local branch, does it create a copy of the master branch that you can edit? If it does, where are these copies located?
If by any chance I mess up on my local branch, how do I revert back the changes(let's say for example, I want to go back to the way it was when I pulled the master)? Is it just a simple matter of going back to master and deleting the local branch?
I suppose that the reason why people call it version control is because of the branches, which I think...Represents the version. Am I correct?
Why is the master called a branch? Shouldn't it be a trunk? okay that just a very dumb question, ignore #4... hahaha!
Answers to my innocent childlike questions shall be appreciated. <3 <3 <3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的意思是,当 git 创建本地分支时,它是否会创建您可以编辑的主分支的副本?如果是,这些副本位于哪里?
Branch 不是副本。 Git 中的分支只是指向最新提交的“指针”。提交对象指向其父级,这就是您获取整个“分支”的方式。
因此,一旦您从 master 分支,您就会获得另一个指向同一提交的指针。不多不少,不多不少。现在master和branch都指向同一个提交。当您现在继续在分支中提交时,分支指针会不断移动以指向其特定于分支的提交。
如果万一我在本地分支上搞砸了,我该如何恢复更改(例如,我想回到拉动主分支时的状态)?难道只是回到master删除本地分支这么简单吗
?便宜,正如我所说,它只是一个指向提交的指针,这意味着它是一个包含
.git\refs\heads\branchname
中 41 个字符 SHA-1 校验和的文件如果你这样做不想删除分支,重置为以前的提交使用类似 git reset --hard HEAD~1
我想人们之所以称之为版本控制是因为分支,我认为……代表版本。我对吗?
我们称它们为版本控制,因为每次修订或提交都是更改的单位。分支是自特定提交或分支点以来的更改集合。\
PS:我当然是在解释 ProGit,所以既然你评论说你已经阅读了它,我不确定我的答案会有帮助。
I mean, when git creates a local branch, does it create a copy of the master branch that you can edit? If it does, where are these copies located?
Branch is not a copy. A branch in Git is just a "pointer" to the latest commit. The commit object point to their parent(s) and that is how you get the entire "branch".
So as soon as you branch from master, you get one more pointer to the same commit. Nothing less, nothing more. Now both master and branch point to the same commit. As you keep committing in the branch now, the branch pointer keeps moving to point to it's branch specific commits.
If by any chance I mess up on my local branch, how do I revert back the changes(let's say for example, I want to go back to the way it was when I pulled the master)? Is it just a simple matter of going back to master and deleting the local branch?
If you don't like a branch, just delete it -
git branch -d hotfix
Branches in Git are cheap, as, like I said, it is just a pointer to a commit which means it is a file which contains the 41 character SHA-1 checksum in.git\refs\heads\branchname
If you do not want to delete the branch, reset to a previous commit using something like
git reset --hard HEAD~1
I suppose that the reason why people call it version control is because of the branches, which I think...Represents the version. Am I correct?
We call them version control because of each revisions or commits which is the unit of change. Branch is a collection of changes since a particular commit or branching point.\
PS: I am of course paraphrasing ProGit, so since you had commented that you had read it, I am not sure my answer will be of help.
我强烈建议阅读 Pro Git 书籍。
I strongly recommend reading the Pro Git Book.