Github trials and tribulations: Accessing non-'main' branches of a pulled repo locally
I initialized a local github repository and then performed a 'git pull' of an existing online repository, so that its files were brought into my local repository folder.
The online repository that I pulled from has multiple branches. However, when I run
git branch -a
from my local repository, I only see the main
branch listed.
I need to ultimately move over into one of the pulled repository's other branches, the Features
branch, and commit and push my work to that branch, NOT main
. Although like I said, I currently don't even see the Features
branch as available.56 Replies
Run
git fetch --all
to have all the remote branches on your localThat is not working
what did you do exactly to get the git pull to work?
nicknall@Nicks-Mac-mini GoWithGigRepo % git remote add origin https://github.com/jordanholmesmotors/GoWIthGIG.git
what's the output of
git remote -v
?Nothing, nothing's listed
that should be showing you two lines for your github thing... Did you get any errors with the remote add command? Or the pull? Are you sure you're in the same folder where you ran git remote add?
Yea I'm sure I'm in the same folder.. it was empty, then after the git pull it got all of the files.
Sorry, the above 'add origin' line wasn't accurate. I had tried creating a remote 'origin' repository, but have since removed it.
The second picture here is from when I did the git pull, and no there weren't any issues, errors etc
You can see after the git pull I tried to get the
Features
branch by using git checkout Features
but that didn't workso you've removed the remote repo? You need to re-add it then, and then run Ragnar's
git fetch --all
Yea, so from the beginning, I did the git pull and I got the files.
And I still hadn't created any remote repo. All I'd done is the pull. Running
git remote
still yielded nothing.you can't pull without a remote repo though
like, pull by definition is a fetch from a remote repo
So my 'pull' line:
nicknall@Nicks-Mac-mini GoWithGigRepo % git pull https://github.com/jordanholmesmotors/GoWIthGIG.git
WAS succesful. I got the files from this line. The 'remote repo' then, is the https://github.com/jordanholmesmotors/GoWIthGIG.git
, right?ah, yes, but only once
just for that command
you'll have to run the
git add remote origin https://github.com/jordanholmesmotors/GoWIthGIG.git
okay, yes, and I did try that too. Let me show you what it was giving me. One second
My listed branches were preceded with
remote/origin/
instead of just being listed by name like I expected
the way 'main' isYes that's how that works. Checking out "Features" should still work
I created the 'origin' remote repo, then performed a
git fetch --all
in order to get the branches, which you can see still weren't being listed (still just saw 'main' from the git branch-a
).
But if I understand correctly, I need to make another origin remote , and then instead of running a git fetch
, i need to do a pull
?The wording isn't quite right, but I think so? I have to run though, I'll be back later
is there anything in this remote branch? Or, this is your personal repo? If not, why not just create the branch locally, and then push it up when you have done work in it?
Yes, the branch does have stuff in it already
I've added my remote repo, 'origin'
so in order to have access to the pulled repo's other branches, do I need to do a git fetch of something?
yes, but you said that didn't work
generally tho, if you are the only person working in a repo
there is no reason to pre-create branches on github, just make them locally and push them up
unless you are working from multiple computers
then you can pull or fetch the branch by name or by all
So if the pulled repo has a 'Features' branch in it, and I want 'Features' to show up locally when I type
git branch -a
, how do I get that to happen?can you see the remote branches from your terminal
like git branch -r?
no I don't see anything with
git branch -r
and git branch -a
only gives me main
my suggestion is to nuke everything have done
and then start fresh and clone it
and try again because something doesn't seem to be right
but if you can't see the remote branches, you won't be able to pull them
and there shouldn't be a reason you can't see the remote branches if you cloned the directory correctly, and they exist
all that's been done so far is:
1. I initialized a blank github repo in a local folder by navigating to inside of my local folder and running the
git init
command
2. I pulled from the existing online repo with : git pull https://github.com/jordanholmesmotors/GoWIthGIG.git
3. now, I've also added my remote repo with :git remote add origin https://github.com/jordanholmesmotors/GoWIthGIG.git
I never cloned anything.ahh, yeah, what I would say is, clone the repo first rather than initiliazing a blank local repo
Is a clone still going to allow me to perform git pushes to the online one though?
clone is the git command to copy the remote repo to your local, which is what you want to do. once you do that, they are synced and you push and pull and fetch or checkout as needed
okay, i understand, so then what's the purpose of using pull?
to keep your local in sync
say you are working on a repo with multiple ppl
and you have your own branch and someone updates the main between times you have pushed your updates
you can pull to make sure all their changes are there and stuff like that
ok, that makes sense
since I've already pulled into this local folder.. can I just trash those files safely and then start a clone? or does something need to be done in between so that the online repo is not affected?
you can just delete the whole thin on local no worries
ok
or leave it, and create a new folder, and clone into that
and once satisfied, then delete the old one
it's not really linked to the repo anyhow, which is why you can't see any of the remote branches
gotcha okay. I'll remember to just do a clone from now on, for next time. I just thought 'pull' was how you did this
but pull is really just giving you the latest copy of the 'main' branch of a\ repo i guess
oh just to be clear
you only clone it once
after that you pull or fetch anytime you need
and pull can pull anything, not just main, you just have to specify what you want to pull
right, okay, and I can also push to the online repo right?
yes
but you will probably also want to brush up on how to merge your branches back into the main at some point, 🙂
So this is correct?
I just wonder why I have 2 'main' branches now?
I thought I would just see the branches listed as 'main, Bugs, Features', etc.. the way the top 'main' one is. Not having the "remotes/origin/' in front of them
main is your only local
everything else is on the repo, which is why it says remote
but if you do like git checkout -b, you can create a new branch locally which is what I do, and then push it up
I guess as long as a commit/push with this configuration will still actually push my changes to the online repo then it doesn't matter
if you're going to be relying on that, please read up on how git remotes work
It just looked weird to me, all of the 'remotes/origin/' branches written in red like that
Previously, working in another repo, my branches were listed like this:
those are local branches
When I 'checkout''d one of these, and push from inside of that checkout'd branch, it would still update the online repo branch with the same name
you'll get a helpful error message when you try:
unless you checked out an existing remote branch, then that setup would've been done already
So like, what is the most basic, straighforward way to simply access a branch from an online repo, to be able to work inside of it locally, and to push to that branch?
My boss made a repo, he created branches in it. I'm only supposed to push to the 'Features' branch that he created.
I've so far I've pulled the online repo into a local repo, and just now I've run a git clone of the online repo (using a different folder).
I just want to be able to work locally inside the 'Features' branch of the online repo, so that when I run a git add, commit, push, it is affecting the online repo's 'Features' branch.
I just want the most basic way to do that
there's better ways to do
git add .
, where you interactively approve changes you made, but that's outside of scope here. That is the simplest way to work with gitokay, that works then, thank you
it's a good idea to skim the git book if you're going to be working with it btw https://git-scm.com/book/en/v2
also, to add to what jochem said. after you have checked out and pulled the features branch, when you do git branch -a, it will show in the list under main, and it will have an asterisk next to it
and that's how you can see what branch you are working in
and also, when you push, you can specify the branch name to push to, to give you 100% peace of mind you are not pushing it to main. So instead of just git push, you can do git push origin [branch name] for instance.
Yes, that's how I'd been pushing previously in my other repo;
git push -u origin gwg-Nick