开发者

Why is my new Git branch not empty?

Suppose we start with a brand new setup.

touch somefile
git init .
git add .
g开发者_JAVA技巧it 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.


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.


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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜