我希望我的分支是最新的(不是任何提交后面),不做任何新的提交,只留下我跟踪和修改的文件。 我想保留在这个文件中的变化。 我不小心跑了“git reset HEAD~”看它做了什么,现在我无法撤消“git reset HEAD~”。
简而言之,下面是事件的顺序和我尝试纠正它的失败。
<modified file> git reset --soft HEAD~ git stash git merge --ff-only git stash pop在“git reset --soft HEAD〜”命令后,我期待以下三个命令有效地将其反转。 我实际上回来的是...
错误:对以下文件的本地更改将被合并覆盖: README.md 请在合并之前提交更改或存储更改。 中止
任何帮助是极大的赞赏!
I want my branch to be up to date (not behind by any commits) without making any new commits and leaving my tracked and modified file alone. I want to keep the changes in this file. I accidentally ran "git reset HEAD~" to see what it did and now I can't undo "git reset HEAD~".
In short, below is the order of events and my failed attempt at correcting it.
<modified file> git reset --soft HEAD~ git stash git merge --ff-only git stash popAfter the "git reset --soft HEAD~" command, I expected the following three commands to effectively reverse it. What I actually got back was...
error: Your local changes to the following files would be overwritten by merge: README.md Please commit your changes or stash them before you can merge. Aborting
Any help is greatly appreciated!
最满意答案
通过放弃使用git stash ,你会变得更好,而只是总是检查临时分支上的更改。 事情变得更加清晰和强大,以及使得git stash无法做到的事情。
从您当前的状态运行
git checkout -b stash-branch-001 git commit -am stash # Now all your pending changes are saved, like git stash does, but properly saved # on a branch with all the benefits of a branch that stash cannot provide. git checkout master git fetch origin # Get latest and greatest from upstream git status # Always run git status and verify before reset --hard git reset --hard origin/master # At this point git status should say "Your branch is up-to-date with # 'origin/master'"然后返回保存的更改运行(在一般情况下):
git rebase master stash-branch-001 # Implicit changes the checked out branch... git checkout master # ... so change back git reset orign/master # At this point git status should say "modified: README.md"或者在这种特殊情况下,临时分支上只有一个提交,你可以只运行
git cherry-pick stash-branch-001 git reset HEAD^代替。 满意后只需删除临时分支。
我想,十年前我停止使用git stash 。 它提供零临时分支机构无法提供的好处。 也许有一些字符可以输入更少,但从概念上来说,只需要像往常一样办理入住和变换,而不是为了暂时保存更改而做一些不同的事情。
查看更改然后只是git log -p ... , git diff ... , gitk或任何常规git命令,而不是只有一个特殊情况的git stash show命令。
如果你正在进行多个临时更改,那么以任何顺序将它们输入和输出变得很简单,而不是像stash一样按顺序弹出它们。
在分支中检入更改提供了很大的灵活性,可以让您恢复任何分支(可能是多个分支)中的临时更改,而git stash pop对其需求非常有限: The working directory must match the index. 。
总而言之,帮自己一个忙,停止使用git stash 。
You will be much better off by abandoning ever using git stash and instead just always checking in changes on temporary branches. Things get much clearer and more robust as well as making things that git stash cannot do possible.
From your current state run
git checkout -b stash-branch-001 git commit -am stash # Now all your pending changes are saved, like git stash does, but properly saved # on a branch with all the benefits of a branch that stash cannot provide. git checkout master git fetch origin # Get latest and greatest from upstream git status # Always run git status and verify before reset --hard git reset --hard origin/master # At this point git status should say "Your branch is up-to-date with # 'origin/master'"Then to bring back the saved changes run (in the general case):
git rebase master stash-branch-001 # Implicit changes the checked out branch... git checkout master # ... so change back git reset orign/master # At this point git status should say "modified: README.md"or in this particular case where there is only one commit on the temporary branch you could run just
git cherry-pick stash-branch-001 git reset HEAD^instead. When satisfied just delete the temporary branch.
I stopped using git stash around ten years ago I think. It provides zero benefits that checking in on temporary branches does not provide. Maybe there is a few less characters to type, but it is conceptually much simpler to just always check in and rebase like you always do instead of doing something differently for saving changes temporarily.
Viewing the changes then is just git log -p ..., git diff ..., gitk or any of your regular git command instead of having one single, special case git stash show command.
If you have multiple temporary changes ongoing, bringing them in and out in any order becomes straight forward, instead of being forced to pop them in sequence like stash does.
Having the changes checked in on a branch provides great flexibility in allowing you to bring back the temporary changes in any branch (and possibly multiple) whereas git stash pop is very limited in its requirement: The working directory must match the index..
So all in all, do yourself a favour and stop using git stash.
更多推荐
发布评论