如何在 Git 中完全用远程分支替换本地分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9210446/
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
How to replace local branch with remote branch entirely in Git?
提问by YemSalat
I have two branches:
我有两个分支:
- local branch (the one which I work with)
- remote branch (public, only well-tested commits go there)
- 当地分支机构(与我合作的分支机构)
- 远程分支(公共的,只有经过良好测试的提交才会去那里)
Recently I seriously messed up my local branch.
最近我严重搞砸了我当地的分支机构。
How would I replace the local branch entirely with the remote one, so I can continue my work from where the remote branch is now?
我如何将本地分支完全替换为远程分支,以便我可以从远程分支现在所在的位置继续我的工作?
I have already searched SO and checking out to the remote branch locally does not have any effect.
我已经搜索过 SO 并且在本地签出到远程分支没有任何影响。
回答by araqnid
- Make sure you've checked out the branch you're replacing (from Zoltán's comment).
Assuming that master is the local branch you're replacing, and that "origin/master" is the remote branch you want to reset to:
git reset --hard origin/master
- 确保您已检查要替换的分支(来自 Zoltán 的评论)。
假设 master 是您要替换的本地分支,而“origin/master”是您要重置为的远程分支:
git reset --hard origin/master
This updates your local HEAD branch to be the same revision as origin/master, and --hard
will sync this change into the index and workspace as well.
这--hard
会将您的本地 HEAD 分支更新为与 origin/master 相同的修订版,并将此更改同步到索引和工作区中。
回答by adamsmith
That's as easy as three steps:
这就像三个步骤一样简单:
- Delete your local branch:
git branch -d local_branch
- Fetch the latest remote branch:
git fetch origin remote_branch
- Rebuild the local branch based on the remote one:
git checkout -b local_branch origin/remote_branch
- 删除本地分支:
git branch -d local_branch
- 获取最新的远程分支:
git fetch origin remote_branch
- 在远程分支的基础上重建本地分支:
git checkout -b local_branch origin/remote_branch
回答by Sailesh
git branch -D <branch-name>
git fetch <remote> <branch-name>
git checkout -b <branch-name> --track <remote>/<branch-name>
回答by bit-less
Replace everything with the remote branch; but, only from the same commit your local branch is on:
用远程分支替换所有内容;但是,仅来自您本地分支所在的同一个提交:
git reset --hard origin/some-branch
OR, get the latestfrom the remote branch and replace everything:
或者,从远程分支获取最新信息并替换所有内容:
git fetch origin some-branch
git reset --hard FETCH_HEAD
As an aside, if needed, you can wipe out untracked files & directories that you haven't committed yet:
顺便说一句,如果需要,您可以清除尚未提交的未跟踪文件和目录:
git clean -fd
回答by Joshua S
The safest and most complete way to replace the current local branch with the remote:
用远程替换当前本地分支的最安全和最完整的方法:
git stash
git merge --abort
git rebase --abort
git branch -M yourBranch replaced_yourBranch
git fetch origin yourBranch:yourBranch
git checkout yourBranch
The stash
line saves the changes that you have not committed. The branch
line moves your branch to a different name, freeing up the original name. The fetch
line retrieves the latest copy of the remote. The checkout
line recreates the original branch as a tracking branch.
该stash
行保存您尚未提交的更改。该branch
行将您的分支移动到不同的名称,从而释放原始名称。该fetch
行检索遥控器的最新副本。该checkout
行将原始分支重新创建为跟踪分支。
Or as a bash function:
或者作为 bash 函数:
replaceWithRemote() {
yourBranch=${1:-`git rev-parse --abbrev-ref HEAD`}
git stash
git merge --abort
git rebase --abort
git branch -M ${yourBranch} replaced_${yourBranch}_`git rev-parse --short HEAD`
git fetch origin ${yourBranch}:${yourBranch}
git checkout ${yourBranch}
}
which renames the current branch to something like replaced_master_98d258f.
它将当前分支重命名为replaced_master_98d258f 之类的名称。
回答by Amit Kaneria
It can be done multiple ways, continuing to edit this answer for spreading better knowledge perspective.
可以通过多种方式完成,继续编辑此答案以传播更好的知识视角。
1) Reset hard
1) 硬重置
If you are working from remote develop branch, you can reset HEAD to the last commit on remote branch as below:
如果您从远程开发分支工作,您可以将 HEAD 重置为远程分支上的最后一次提交,如下所示:
git reset --hard origin/develop
2) Delete current branch, and checkout again from the remote repository
2) 删除当前分支,再从远程仓库checkout
Considering, you are working on develop branch in local repo, that syncs with remote/develop branch, you can do as below:
考虑到您正在本地仓库中开发分支,与远程/开发分支同步,您可以执行以下操作:
git branch -D develop
git checkout -b develop origin/develop
3) Abort Merge
3) 中止合并
If you are in-between a bad merge (mistakenly done with wrong branch), and wanted to avoid the merge to go back to the branch latest as below:
如果您处于错误合并之间(错误地使用错误的分支完成),并且希望避免合并返回最新的分支,如下所示:
git merge --abort
4) Abort Rebase
4) 中止变基
If you are in-between a bad rebase, you can abort the rebase request as below:
如果您处于错误的 rebase 之间,您可以中止 rebase 请求,如下所示:
git rebase --abort
回答by TTT
I'm kind of surprised no one mentioned this yet; I use it nearly every day:
我有点惊讶还没有人提到这一点;我几乎每天都使用它:
git reset --hard @{u}
Basically, @{u}
is just shorthand for origin/[my-current-branch-name]
. It's nice because it's branch agnostic.
基本上,@{u}
只是origin/[my-current-branch-name]
. 这很好,因为它与分支无关。
Make sure to git fetch
first to get the latest copy of the remote branch.
确保git fetch
首先获取远程分支的最新副本。
回答by ksol
You can do as @Hugo of @Laurent said, or you can use git rebase
to delete the commits you want to get rid off, if you know which ones. I tend to use git rebase -i head~N
(where N is a number, allowing you to manipulate the last N commits) for this kind of operations.
你可以按照@Laurent 的@Hugo 说的那样做,或者你可以使用git rebase
删除你想要摆脱的提交,如果你知道哪些。我倾向于使用git rebase -i head~N
(其中 N 是一个数字,允许您操纵最后 N 次提交)进行此类操作。
回答by Tom Stickel
The selected answer is absolutely correct, however it did not leave me with the latest commit/pushes ...
将选择的答案是绝对正确的,但它并没有离开我了最新提交/推...
So for me:
所以对我来说:
git reset --hard dev/jobmanager-tools
git pull ( did not work as git was not sure what branch i wanted)
Since I know I want to temporarily set my upstream branch for a few weeks to a specific branch ( same as the one i switched to / checked out earlier and did a hard reset on )
因为我知道我想暂时将上游分支设置为特定分支几周(与我之前切换到/签出并对其进行硬重置的分支相同)
So AFTER reset
所以重置后
git branch --set-upstream-to=origin/dev/jobmanager-tools
git pull
git status ( says--> on branch dev/jobmanager-tools
回答by kqr
If you want to update branch that is not currently checked out you can do:
如果要更新当前未检出的分支,可以执行以下操作:
git fetch -f origin rbranch:lbranch