【问题标题】:why git stash cannot abandon the changes made just now?为什么 git stash 不能放弃刚才所做的更改?
【发布时间】:2012-08-21 19:30:49
【问题描述】:

我从github fork了一个项目,origin指向我自己的github repo,remote指向它的原始repo,因为我想从远程获取更新,

我使用 git pull remote branch_name,然后我的本地 repo 处于冲突模式, 现在我想取消 git pull 的效果,所以我使用了 git stash,但惊讶地发现我没有这样做?怎么了?

详细信息如下:

[mirror@home weechat]$ git status
# On branch master
# Unmerged paths:
#   (use "git add/rm <file>..." as appropriate to mark resolution)
#
#       both modified:      CMakeLists.txt
#       both modified:      ChangeLog
#       both modified:      NEWS
#       both modified:      configure.in
#       both modified:      po/cs.po
#       both modified:      po/de.po
#       both modified:      po/es.po
#       both modified:      po/fr.po
#       both modified:      po/hu.po
#       both modified:      po/ru.po
#       both modified:      po/weechat.pot
#       deleted by us:      src/irc/irc-server.c
#       both modified:      weechat.spec
#
no changes added to commit (use "git add" and/or "git commit -a")
[mirror@home weechat]$ git stash
CMakeLists.txt: needs merge
ChangeLog: needs merge
NEWS: needs merge
configure.in: needs merge
po/cs.po: needs merge
po/de.po: needs merge
po/es.po: needs merge
po/fr.po: needs merge
po/hu.po: needs merge
po/ru.po: needs merge
po/weechat.pot: needs merge
src/irc/irc-server.c: needs merge
weechat.spec: needs merge
CMakeLists.txt: needs merge
ChangeLog: needs merge
NEWS: needs merge
configure.in: needs merge
po/cs.po: needs merge
po/de.po: needs merge
po/es.po: needs merge
po/fr.po: needs merge
po/hu.po: needs merge
po/ru.po: needs merge
po/weechat.pot: needs merge
src/irc/irc-server.c: needs merge
weechat.spec: needs merge
CMakeLists.txt: unmerged (118d776a202cfc6688290e96fca20ac4f8631f5e)
CMakeLists.txt: unmerged (d05c9a9711a965dcad0aefa03b79ae7a4f5cb8fb)
CMakeLists.txt: unmerged (dd2b14cb188e1927e098705d77e922ad530e232b)
ChangeLog: unmerged (764711936b54aa75812828e1ab15e1632aa3eb9d)
ChangeLog: unmerged (e00d4bf654c3d199e24bf4910b066f1659fea4b6)
ChangeLog: unmerged (dbec2315ffc1f15feede248abe3ed85beaa82ed7)
NEWS: unmerged (457d5443548e6130b50e42a9c87a6f2e8ba4c596)
NEWS: unmerged (706cee4b8853013f20129ccb2fa9057b7500fceb)
NEWS: unmerged (33afffbac62399faa32e2560a9accd95dd9a050e)
configure.in: unmerged (8e1058e5e00a5e671459cee8300420b0488560d9)
...
fatal: git-write-tree: error building trees
Cannot save the current index state

那么如何取消 git pull 的效果呢?我应该删除repo,然后重新下载吗?

【问题讨论】:

    标签: git github git-pull merge-conflict-resolution git-stash


    【解决方案1】:

    运行git merge --abort

    您正处于合并过程中,因此绝对不应该进行存储(您会丢失与合并相关的历史信息)。

    【讨论】:

    • 为什么 git stash 会丢失历史?它会失去什么样的历史?我认为只有在提交所有更改时才会发生拉动:)
    • 运行git reset; git stash save 将允许您保存工作目录的状态,但它会丢失所有冲突信息,因为它会重置索引。
    【解决方案2】:

    git stash 是保存更改以备将来使用,而不是取消冲突状态,这就是它不起作用的原因。

    改为使用git reset ORIG_HEAD(参见How to undo a git pull?

    【讨论】:

    • stash 是用来放弃修改的,为什么要用来保存修改?
    • 不,stash 用于保存对工作副本所做的修改并丢弃它们,以便以后使用git stash pop 恢复它们。放弃修改由git reset 完成
    • 在什么情况下我应该使用 git stash?如果我在这里使用 git reset,合并历史将会丢失,但是如果我使用 git stash,它就不起作用,为什么?因为我无法保存一些有冲突的代码,对吧?
    猜你喜欢
    • 2021-10-26
    • 1970-01-01
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    • 2017-11-24
    相关资源
    最近更新 更多