threads / discuss / 62706

connecting the local main branch to the remote origin/main without pushing

Subject: connecting the local main branch to the remote origin/main without pushing

## tl;dr

10 messages between Dec 28, 2024 and Dec 29, 2024.

replies: 9people: 5as markdown or json

crstml@libero.it· Dec 28, 2024, 15:47 UTC · lore
Hello all

I would like to put a set of files under version control and I have some issues with the workflow. Let me explain:

First I create the bare repository with the command:
     git init --bare -b main ~/rps/project-x.git
Then I can proceed in one of the following ways:
---- Method 1 ----
    By first cloning the remote repository locally and next
    putting the files under version control. All running the
    following commands:
    1    cd ~/projects;
    2    git clone ~/rps/project-x.git project-x
    3    cp ~/my-existing-project-x-files/* project-x
    4    cd project-x
    5    git add .
    6    git commit -m "fc"
    7    git push origin
    8    rm -rf ~/my-existing-project-x-files   # clean your home folder
---- Method 2 ----
    By putting the existing files under version control and next
    adding the remote. Running the following commands:
    1    cd ~/projects/project-x
    2    git init -b main
    3    git add .
    4    git commit -m "fc"
    5    git remote add origin ~/rps/project-x.git
    6    git push --set-upstream origin main
Let me discuss both these methods:
Method 1:
   Everything works but the cp statement may be problematic. If
   you have hidden files (starting with .) or if you want to
   preserve the file permissions and owenership, the invokation
   of the cp command is trickier.
   After you copy the files all the next statements work well.
   The main branch in the cloned repository is connected to the
   upstream origin/main branch and "git push" will work.
   There is one more small problem with this workflow: the
   statement 8 is ugly.
Method 2
   Everything is very clean (apparently). We don't have to think
   to file permissions, hidden files, tricky cp invocations and
   there is no need to clean your home folder at the end. We are
   interested to put files under version control, so we focus only
   the version control system.
   The problem  with this workflow (from my point of view) is the
   statement 6. This statement makes two things which is contrary
   to the UNIX philosophy: programs that do one thing and do it well.
       1) The command connects the local main branch to the
          remote origin/main branch.
       2) Pushes the files to the remote.
   From my point of view instead of executing the statement 6 I would
   like to execute the following two statements that I will number
   here as 6.1 and 6.2:
    6.1  # To connect the main branch to origin/main
         #
         git branch -u origin/main main
    6.2  # To push to the remote.
         #
         git push origin
    However, the statement 6.1 does not work. Git prints the following
    message.
    hint: If you are planning on basing your work on an upstream
    hint: branch that already exists at the remote, you may need to
    hint: run "git fetch" to retrieve it.
    hint:
    hint: If you are planning to push out a new local branch that
    hint: will track its remote counterpart, you may want to use
    hint: "git push -u" to set the upstream config as you push.
    hint: Disable this message with "git config advice.setUpstreamFailure false"
    The end solution it suggests to use with "git push -u" which
    is the same as the statement on line 6 that I would like to
    avoid.  I would add that by issuing a "git fecth" before 6.1
    would not bring the remote branch origin/main in the local
    repository.
    The core of the problem is that the local branch main is not connected
    to the origin/main branch.
My question is:
      Is it possible when applying the method 2 to have (without pushing)
      the local main branch connected to the remote origin/main branch as
      in the case of method 1 which by cloning connects these branches.

Thank you Cristian

rsbecker@nexbridge.com· Dec 28, 2024, 16:11 UTC · re: crstml@libero.it · lore

RE: connecting the local main branch to the remote origin/main without pushing

On December 28, 2024 10:47 AM crstml@libero.it wrote:
Show 111 quoted lines
>I would like to put a set of files under version control and I have some issues with
>the workflow. Let me explain:
>
>First I create the bare repository with the command:
>
>     git init --bare -b main ~/rps/project-x.git
>
>Then I can proceed in one of the following ways:
>
>
>---- Method 1 ----
>
>    By first cloning the remote repository locally and next
>    putting the files under version control. All running the
>    following commands:
>
>    1    cd ~/projects;
>    2    git clone ~/rps/project-x.git project-x
>    3    cp ~/my-existing-project-x-files/* project-x
>    4    cd project-x
>    5    git add .
>    6    git commit -m "fc"
>    7    git push origin
>    8    rm -rf ~/my-existing-project-x-files   # clean your home folder
>
>---- Method 2 ----
>
>    By putting the existing files under version control and next
>    adding the remote. Running the following commands:
>
>    1    cd ~/projects/project-x
>    2    git init -b main
>    3    git add .
>    4    git commit -m "fc"
>    5    git remote add origin ~/rps/project-x.git
>    6    git push --set-upstream origin main
>
>
>Let me discuss both these methods:
>
>Method 1:
>
>   Everything works but the cp statement may be problematic. If
>   you have hidden files (starting with .) or if you want to
>   preserve the file permissions and owenership, the invokation
>   of the cp command is trickier.
>
>   After you copy the files all the next statements work well.
>   The main branch in the cloned repository is connected to the
>   upstream origin/main branch and "git push" will work.
>
>   There is one more small problem with this workflow: the
>   statement 8 is ugly.
>
>
>
>Method 2
>
>   Everything is very clean (apparently). We don't have to think
>   to file permissions, hidden files, tricky cp invocations and
>   there is no need to clean your home folder at the end. We are
>   interested to put files under version control, so we focus only
>   the version control system.
>
>
>   The problem  with this workflow (from my point of view) is the
>   statement 6. This statement makes two things which is contrary
>   to the UNIX philosophy: programs that do one thing and do it well.
>
>       1) The command connects the local main branch to the
>          remote origin/main branch.
>
>       2) Pushes the files to the remote.
>
>   From my point of view instead of executing the statement 6 I would
>   like to execute the following two statements that I will number
>   here as 6.1 and 6.2:
>
>    6.1  # To connect the main branch to origin/main
>         #
>         git branch -u origin/main main
>
>    6.2  # To push to the remote.
>         #
>         git push origin
>
>    However, the statement 6.1 does not work. Git prints the following
>    message.
>
>    hint: If you are planning on basing your work on an upstream
>    hint: branch that already exists at the remote, you may need to
>    hint: run "git fetch" to retrieve it.
>    hint:
>    hint: If you are planning to push out a new local branch that
>    hint: will track its remote counterpart, you may want to use
>    hint: "git push -u" to set the upstream config as you push.
>    hint: Disable this message with "git config advice.setUpstreamFailure false"
>
>    The end solution it suggests to use with "git push -u" which
>    is the same as the statement on line 6 that I would like to
>    avoid.  I would add that by issuing a "git fecth" before 6.1
>    would not bring the remote branch origin/main in the local
>    repository.
>
>    The core of the problem is that the local branch main is not connected
>    to the origin/main branch.
>
>My question is:
>      Is it possible when applying the method 2 to have (without pushing)
>      the local main branch connected to the remote origin/main branch as
>      in the case of method 1 which by cloning connects these branches.

I think method 2 is failing for you because you do not have origin/main in your local repository. That requires a git fetch. Git fetch will not overwrite your working area, but is needed so that tracking can occur with an existing remote branch.

The reason git push -u works is that the resolution of your branch tracking can be worked out by git as part of the push, where the remote reference is known. Without that, the git branch -u does not work (no reference).

So do the git fetch as 5.9 in Method 2, then 6.1 should work, assuming origin/main exists in your remote. This downloads the clone history without modifying your work area, so it should be fine.

--Randall
crstml@libero.it· Dec 29, 2024, 09:01 UTC · re: rsbecker@nexbridge.com · lore

Re: connecting the local main branch to the remote origin/main without pushing

rsbecker@nexbridge.com wrote:
Show 126 quoted lines
> On December 28, 2024 10:47 AM crstml@libero.it wrote:
>> I would like to put a set of files under version control and I have some issues with
>> the workflow. Let me explain:
>>
>> First I create the bare repository with the command:
>>
>>      git init --bare -b main ~/rps/project-x.git
>>
>> Then I can proceed in one of the following ways:
>>
>>
>> ---- Method 1 ----
>>
>>     By first cloning the remote repository locally and next
>>     putting the files under version control. All running the
>>     following commands:
>>
>>     1    cd ~/projects;
>>     2    git clone ~/rps/project-x.git project-x
>>     3    cp ~/my-existing-project-x-files/* project-x
>>     4    cd project-x
>>     5    git add .
>>     6    git commit -m "fc"
>>     7    git push origin
>>     8    rm -rf ~/my-existing-project-x-files   # clean your home folder
>>
>> ---- Method 2 ----
>>
>>     By putting the existing files under version control and next
>>     adding the remote. Running the following commands:
>>
>>     1    cd ~/projects/project-x
>>     2    git init -b main
>>     3    git add .
>>     4    git commit -m "fc"
>>     5    git remote add origin ~/rps/project-x.git
>>     6    git push --set-upstream origin main
>>
>>
>> Let me discuss both these methods:
>>
>> Method 1:
>>
>>    Everything works but the cp statement may be problematic. If
>>    you have hidden files (starting with .) or if you want to
>>    preserve the file permissions and owenership, the invokation
>>    of the cp command is trickier.
>>
>>    After you copy the files all the next statements work well.
>>    The main branch in the cloned repository is connected to the
>>    upstream origin/main branch and "git push" will work.
>>
>>    There is one more small problem with this workflow: the
>>    statement 8 is ugly.
>>
>>
>>
>> Method 2
>>
>>    Everything is very clean (apparently). We don't have to think
>>    to file permissions, hidden files, tricky cp invocations and
>>    there is no need to clean your home folder at the end. We are
>>    interested to put files under version control, so we focus only
>>    the version control system.
>>
>>
>>    The problem  with this workflow (from my point of view) is the
>>    statement 6. This statement makes two things which is contrary
>>    to the UNIX philosophy: programs that do one thing and do it well.
>>
>>        1) The command connects the local main branch to the
>>           remote origin/main branch.
>>
>>        2) Pushes the files to the remote.
>>
>>    From my point of view instead of executing the statement 6 I would
>>    like to execute the following two statements that I will number
>>    here as 6.1 and 6.2:
>>
>>     6.1  # To connect the main branch to origin/main
>>          #
>>          git branch -u origin/main main
>>
>>     6.2  # To push to the remote.
>>          #
>>          git push origin
>>
>>     However, the statement 6.1 does not work. Git prints the following
>>     message.
>>
>>     hint: If you are planning on basing your work on an upstream
>>     hint: branch that already exists at the remote, you may need to
>>     hint: run "git fetch" to retrieve it.
>>     hint:
>>     hint: If you are planning to push out a new local branch that
>>     hint: will track its remote counterpart, you may want to use
>>     hint: "git push -u" to set the upstream config as you push.
>>     hint: Disable this message with "git config advice.setUpstreamFailure false"
>>
>>     The end solution it suggests to use with "git push -u" which
>>     is the same as the statement on line 6 that I would like to
>>     avoid.  I would add that by issuing a "git fecth" before 6.1
>>     would not bring the remote branch origin/main in the local
>>     repository.
>>
>>     The core of the problem is that the local branch main is not connected
>>     to the origin/main branch.
>>
>> My question is:
>>       Is it possible when applying the method 2 to have (without pushing)
>>       the local main branch connected to the remote origin/main branch as
>>       in the case of method 1 which by cloning connects these branches.
> 
> I think method 2 is failing for you because you do not have origin/main in your
> local repository. That requires a git fetch. Git fetch will not overwrite your
> working area, but is needed so that tracking can occur with an existing
> remote branch.
>
> The reason git push -u works is that the resolution of your branch tracking
> can be worked out by git as part of the push, where the remote reference
> is known. Without that, the git branch -u does not work (no reference).
> 
> So do the git fetch as 5.9 in Method 2, then 6.1 should work, assuming origin/main
> exists in your remote. This downloads the clone history without modifying
> your work area, so it should be fine.
> 
Good observations.

I've imagined that the commit history and the configuration of the remote repository is necessary to be known to the local git and I've also performed a test in which I've executed a "git fetch" before 6.1 (without "git fetch" no information from the remote repository exists locally).

Unfortunately it did not work and git printed out the following message:

hint: hint: If you are planning on basing your work on an upstream hint: branch that already exists at the remote, you may need to hint: run "git fetch" to retrieve it. hint: hint: If you are planning to push out a new local branch that hint: will track its remote counterpart, you may want to use hint: "git push -u" to set the upstream config as you push. hint: Disable this message with "git config advice.setUpstreamFailure false"

> --Randall
> 
Andreas Schwab· Dec 28, 2024, 17:15 UTC · re: crstml@libero.it · lore

Re: connecting the local main branch to the remote origin/main without pushing

On Dez 28 2024, crstml@libero.it wrote:
> My question is:
>      Is it possible when applying the method 2 to have (without pushing)
>      the local main branch connected to the remote origin/main branch as
>      in the case of method 1 which by cloning connects these branches.
You can establish the effect by setting two config entries:

$ git config branch.main.remote origin $ git config branch.main.merge refs/heads/main

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."
Junio C Hamano· Dec 28, 2024, 17:20 UTC · re: Andreas Schwab · lore

Re: connecting the local main branch to the remote origin/main without pushing

Andreas Schwab <schwab@linux-m68k.org> writes:
Show 11 quoted lines
> On Dez 28 2024, crstml@libero.it wrote:
>
>> My question is:
>>      Is it possible when applying the method 2 to have (without pushing)
>>      the local main branch connected to the remote origin/main branch as
>>      in the case of method 1 which by cloning connects these branches.
>
> You can establish the effect by setting two config entries:
>
> $ git config branch.main.remote origin
> $ git config branch.main.merge refs/heads/main
Perfect ;-)  Thanks.
Jeff King· Dec 28, 2024, 19:08 UTC · re: Andreas Schwab · lore

Re: connecting the local main branch to the remote origin/main without pushing

On Sat, Dec 28, 2024 at 06:15:01PM +0100, Andreas Schwab wrote:
Show 11 quoted lines
> On Dez 28 2024, crstml@libero.it wrote:
> 
> > My question is:
> >      Is it possible when applying the method 2 to have (without pushing)
> >      the local main branch connected to the remote origin/main branch as
> >      in the case of method 1 which by cloning connects these branches.
> 
> You can establish the effect by setting two config entries:
> 
> $ git config branch.main.remote origin
> $ git config branch.main.merge refs/heads/main
Also:
  git branch --set-upstream-to=origin/main main
(sets the same config variables, but maybe a little more ergonomic).
-Peff
Andreas Schwab· Dec 28, 2024, 19:39 UTC · re: Jeff King · lore

Re: connecting the local main branch to the remote origin/main without pushing

On Dez 28 2024, Jeff King wrote:
Show 19 quoted lines
> On Sat, Dec 28, 2024 at 06:15:01PM +0100, Andreas Schwab wrote:
>
>> On Dez 28 2024, crstml@libero.it wrote:
>> 
>> > My question is:
>> >      Is it possible when applying the method 2 to have (without pushing)
>> >      the local main branch connected to the remote origin/main branch as
>> >      in the case of method 1 which by cloning connects these branches.
>> 
>> You can establish the effect by setting two config entries:
>> 
>> $ git config branch.main.remote origin
>> $ git config branch.main.merge refs/heads/main
>
> Also:
>
>   git branch --set-upstream-to=origin/main main
>
> (sets the same config variables, but maybe a little more ergonomic).
That does not work if origin/main does not exist yet.
-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."
crstml@libero.it· Dec 29, 2024, 10:01 UTC · re: Andreas Schwab · lore

Re: connecting the local main branch to the remote origin/main without pushing

Andreas Schwab wrote:
Show 12 quoted lines
> On Dez 28 2024, crstml@libero.it wrote:
> 
>> My question is:
>>       Is it possible when applying the method 2 to have (without pushing)
>>       the local main branch connected to the remote origin/main branch as
>>       in the case of method 1 which by cloning connects these branches.
> 
> You can establish the effect by setting two config entries:
> 
> $ git config branch.main.remote origin
> $ git config branch.main.merge refs/heads/main
> 
Indeed.

By making a diff between a folder containing a cloned empty repository (method 1) and an empty folder in which "git init" and "git remote" were run (method 2) the only difference is in the .git/config file. In the cloned version the file contains the following section:

[branch "main"]
         remote = origin
         merge = refs/heads/main
These commands add exactly this section to the file.

"git branch -u" does exactly the same thing when connecting a local branch to an existing remote branch. It adds this section. "git push ---set-upstream" also does the same thing.

It would be nice if "git branch -u" would work for an empty remote repository and allow us to set the upstream branch.

Cristian
rsbecker@nexbridge.com· Dec 29, 2024, 14:39 UTC · re: crstml@libero.it · lore

RE: connecting the local main branch to the remote origin/main without pushing

On December 29, 2024 5:02 AM, crstml@libero.it wrote:
Show 33 quoted lines
>Andreas Schwab wrote:
>> On Dez 28 2024, crstml@libero.it wrote:
>>
>>> My question is:
>>>       Is it possible when applying the method 2 to have (without pushing)
>>>       the local main branch connected to the remote origin/main branch as
>>>       in the case of method 1 which by cloning connects these branches.
>>
>> You can establish the effect by setting two config entries:
>>
>> $ git config branch.main.remote origin $ git config branch.main.merge
>> refs/heads/main
>>
>
>Indeed.
>
>By making a diff between a folder containing a cloned empty repository (method 1)
>and an empty folder in which "git init" and "git remote" were run (method 2) the
>only difference is in the .git/config file. In the cloned version the file contains the
>following section:
>
>[branch "main"]
>         remote = origin
>         merge = refs/heads/main
>
>These commands add exactly this section to the file.
>
>"git branch -u" does exactly the same thing when connecting a local branch to an
>existing remote branch. It adds this section. "git push ---set-upstream"
>also does the same thing.
>
>It would be nice if "git branch -u" would work for an empty remote repository and
>allow us to set the upstream branch.
It might be a useful contribution to make git branch --force -u understand this.
crstml@libero.it· Dec 29, 2024, 10:03 UTC · re: Andreas Schwab · lore

Re: connecting the local main branch to the remote origin/main without pushing

Andreas Schwab wrote:
Show 12 quoted lines
> On Dez 28 2024, crstml@libero.it wrote:
> 
>> My question is:
>>       Is it possible when applying the method 2 to have (without pushing)
>>       the local main branch connected to the remote origin/main branch as
>>       in the case of method 1 which by cloning connects these branches.
> 
> You can establish the effect by setting two config entries:
> 
> $ git config branch.main.remote origin
> $ git config branch.main.merge refs/heads/main
> 
Excelent. Thank you for the info.
Cristian

← back to recent threads