如何在Git中完全用远程分支替换本地分支?

本文翻译自:How to replace local branch with remote branch entirely in Git?

I have two branches: 我有两个分支:

  1. local branch (the one which I work with) 本地分公司(与我合作的分公司)
  2. 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并且在本地检查远程分支没有任何影响。


#1楼

参考:https://stackoom.com/question/ce3a/如何在Git中完全用远程分支替换本地分支


#2楼

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 

#3楼

如果要更新当前未签出的分支,则可以执行此操作

git fetch -f origin rbranch:lbranch

#4楼

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. stash行保存您尚未提交的更改。 The branch line moves your branch to a different name, freeing up the original name. branch线将您的分支移动到另一个名称,释放原始名称。 The fetch line retrieves the latest copy of the remote. fetch行检索远程的最新副本。 The checkout line recreates the original branch as a tracking branch. 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. 它将当前分支重命名为replacement_master_98d258f。


#5楼

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 latest from 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

#6楼

As provided in chosen explanation, git reset is good. 如所选解释中所提供的, git reset是好的。 But nowadays we often use sub-modules: repositories inside repositories. 但是现在我们经常使用子模块:存储库中的存储库。 For example, you if you use ZF3 and jQuery in your project you most probably want them to be cloned from their original repositories. 例如,如果您在项目中使用ZF3和jQuery,则最有可能希望从原始存储库中克隆它们。 In such case git reset is not enough. 在这种情况下, git reset是不够的。 We need to update submodules to that exact version that are defined in our repository: 我们需要将子模块更新为我们的存储库中定义的确切版本:

git checkout master
git fetch origin master
git reset --hard origin/master
git pull

git submodule foreach git submodule update

git status

it is the same as you will come (cd) recursively to the working directory of each sub-module and will run: 它是相同的,你将(cd)递归到每个子模块的工作目录,并将运行:

git submodule update

And it's very different from 它与...非常不同

git checkout master
git pull

because sub-modules point not to branch but to the commit. 因为子模块不是指向分支而是指向提交。

In that cases when you manually checkout some branch for 1 or more submodules you can run 在这种情况下,当您手动签出一个或多个子模块的某个分支时,您可以运行

git submodule foreach git pull
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值