为什么我的新 Git 分支不为空?

发布于 11-14 03:00 字数 409 浏览 3 评论 0原文

假设我们从一个全新的设置开始。

touch somefile
git init .
git add .
git commit -m "initial commit"
git branch dev
git checkout dev

在执行git status之后,我得到了

nothing to commit (working directory clean)

在执行git merge master之后,我得到了

Already up-to-date.

为什么是这样?我认为新分支开始时是干净的(并且是空的),直到我们直接添加内容或从现有分支合并。

Suppose we start with a brand new setup.

touch somefile
git init .
git add .
git commit -m "initial commit"
git branch dev
git checkout dev

After doing git status, I get

nothing to commit (working directory clean)

After doing git merge master, I get

Already up-to-date.

Why is that? I thought new branch starts clean (and empty) until we either add content directly or merge from existing branch.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

走走停停2024-11-21 03:00:27

Git 中的分支只不过是指向提交的指针。当您像以前那样创建分支时,它指向与其他分支相同的提交。一旦你提交了一些东西,你就会发现它们有所不同。大多数其他版本控制系统不适用于此类基于快照的方法。

因此,当您发出合并命令时,它没有任何可合并的内容,因为您当前的分支已经包含其他分支的所有提交。

我强烈建议阅读 progit.org/book 并充分理解历史结构遵循的 DAG(有向无环图)。

希望这有帮助。

Branches in Git are nothing more than pointers to commits. When you create a branch as you did, it is pointing to the same commit as the other branch. Once you commit something after that is when you will see them differ. Most other version control systems don't work on snapshot-based approaches like this.

Thus, when you issue the merge command, it has nothing to merge as your current branch already contains all the commits of the other branch.

I would highly recommend reading progit.org/book and fully understanding the DAG (directed acyclic graph) that the history structure follows.

Hope this helps.

橘和柠2024-11-21 03:00:27

新分支 dev 将使用 master 分支的状态创建,因此在您提交之前没有任何内容可以合并到 dev 分支中更改为 master (在最初创建 dev 分支之后)。

The new branch dev will be created with the state of the master branch, so there's nothing to merge into the dev branch until you've commited changes to master (after the dev branch has been created initially).

贪恋2024-11-21 03:00:27

git 中的分支是指针,但从某种意义上说,如果您希望它们使用孤儿分支,它们可以为空。创建孤立分支后,检查状态,您将看到该树全部被视为新树并等待添加。删除它,你就得到了你想要的干净分支。

mkdir foo
cd foo
touch somefile
git init .
git add .
git commit -m "initial commit"
git checkout --orphan dev
rm -rf .

这仅适用于 git 1.7.2 或更高版本。

Branches in git are pointers but they can, in a manner of speaking, be empty if you want them to be using orphans. Once you've created an orphan branch check the status and you'll see the tree is all considered new and waiting to be added. Delete it and you have the clean branch you're after.

mkdir foo
cd foo
touch somefile
git init .
git add .
git commit -m "initial commit"
git checkout --orphan dev
rm -rf .

This is only possible with git 1.7.2 or higher.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文