Git:推送到不同的远程分支

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

Git : Push to different remote branch

gitgithub

提问by RJR

Problem:

问题:

Need to push the changes from local git branch to a different remote git repository branch and this changes pushed to the branch will be compared with the master existing in the remote URL and changes will get merged.

需要将更改从本地 git 分支推送到不同的远程 git 存储库分支,推送到该分支的更改将与远程 URL 中存在的 master 进行比较,并且更改将被合并。

Steps

脚步

I have followed so far create a local git repository,

到目前为止,我一直在创建一个本地 git 存储库,

Initialized a simple local git repo using below using commands like below,

使用下面的命令初始化一个简单的本地 git repo,

 git init

Added existing files to the repo and get them added to the staging area using the below command,

将现有文件添加到 repo 并使用以下命令将它们添加到暂存区,

MacBook-Pro: $ git add *.h
MacBook-Pro: $ git add *.m

Checked the status using below command,

使用以下命令检查状态,

MacBook-Pro: $ git status

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   test.h
#   new file:   test.m
#

Committed them,

承诺他们,

git commit -m"Added test base files"

Now created a new branch named issue_fix,

现在创建了一个名为 issue_fix 的新分支,

   MacBook-Pro:$ git branch issue_fix

Started working on the branch by checking out the branch.

通过检查分支开始在分支上工作。

MacBook-Pro: $ git checkout issue_fix

Made few commits to the branch. Everything was fine up to this.

很少提交到分支。一切都很好。

Now I'm in a situation where, I need to push the changes I made to my 'issue_fix' branch to a remote repository URL like this

现在我处于一种情况,我需要将我对“issue_fix”分支所做的更改推送到这样的远程存储库 URL

    https://github.com/myaccountname/project.git

My Changes will get pushed to the branch name given to me or if the branch was not available I need to create a remote branch and push my changes in the local branch to that one.

我的更改将被推送到给定的分支名称,或者如果该分支不可用,我需要创建一个远程分支并将我在本地分支中的更改推送到该分支。

Most importantly pushed changes will be compared with the master in the given repository URL and if everything is fine it will get merged with the master. So I will be always pushing my changes from local to remote branch only.

最重要的是,推送的更改将与给定存储库 URL 中的 master 进行比较,如果一切正常,它将与 master 合并。所以我将始终只将我的更改从本地分支推送到远程分支。

Problem occurred because Clone URL was not given when I started on this, only the source was provided so I created a local git repository and started working on that now the repository URL has been provided and asked to push my changes to a branch.

出现问题是因为在我开始时没有给出克隆 URL,只提供了源,所以我创建了一个本地 git 存储库并开始处理,现在已经提供了存储库 URL 并要求将我的更改推送到分支。

I would like to know, is this possible in the first case?.If possible, give me the commands I need to give to get it working.

我想知道,在第一种情况下这可能吗?。如果可能,请给我命令以使其正常工作。

回答by CharlesB

Git is distributed and works locally for most of its operations; it doesn't matter whether the remote exists when you make your branches or commits locally. It just has to exist when you make the push.

Git 是分布式的,它的大部分操作都在本地工作;当您在本地创建分支或提交时,远程是否存在并不重要。当您推动时,它必须存在。

So you can add the remote repository, and then push your branch to it. The branch will get created remotely if it doesn't exist.

所以你可以添加远程存储库,然后将你的分支推送到它。如果该分支不存在,则会远程创建该分支。

git remote add github-repo https://github.com/myaccountname/project.git
git push github-repo issue_fix

回答by Nils Werner

To fix this debacle (I've had these problems before, too. They're dead annoying) you could try do the following:

要解决此故障(我以前也遇到过这些问题。它们很烦人)您可以尝试执行以下操作:

Your situation

你的情况

The main repository

主存储库

A-B-C-D-E-F-G-H

Your repository

您的存储库

            A'-B'-C'-D'

Where the code in Gis equal to the code in your local A', but they do not share the same history

其中的代码与G您本地的代码相同A',但它们不共享相同的历史记录

Clone the repository you want to push changes to

克隆要推送更改的存储库

git clone https://....

This will provide you with a working copy of the codebase and, more importantly, also with its history.

这将为您提供代码库的工作副本,更重要的是,还会提供其历史记录。

The clone of the main repository

主存储库的克隆

A-B-C-D-E-F-G-H

in a different folder than your repo

在与您的回购不同的文件夹中

            A'-B'-C'-D'

Fetch your feature-branch from your messed up repository.

从混乱的存储库中获取您的功能分支。

git fetch ../messed-up-repo

Where ../messed-up-repois the path to your second repository.

../messed-up-repo您的第二个存储库的路径在哪里。

This will pull all changes from the other repo, without merging them. The reason for this is because your branch will not find a common ancestor with the master branch, thus it will be hard or maybe even impossible to merge them.

这将从另一个 repo 中提取所有更改,而不合并它们。这样做的原因是因为您的分支将找不到与 master 分支的共同祖先,因此合并它们将很难甚至不可能。

The main repository will now look like this:

主存储库现在将如下所示:

 A-B-C-D-E-F-G-H                       # master
/
\
 ------------A'-B'-C'-D'               # messy

Create a new branch

创建一个新分支

git branch new-featurebranch aabbccdd

Where aabbccddis the commit ID at the time you downloaded the repository. This will create a new branch at the commit you downloaded back then. The better you can guess this value, the less conflicts you will have later.

aabbccdd下载存储库时的提交 ID 在哪里。这将在您当时下载的提交处创建一个新分支。您对这个值的猜测越好,以后的冲突就越少。

                -                        # new branch
              /
 A-B-C-D-E-F-G-H                         # master
/
\
 ------------A'-B'-C'-D'                 # messy

Interactively rebase your branch onto it

以交互方式将您的分支变基到它上面

git checkout messy-featurebranch
git rebase -i new-featurebranch

This will show an editor with a list of all commits in your branch. The first one will probably be the "I downloaded a copy of the code, but didnt clone it properly" commit. DELETE THAT LINE. All other commits should then apply seamlessly.

这将显示一个编辑器,其中包含您分支中所有提交的列表。第一个可能是“我下载了代码的副本,但没有正确克隆它”提交。删除该行。然后所有其他提交应该无缝应用。

               -B'-C'-D'                 # new branch
              /
 A-B-C-D-E-F-G-H                         # master
/
\
 ------------A'                          # messy

回答by Asenar

As it seems to be a really few edition, I will suggest to just clone the main repo (in a new directory), then overwrite their files with your own and make a new commit.

由于它似乎是一个非常少的版本,我建议只克隆主存储库(在一个新目录中),然后用您自己的文件覆盖它们的文件并进行新的提交。

Otherwise there is no "simple way" to do it, but maybe some hints :

否则没有“简单的方法”可以做到,但也许有一些提示:

  1. clone into a new directory the main repo https://github.com/myaccountname/project.git
  2. add your old working copy as a remote : git remote add old /path/to/old/repo/
  3. git fetch` will import all your modifications
  1. 将主 repo 克隆到一个新目录中 https://github.com/myaccountname/project.git
  2. 添加您的旧工作副本作为远程: git remote add old /path/to/old/repo/
  3. git fetch` 将导入您的所有修改

Now there is several option, according to what is best for you : git cherry-pick <1st sha1 you want>..<last commit (probably old/issue_fix)>seems fit your needs, but if it does not you can try a git rebase --interactiveNils, but you can have issue if the 1st commit you made wasn't an empty commit.

现在有几种选择,根据什么最适合您:git cherry-pick <1st sha1 you want>..<last commit (probably old/issue_fix)>似乎符合您的需求,但如果不符合您的需求,您可以尝试git rebase --interactiveNils,但如果您所做的第一次提交不是空提交,您可能会遇到问题。