git 如何选择使用本地提交覆盖远程存储库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8386996/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:26:22  来源:igfitidea点击:

How can I choose to overwrite remote repository with local commits?

gitunfuddle

提问by ericgr

Basically, due to events beyond my control, my remote repo was moved - I did a lot of work on my local copy in the meantime and now I really just want to overwrite everything in the remote repo with my local files.

基本上,由于我无法控制的事件,我的远程存储库被移动了 - 同时我在本地副本上做了很多工作,现在我真的只想用我的本地文件覆盖远程存储库中的所有内容。

However, I don't seem to be able to do this. The closest I can get is to pull and merge, but then it wants to walk me through some convoluted process for merging. I don't want to merge. I want to overwrite. I don't need a new branch - basically, I just want a fresh start.

但是,我似乎无法做到这一点。我能得到的最接近的是拉取和合并,但它想引导我完成一些复杂的合并过程。我不想合并。我想覆盖。我不需要一个新的分支——基本上,我只是想要一个新的开始。

The remote repo is on unfuddle.

远程回购正在解开。

回答by Kjuly

You can remove the branch and recreate it, let's say the branch that you want to overwrite is dev:

您可以删除分支并重新创建它,假设您要覆盖的分支是dev

Remove the branch in your remote host(github)

删除远程主机(github)中的分支

git push origin :dev  

Then just push your dev again:

然后再次推送您的开发人员:

git push origin dev

I use Github for hosting, not familiar with unfuddle, but I think it'll works for the unfuddle, too. :)

我使用 Github 进行托管,不熟悉 unfuddle,但我认为它也适用于 unfuddle。:)



Just as @melee mentioned, you can also use

正如@melee 提到的,你也可以使用

git push origin dev -f

(not sure whether the -fis valid, but --forceis OK)

(不确定是否-f有效,但--force可以)

git push origin dev --force

to force overwrite the branch. I remember I did it before. Thanks @melee. :)

强制覆盖分支。我记得我以前做过。谢谢@melee。:)

回答by a20

How to do this for master branch, without pulling data down from the remote repo:

如何为 master 分支执行此操作,而无需从远程存储库中提取数据:

  1. Create a new folder, init git, add remote repo - don't pull or fetch!

    mkdir clean_repo

    git init

    git remote add origin <remote-repo>

  2. create (and switch to) empty local branch, add, commit and push a test file into this.

    git checkout test

    echo "test" > test

    git add .

    git commit -m "adding test"

    git push origin:test

  3. On github / bitbucket, change default branch to new branch

  4. On local, switch to master branch, commit and push to remote repo / branch

    git checkout -b master

    git push origin --mirror

  1. 创建一个新文件夹,init git,添加远程仓库 - 不要拉或取!

    mkdir clean_repo

    git init

    git remote add origin <remote-repo>

  2. 创建(并切换到)空的本地分支,添加、提交并将测试文件推送到此。

    git checkout test

    echo "test" > test

    git add .

    git commit -m "adding test"

    git push origin:test

  3. 在 github / bitbucket 上,将默认分支更改为新分支

  4. 在本地,切换到 master 分支,提交并推送到远程仓库/分支

    git checkout -b master

    git push origin --mirror

回答by Pooh Eamcharoenying

Been having the same problem. This command worked for me.

一直有同样的问题。这个命令对我有用。

git push --set-upstream origin master --force

回答by Simple-Solution

I'm not an expert in github ecosystem, but why can't you just reset your remote repository url?

我不是 github 生态系统的专家,但你为什么不能重置你的远程存储库 url?

git remote set-url origin /path/to/your/new/remote/repository/url

You might also need to configure your up-stream branch by looking in here.

您可能还需要通过查看此处来配置您的上游分支。

for more on git remote..., please take a look here.

有关更多信息git remote...,请查看此处