简体   繁体   English

Git 分支和本地更改

[英]Git branching & local changes

We cannot switch branches in Git without committing the changes or stashing them.如果不提交更改或存储它们,我们就无法在 Git 中切换分支。 What is the bigger picture behind this and why did Linus take this approach while designing Git?这背后的大局是什么?为什么 Linus 在设计 Git 时采用这种方法?

If switching branches cause local changes showing up across branches, What is the point in branches as they are expected work in isolation ?如果切换分支导致跨分支出现本地更改,那么分支的意义何在,因为它们被期望单独工作?

We cannot switch branches in Git without committing the changes or stashing them.如果不提交更改或存储它们,我们就无法在 Git 中切换分支。

Actually you can force switching branch, but then you lose any uncommitted changes on current branch.实际上你可以强制切换分支,但是你会丢失当前分支上所有未提交的更改。 It should be very unusual if you want to lose your current work just to switching branch.如果您想丢失当前的工作只是为了切换分支,这应该是非常不寻常的。 Why not commit it first to current branch, or at least stash the changes so you can come back?为什么不先将其提交到当前分支,或者至少将更改stash起来以便您可以回来?

Anyway to force switching branch, you can:无论如何要强制切换分支,您可以:

1) 1)

git checkout -f <new_branch>  // will lose any uncommitted changes on <old_branch>

or,或者,

2) 2)

git reset --hard <commit-hash-id-of-new-branch>  // will lose any uncommitted changes on <old_branch>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM