Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting upstream branch to remote with different name than your local

Tags:

git

github

I created a new branch off an existing PR on github using the browse branches dropdown...typed in an new branch name so it would create a new one based off the PR I was looking at.

I then went locally and did a git checkout -b myBranch

How do I sync up my branch with the remote? I did not name my local branch as the same name as the remote.

git push -u origin my_branch - I assume that's if you already have things synced AND that the local and remote branch names are exactly the same.

So what about my situation?

I tried this but got an error

▶ git branch --set-upstream-to=origin/feature/WA-3 WA-3 error: the requested upstream branch 'origin/feature/WA-3' does not exist

More info to help

The remote branch's name is feature/WA-3 while my local is named W3

▶ git remote show origin

* remote origin
  Fetch URL: https://github.com/xxxx.git
  Push  URL: https://github.com/xxxx.git
  HEAD branch: develop
  Remote branches:
    develop                         tracked
    feature/WA-3                    new (next fetch will store in remotes/origin)
    master                          tracked
    refs/remotes/origin/w9-homepage stale (use 'git remote prune' to remove)
    w1-log-in              tracked
    wa-9                  tracked
  Local branches configured for 'git pull':
    develop            merges with remote develop
    w1-log-in merges with remote w1-user-can-log-in
    w9-homepage        merges with remote wa-9-homepage
  Local refs configured for 'git push':
    develop            pushes to develop            (up to date)
    w1-log-in pushes to w1-log-in (up to date)

enter image description here

like image 262
PositiveGuy Avatar asked Jun 01 '17 21:06

PositiveGuy


People also ask

How do I change my upstream remote?

The easiest way to set the upstream branch is to use the “git push” command with the “-u” option for upstream branch. Alternatively, you can use the “–set-upstream” option that is equivalent to the “-u” option. As an example, let's say that you created a branch named “branch” using the checkout command.

How do you push a local branch to a remote branch with a different name?

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 diff a local branch to a remote?

You can git branch -a to list all branches (local and remote) and then choose the branch name from the list (just remove remotes/ from the remote branch name. Example: git diff main origin/main (where "main" is the local main branch and "origin/main" is a remote, namely the origin and main branch.)


1 Answers

Use the colon notation:

git push -u origin local_branch:remote_branch
like image 64
choroba Avatar answered Sep 20 '22 13:09

choroba