Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I push a local Git branch to a remote with a different name easily?

I've been wondering if there's an easy way to push and pull a local branch with a remote branch with a different name without always specifying both names.

For example:

$ git clone myrepo.git
$ git checkout -b newb
$ ...
$ git commit -m "Some change"
$ git push origin newb:remote_branch_name

Now if someone updates remote_branch_name, I can:

$ git pull

And everything is merged / fast-forwarded. However, if I make changes in my local "newb", I can't:

$ git push

Instead, I have to:

% git push origin newb:remote_branch_name

Seems a little silly. If git-pull uses git-config branch.newb.merge to determine where to pull from, why couldn't git-push have a similar config option? Is there a nice shortcut for this or should I just continue the long way?

like image 538
jmacdonagh Avatar asked Sep 27 '22 11:09

jmacdonagh


People also ask

How do I change a branch name in both remote and local?

Git Branch Rename Command The steps to change a git branch name are: Rename the Git branch locally with the git branch -m new-branch-name command. Push the new branch to your GitHub or GitLab repo. Delete the branch with the old name from your remote repo.

How do I change the name of my remote branch?

Rename a Remote Git Branch There isn't a way to directly rename a Git branch in a remote repository. You will need to delete the old branch name, then push a branch with the correct name to the remote repository. The output confirms that the branch was deleted.


1 Answers

When you do the initial push add the -u parameter:

git push -u origin my_branch:remote_branch

Subsequent pushes will go where you want.

EDIT:

As per the comment, that only sets up pull.

git branch --set-upstream

should do it.

like image 247
Adam Dymitruk Avatar answered Oct 17 '22 01:10

Adam Dymitruk