K
K
Kirill2021-12-21 12:43:04
git
Kirill, 2021-12-21 12:43:04

How to link git branches?

I connect via ssh to the server. I want to pull a branch from the repository to the server so that they are connected. I can create a new branch on the server with the same name as in the repository, and spool everything from origin/branch into it, but they don't get linked as I understand it. How to do it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Kuznetsov, 2021-12-22
@Lirrr

And to link branches, use the following command

git branch --set-upstream-to=<внешний-репозиторий>/<внешняя-ветка> <локальная-ветка>

---------
But you complicate things. If for some reason manually created a branch, then the connection will have to be configured by hand. It's not entirely clear from your question what exactly you did. I will assume that you cloned your repository to the server. This command will automatically create a remote called origin that will store the URL of the upstream repository, unpack the origin/main branch into the local main branch, and immediately create a link between them. Do you want to unpack some other branch and do it the hard way?
git clone <URL>
git branch feature # создать пустую ветку с именем feature
git switch feature # переключиться в эту новую ветку
# но свежесозданная ветка не связана ни с какой внешней, поэтому следующей командой
# придётся указать откуда и что скачивать
git pull origin feature # влить внешнюю ветку origin/feature в текущую локальную ветку
# но проще ветки сначала связать
git  branch --set-upstream-to=origin/feature feature
# и тогда заработает простой pull
git pull

But you don't really need all this!
It is enough to simply write after cloning
git switch origin/feature
One line will do all of the above. Creates a branch locally with the same name, unpacks the contents of the external branch into it, and creates a link between the local branch and the external branch.
If later, being in this branch, you execute a short command git pull, you will see that everything is connected and downloaded from where it is necessary.
And you can even make it even easier. Already during cloning, immediately unpack the desired branch.
git clone --branch feature <URL>

A
Anton, 2021-12-21
@karminski

Branches are "linked" using pull/push commands. Pull - "pulls" the code from a remote server (for example, github or another) to your local server/machine. Push does the opposite - your code "flies" to a remote server.
Or are you talking about merge?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question