Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git : Push to different remote branch

Tags:

git

github

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.

like image 459
RJR Avatar asked Jul 09 '13 09:07

RJR


People also ask

Can I push to a different remote branch?

Push Branch to Another Branch In some cases, you may want to push your changes to another branch on the remote repository. In order to push your branch to another remote branch, use the “git push” command and specify the remote name, the name of your local branch as the name of the remote branch.

How do I push to a different remote repository?

To push the commit from the local repo to your remote repositories, run git push -u remote-name branch-name where remote-name is the nickname the local repo uses for the remote repositories and branch-name is the name of the branch to push to the repository. You only have to use the -u option the first time you push.

How do I push to a remote branch with a different name?

If you want to push to a remote branch with a different name than your local branch, separate the local and remote names with a colon: git push origin local-name:remote-name .

Will git push create remote branch?

Actually, Git does not allow creating a (new, isolated) branch on a remote repository. Instead, you can push an existing local branch and thereby publish it on a remote repository.


1 Answers

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
like image 67
CharlesB Avatar answered Sep 18 '22 11:09

CharlesB