git 用远程分支覆盖我的本地分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6229764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Overwriting my local branch with remote branch
提问by Sara Chipps
I have completely fubar'd my local branch, and would like to start over. The version on the server is correct.
我已经完全关闭了我当地的分支机构,并想重新开始。服务器上的版本是正确的。
I don't want to start over, I would like to use my local history to fix my huge screwup. (I can if I have to.)
我不想重新开始,我想用我当地的历史来解决我的大问题。(如果必须的话,我可以。)
git fetch branchname
, and git pull branchname
don't work. The message I get is "up to date" however, my local version does not match that of the server.
git fetch branchname
,并且git pull branchname
不工作。我收到的消息是“最新”但是,我的本地版本与服务器的版本不匹配。
git pull origin/branchname
gives me a "not found" error.
git pull origin/branchname
给我一个“未找到”错误。
回答by knittl
first, create a new branch in the current position (in case you need your old 'screwed up' history):
首先,在当前位置创建一个新分支(以防您需要旧的“搞砸”历史记录):
git branch fubar-pin
update your list of remote branches and sync new commits:
更新远程分支列表并同步新提交:
git fetch --all
then, reset your branch to the point where origin/branch points to:
然后,将您的分支重置为原点/分支指向的点:
git reset --hard origin/branch
be careful, this will remove any changes from your working tree!
小心,这将从您的工作树中删除任何更改!
回答by Casey Marshall
What I do when I mess up my local branch is I just rename my broken branch, and check out/branch the upstream branch again:
当我弄乱本地分支时,我所做的就是重命名损坏的分支,然后再次检出/分支上游分支:
git branch -m branch branch-old
git fetch remote
git checkout -b branch remote/branch
Then if you're sure you don't want anything from your old branch, remove it:
然后,如果您确定不需要旧分支中的任何内容,请将其删除:
git branch -D branch-old
But usually I leave the old branch around locally, just in case I had something in there.
但通常我会在当地留下旧的分支,以防万一我在那里有东西。
回答by Wes Hardaker
Your local branch likely has modifications to it you want to discard. To do this, you'll need to use git reset
to reset the branch head to the last spot that you diverged from the upstream repo's branch. Use git branch -v
to find the sha1 id of the upstream branch, and reset your branch it it using git reset SHA1ID
. Then you should be able to do a git checkout
to discard the changes it left in your directory.
您的本地分支可能已经修改了您想要丢弃的内容。为此,您需要使用git reset
将分支头重置为与上游存储库分支分开的最后一个位置。使用git branch -v
发现上游分支的SHA1 ID,并使用重置您的分支它它git reset SHA1ID
。然后你应该能够做一个git checkout
丢弃它在你的目录中留下的更改。
Note: always do this on a backed-up repo. That way you can assure you're self it worked right. Or if it didn't, you have a backup to revert to.
注意:始终在备份存储库上执行此操作。这样你就可以保证你是自己它工作正常。或者,如果没有,您有一个备份可以恢复。
回答by Karthikeyan Varadarajan
git reset --hard
This is to revert all your local changes to the origin head
这是将所有本地更改还原到原始头