代码之家  ›  专栏  ›  技术社区  ›  Maciaz

Git,为什么我的主分支会受到影响?

git
  •  2
  • Maciaz  · 技术社区  · 7 年前

    我在一家总店工作。有了一个新想法,所以我选择了:

    git checkout -b new_branch_name
    

    对新分支进行了一些更改,然后切换回主分支。我在新分支上所做的所有更改都应用于master。我应该怎么做来防止这种情况?

    3 回复  |  直到 7 年前
        1
  •  3
  •   ashmaroli    7 年前

    如果不向新分支提交更改,则会缓存更改并通过父分支访问这些更改( master 在这种情况下)

    为了防止这种情况,您应该始终 git add [FILE] git commit 切换到父分支之前。 如果在提交之前切换,只需切换回新分支,添加更改的文件并提交

        2
  •  2
  •   Karol Dowbecki    7 年前

    因为你没有 add commit 这些新文件未被跟踪,不在您的git repo范围内。它们只存在于文件系统上。您可以使用 git clean -f -d 恢复回购的干净状态。

        3
  •  1
  •   LeGEC    7 年前

    如果您没有添加或提交任何内容:

    # switch to your new branch
    git checkout new_branch_name
    
    # confirm that your changes are still there :
    git status
    
    # commit on your new branch
    git add ...
    git commit ...
    
    # go back to master
    git checkout master