Reputation: 20202
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)
Upvotes: 23
Views: 10766
Reputation: 9552
Not sure whether I am missing something here, I always fall back to being a beginner when it comes to this.
You can just easily rename your branch, for example using lazygit that makes it a one button step, or just check out a new branch from your given one with the right name.
Trying at first just with push
:
$ git push
fatal: The upstream branch of your current branch does not match
the name of your current branch. To push to the upstream branch
on the remote, use
git push origin HEAD:master
To push to the branch of the same name on the remote, use
git push origin HEAD
To choose either option permanently, see push.default in 'git help config'.
You see here two options, choose the latter.
Thus, at first, rename your branch to the name that it has to have in the repository. As soon as your local branch name is the one that you want to see in the repository, you only need to run
git push -f origin HEAD
so that the new local branch (HEAD) would be pushed on top of the remote master (origin). It pushes to the remote.
Looks like:
$ git push -f origin HEAD
Enumerating objects: 21, done.
Counting objects: 100% (21/21), done.
Delta compression using up to 8 threads
Compressing objects: 100% (9/9), done.
Writing objects: 100% (11/11), 1019 bytes | 1019.00 KiB/s, done.
Total 11 (delta 4), reused 0 (delta 0), pack-reused 0
remote:
remote: To create a merge request for my_new_remote_branch_name_that_is_now_the_name_of_HEAD, visit:
remote: https://gitlab.com/my_URL_to_the_repo
remote:
To gitlab.com:my_URL_to_the_repo
* [new branch] HEAD -> my_new_remote_branch_name_that_is_now_the_name_of_HEAD
The command git origin HEAD
is just short for
git push -u origin HEAD:my_new_remote_branch_name_that_is_now_the_name_of_HEAD
Which is the same as:
git push --set-upstream origin HEAD:my_new_remote_branch_name_that_is_now_the_name_of_HEAD
Only if you want to choose a remote name that is not the one of HEAD, you need the long command of the answer.
Upvotes: 1
Reputation: 241908
Use the colon notation:
git push -u origin local_branch:remote_branch
Upvotes: 43