了解 git 个分支

Understanding git branches

新手git分支问题

如果我有一个像

这样的简单页面
    <!DOCTYPE html>
    <html>

        <head>  </head>

    <body>

      This is master

    </body>

    </html>

然后创建一个新分支并切换到那个分支

    git branch new-branch
    git checkout new-branch

然后在该分支中做一些事情,比如

    <!DOCTYPE html>
    <html>

        <head>  </head>

    <body>

      This is master

      This is in the new-branch

    </body>

    </html>

我认为这个新分支会与主分支分开,如果我切换回 master 它不会显示新分支中添加的内容

如果我结账大师

    git checkout master

仍然显示新分支中添加的内容。

谁能解释为什么会这样。

您只是忘记提交您在新分支中所做的更改。

git checkout new-branch

# Do your stuff in <edited_file>

git add <edited_file>
git commit -m "A short desc. of your changes"
git checkout master

注意:您应该首先将您的初始文件提交到 master 分支。完整版:

git init .

vim my_file          # Create some initial content in my_file

git add my_file
git commit -m "My first file"

git branch new-branch
git checkout new-branch

vim my_file          # Add some line to my_file

git add my_file
git commit -m "Some new lines"

git checkout master  # my_file in master does NOT include changes made the second time