git 用github中的另一个完全替换远程分支,不留旧的痕迹

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

Completely replace remote branch with another in github leaving no traces of the old one

gitsshpush

提问by Mr_and_Mrs_D

I pushed a branch to an empty github repo :

我将一个分支推送到一个空的 github 仓库:

MrD@MRSD /c/Dropbox/eclipse_workspaces/javaEE/ted2012 (GitHubSquash)
$ git remote add github  https://github.com/Utumno/ted2012.git
MrD@MRSD /c/Dropbox/eclipse_workspaces/javaEE/ted2012 (GitHubSquash)
$ git push -u github GitHubSquash
Username for 'https://github.com': Utumno
Password for 'https://[email protected]':
//...
To https://github.com/Utumno/ted2012.git
 * [new branch]      GitHubSquash -> GitHubSquash
Branch GitHubSquash set up to track remote branch GitHubSquash from github.

Then I noticed I had pushed some fluff along and tried to delete the branch/replace it with another etc. I failed :

然后我注意到我推了一些绒毛并试图删除分支/用另一个等替换它。我失败了:

MrD@MRSD /c/Dropbox/eclipse_workspaces/javaEE/ted2012 (GitHub2)
$ git push  :github && git push github GitHub2
ssh: connect to host  port 22: Bad file number
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
MrD@MRSD /c/Dropbox/eclipse_workspaces/javaEE/ted2012 (GitHub2)
$ git checkout GitHubSquash
Switched to branch 'GitHubSquash'
Your branch is ahead of 'github/GitHubSquash' by 1 commit.
  (use "git push" to publish your local commits)
MrD@MRSD /c/Dropbox/eclipse_workspaces/javaEE/ted2012 (GitHubSquash)
$ git push  :github
ssh: connect to host  port 22: Bad file number
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
MrD@MRSD /c/Dropbox/eclipse_workspaces/javaEE/ted2012 (GitHubSquash)
$ git push  -u :github
ssh: connect to host  port 22: Bad file number
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

I had to delete the repo and push my new branch anew. That worked but left me wondering :

我不得不删除 repo 并重新推送我的新分支。那行得通,但让我想知道:

  1. What should I have done to completely replace the remote branch with another ?

  2. Why on earth I was getting the ssh: connect to host port 22: Bad file numbererrors - while my first push succeeded (and notice I am on http) ?

    $ git --version
    git version 1.8.1.msysgit.1
    
  1. 我应该怎么做才能用另一个完全替换远程分支?

  2. 为什么我得到了 ssh: connect to host port 22: Bad file numbererrors - 虽然我的第一次推送成功了(注意我在 http 上)?

    $ git --version
    git version 1.8.1.msysgit.1
    

回答by Chronial

Your syntax is wrong. The correct command is:

你的语法是错误的。正确的命令是:

git push -f github GitHubSquash

This will replace the remote GitHubSquash branch with your local version. If you just want to delete the remote branch:

这将用您的本地版本替换远程 GitHubSquash 分支。如果只想删除远程分支:

git push -f github :GitHubSquash

I guess you got your error, because git is trying to interpret :githubas an url and weird stuff happens :).

我猜你有错误,因为 git 试图解释:github为一个 url 并且发生了奇怪的事情:)。

回答by mixo

git push origin :GitHubSquash

to delete remote branch

删除远程分支

git push --set-upstream origin GitHubSquash

to push local branch

推本地分支