1

I am working on a git repository that has branches constantly being merged into it, and I would like to somehow sync

git pull <remote_master> 

So that in the future I just need to execute

git pull 

And it will execute git pull from this <remote_master> ; is this possible?

I am on a Linux machine if it makes any difference.

Further to the comments below

When I try the solution proposed, I get the following error:

error: the requested upstream branch 'git@github.com:xxx/xxx.git' does not exist
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.

Here is what my git branch looks like:

(jobserveee) patrick@patrick-lp:~/work/jobserve$ git branch
  ED-3312_adding_captureTimestamp_to_marketData_files
  ED-3312_adding_capture_timestamp_to_quote_and_trade_tables_in_MD_files
  ED-4785_Investigate_security_type_for_TSE_data_pre-2015
  ED-4931_stop_updating_rawDataFile_statuses_after_pass1_has_run
  ED-4931_stop_updating_rawDataFile_statuses_after_pass1_has_run3
* master

I am trying to sync the master branch with a remote branch (I won't paste it here as I'm not sure I am allowed), but basically the way I get my remote branch URL is: in the remote repository I click code -> SSH and I copy the URL.

Then I execute

git branch --set-upstream-to <paste_the_URL_I_described_above>
Patrick_Chong
  • 434
  • 2
  • 12

1 Answers1

1

Try with:

git switch <branch to sync>
git branch --set-upstream-to origin <branch to sync>

This sets the default remote branch for the current local branch, as perfectly explained in this answer.

Davide Calarco
  • 432
  • 4
  • 6
  • Hmmm it says that branch doesn't exist for some reason – Patrick_Chong Aug 12 '22 at 13:31
  • @Patrick_Chong: Presumably that means the upstream you're trying to set, e.g., `origin/br-abc`, does not exist in your repository yet. If branch `br-abc` does exist on `origin`, run `git fetch origin` now to have your Git call up the Git software over at `origin` and see that they have a `br-abc`, so that your Git creates your `origin/br-abc`. If *they* don't have a `br-abc` yet either, create one there, *then* run `git fetch`, or do all of that at once with `git push -u`. – torek Aug 13 '22 at 01:00
  • Davide, for the second command when you say `` is the URL for my remote branch? Or do I replace `origin` with my remote branch? – Patrick_Chong Aug 15 '22 at 08:10
  • Sorry Patrick for the unclear answer. `` is the name of the branch, such as `main` or `develop`. – Davide Calarco Aug 15 '22 at 09:08
  • So I don't enter the remote branch anywhere? – Patrick_Chong Aug 15 '22 at 09:43
  • The branch you specify in `git switch` is local, since it is selected from the local set of branches. Instead, the branch you set in `git branch --set-upstream-to origin ` is remote, since you have prepended the name of the branch with `origin`. – Davide Calarco Aug 15 '22 at 15:00
  • Thanks Davide, it is all clear now. I am still struggling a bit with torek's answer, as I am still getting `does not exist` issue. What does he mean by " `origin/br-abc` does not exist in my repository"- does he mean my local repository? I am also not sure what `origin` refers to- is this the name of m local branch I am trying to sync with the remote branch? – Patrick_Chong Aug 16 '22 at 13:12
  • `origin` is the name of your remote (a nice-looking name in place of the bare URL). In general, if you specify just the name of a branch it is referred to local environment; instead, if you specify also origin at the side of the branch name it refers to the branch on remote repository. In order to do the operations I gave, first ensure that both the local and the remote branches exist; if not, just create them. – Davide Calarco Aug 16 '22 at 15:25
  • Hi Davide, thank you for the explanation it is extremely helpful. I have tried it, but I still get the same `does not exist` error. I have added to my question above- would you kindly have a look? Thanks! – Patrick_Chong Aug 16 '22 at 16:17
  • First of all, git suggests you to run `git fetch` because your local environment may not know yet that the target remote branch exists. `` is the name of the branch, like 'main' or 'develop', not a URL. Remember to prepend the keyword `origin` like in my answer. git suggests to use the flag -u, but it is just the shorthand for --set-upstream-to. – Davide Calarco Aug 16 '22 at 18:13
  • Thanks Davide, again extremely helpful comment! - I have run a `git fetch` and it produces `* branch HEAD -> FETCH_HEAD`. I then run `git branch --set-upstream-to origin master` (as the name of the remote branch I would like to sync is "master"). But again I get the error that `requested branch origin does not exist` – Patrick_Chong Aug 17 '22 at 08:46
  • Ok. Try with replacing the space between `origin` and the branch name with a forward slash `/`. So you have for instance: `git switch ` `git branch --set-upstream-to origin/` Try listing the remotes you have through the command: `git remote` – Davide Calarco Aug 17 '22 at 10:20
  • You can also use this notation: `git branch --set-upstream-to=origin/ ` – Davide Calarco Aug 17 '22 at 10:31