简体   繁体   中英

Git : Push to different remote branch

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.

Steps

I have followed so far create a local git repository,

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

 git init

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

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,

   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

    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.

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.

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.

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.

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

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 G is equal to the code in your local A' , but they do not share the same history

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-repo is the path to your second repository.

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.

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 aabbccdd is 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.

                -                        # 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

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

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 --interactive Nils, but you can have issue if the 1st commit you made wasn't an empty commit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM