2

我在 git 中使用 2 个本地分支,我们称它们为 alerts_v2 和 . 我对 A 进行了一些更改,提交它们,然后切换到 B。我在 B 中做了一些工作,现在切换回分支 A 后,我将远程分支的文件更改与本地更改混合在一起。因此,当我执行 git status 时,我列出了一堆我没有更改的文件。我的目标是让我的分支在提交的更改方面与远程主机匹配,然后只有在我执行“git status”时才显示我的本地更改。

更新:我已经粘贴了 reflog 的副本。分支 alerts_v2 对应于上例中的分支 A。在切换回 alerts_v2 之前,我实际上使用了几个分支,因此分支 B 实际上代表了几个分支(但我认为这并不重要)。

条目 23a03b7 是我离开 alerts_v2 的地方,而条目 f6c143e 是我搬回来的地方。

31b259f HEAD@{1}: origin/master: updating HEAD
74aab0b HEAD@{2}: commit: Blap
31b259f HEAD@{3}: origin/master: updating HEAD
f6c143e HEAD@{4}: checkout: moving from version to alerts_v2
31b259f HEAD@{5}: commit: Added version information popup
b2c9ce3 HEAD@{6}: origin/master: updating HEAD
f0c9b0c HEAD@{7}: commit: Blap
b2c9ce3 HEAD@{8}: origin/master: updating HEAD
abba6bc HEAD@{9}: commit: Blap
b2c9ce3 HEAD@{10}: origin/master: updating HEAD
34ecadc HEAD@{11}: commit: Blap
f3cb5f9 HEAD@{12}: commit: Blap
b2c9ce3 HEAD@{13}: origin/master: updating HEAD
3a8c25d HEAD@{14}: commit: Blap
b2c9ce3 HEAD@{15}: origin/master: updating HEAD
6820bd3 HEAD@{16}: pull : Merge made by recursive.
ad215b1 HEAD@{17}: commit: Blap
27bd651 HEAD@{18}: commit: Blap
e74af44 HEAD@{19}: checkout: moving from e74af44ae40484d837e0295c1622731de1dcb84e to version
e74af44 HEAD@{20}: pull : Fast-forward
76bb1fa HEAD@{21}: checkout: moving from ncli to master2
e74af44 HEAD@{22}: commit: Added download NCLI button
ed2d3c4 HEAD@{23}: origin/master: updating HEAD
9ad842a HEAD@{24}: pull : Merge made by recursive.
b99d663 HEAD@{25}: commit: Added download ncli button
e83d923 HEAD@{26}: origin/master: updating HEAD
719791a HEAD@{27}: commit: Added download NCLI button
9e613d1 HEAD@{28}: commit: Added download ncli button
e83d923 HEAD@{29}: origin/master: updating HEAD
a4d6d42 HEAD@{30}: pull : Merge made by recursive.
39f5fed HEAD@{31}: commit: Add NCLI download capability
76bb1fa HEAD@{32}: checkout: moving from 76bb1fa890089ca732322eba7ad9e74486e2bda1 to ncli
76bb1fa HEAD@{33}: checkout: moving from master to master2
9248d50 HEAD@{34}: commit: Blap
76bb1fa HEAD@{35}: origin/master: updating HEAD
23a03b7 HEAD@{36}: checkout: moving from alerts_v2 to master
f6c143e HEAD@{37}: commit: Blap
8a4b166 HEAD@{38}: commit: Blap
76bb1fa HEAD@{39}: pull : Fast-forward
d245574 HEAD@{40}: pull : Fast-forward
c1e5e92 HEAD@{41}: pull : Fast-forward
b8602a5 HEAD@{42}: commit: Changed Alert event handling
db06232 HEAD@{43}: origin/master: updating HEAD
e807ef7 HEAD@{44}: rebase: Commit
db06232 HEAD@{45}: checkout: moving from alerts_v2 to db06232a413d79fd5607c5618dce8a316efbe84
8cc089f HEAD@{46}: pull : Merge made by recursive.
c74acd9 HEAD@{47}: commit: Commit
4a0c135 HEAD@{48}: commit: Changed the name attribute in the AlertModel to map to Alert Type
ed087cd HEAD@{49}: origin/master: updating HEAD
3db4639 HEAD@{50}: commit: Changed the name attribute in the AlertModel to map to Alert Type
ed087cd HEAD@{51}: origin/master: updating HEAD
ecaea02 HEAD@{52}: commit: Changed the name attribute in the AlertModel to map to Alert Type
ed087cd HEAD@{53}: origin/master: updating HEAD
36323fa HEAD@{54}: pull : Merge made by recursive.
18034e5 HEAD@{55}: commit: Changed AlertMode name attribute to map to Alert Type
a46de98 HEAD@{56}: origin/master: updating HEAD
6fae22b HEAD@{57}: checkout: moving from alerts_patch1 to alerts_v2
8efde4b HEAD@{58}: checkout: moving from alerts_v2 to alerts_patch1
6fae22b HEAD@{59}: commit: Made some changes
a46de98 HEAD@{60}: checkout: moving from a46de98c050cfc8b61b5efccb20d0a271b3959d2 to alerts_v
a46de98 HEAD@{61}: checkout: moving from a46de98c050cfc8b61b5efccb20d0a271b3959d2 to events1
a46de98 HEAD@{62}: commit: Initial implementation of Alert Manager, Namespace Manager and Pol
5cfb8a2 HEAD@{63}: origin/master: updating HEAD
f978dae HEAD@{64}: commit: Initial implementation of Alert Manager, Namespace Manager and Pol
5cfb8a2 HEAD@{65}: origin/master: updating HEAD
4

1 回答 1

0

当您有本地更改时签出(或切换)到不同的分支将成功,只要这些更改不会在新分支上产生冲突。这些可能是您看到的已更改文件,您在分支 B 上的待处理工作。

您可以通过硬重置删除对跟踪文件的所有更改:

git reset --hard HEAD

但这会删除你的更改,如果你想保存它们,你可以隐藏它们:

git stash

Stashing 将创建您的更改记录,并将任何跟踪的文件恢复到HEAD. 您可以看到您拥有的 stash 列表git stash list

当您移回正确的分支时,您可以检索您保存的存储:

git stash pop

还有一些存储文档以防万一

于 2012-12-29T02:40:50.527 回答