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.
No description
No description
No description
56 Replies
Rägnar O'ock
Rägnar O'ock14mo ago
Run git fetch --all to have all the remote branches on your local
thethingisback
thethingisbackOP14mo ago
That is not working
No description
Jochem
Jochem14mo ago
what did you do exactly to get the git pull to work?
thethingisback
thethingisbackOP14mo ago
nicknall@Nicks-Mac-mini GoWithGigRepo % git remote add origin https://github.com/jordanholmesmotors/GoWIthGIG.git
Jochem
Jochem14mo ago
what's the output of git remote -v?
thethingisback
thethingisbackOP14mo ago
Nothing, nothing's listed
No description
Jochem
Jochem14mo ago
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?
thethingisback
thethingisbackOP14mo ago
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
No description
No description
thethingisback
thethingisbackOP14mo ago
You can see after the git pull I tried to get the Features branch by using git checkout Features but that didn't work
Jochem
Jochem14mo ago
so you've removed the remote repo? You need to re-add it then, and then run Ragnar's git fetch --all
thethingisback
thethingisbackOP14mo ago
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.
Jochem
Jochem14mo ago
you can't pull without a remote repo though like, pull by definition is a fetch from a remote repo
thethingisback
thethingisbackOP14mo ago
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?
Jochem
Jochem14mo ago
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
thethingisback
thethingisbackOP14mo ago
okay, yes, and I did try that too. Let me show you what it was giving me. One second
thethingisback
thethingisbackOP14mo ago
No description
thethingisback
thethingisbackOP14mo ago
My listed branches were preceded with remote/origin/ instead of just being listed by name like I expected the way 'main' is
Jochem
Jochem14mo ago
Yes that's how that works. Checking out "Features" should still work
thethingisback
thethingisbackOP14mo ago
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?
Jochem
Jochem14mo ago
The wording isn't quite right, but I think so? I have to run though, I'll be back later
ghostmonkey
ghostmonkey14mo ago
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?
thethingisback
thethingisbackOP14mo ago
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?
ghostmonkey
ghostmonkey14mo ago
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
thethingisback
thethingisbackOP14mo ago
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?
ghostmonkey
ghostmonkey14mo ago
can you see the remote branches from your terminal like git branch -r?
thethingisback
thethingisbackOP14mo ago
no I don't see anything with git branch -r and git branch -a only gives me main
ghostmonkey
ghostmonkey14mo ago
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
thethingisback
thethingisbackOP14mo ago
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.
ghostmonkey
ghostmonkey14mo ago
ahh, yeah, what I would say is, clone the repo first rather than initiliazing a blank local repo
thethingisback
thethingisbackOP14mo ago
Is a clone still going to allow me to perform git pushes to the online one though?
ghostmonkey
ghostmonkey14mo ago
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
thethingisback
thethingisbackOP14mo ago
okay, i understand, so then what's the purpose of using pull?
ghostmonkey
ghostmonkey14mo ago
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
thethingisback
thethingisbackOP14mo ago
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?
ghostmonkey
ghostmonkey14mo ago
you can just delete the whole thin on local no worries
thethingisback
thethingisbackOP14mo ago
ok
ghostmonkey
ghostmonkey14mo ago
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
thethingisback
thethingisbackOP14mo ago
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
ghostmonkey
ghostmonkey14mo ago
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
thethingisback
thethingisbackOP14mo ago
right, okay, and I can also push to the online repo right?
ghostmonkey
ghostmonkey14mo ago
yes but you will probably also want to brush up on how to merge your branches back into the main at some point, 🙂
thethingisback
thethingisbackOP14mo ago
So this is correct?
nicknall@Nicks-Mac-mini GWGRepo % git clone https://github.com/jordanholmesmotors/GoWIthGIG.git
Cloning into 'GoWIthGIG'...
remote: Enumerating objects: 754, done.
remote: Counting objects: 100% (598/598), done.
remote: Compressing objects: 100% (460/460), done.
remote: Total 754 (delta 105), reused 595 (delta 104), pack-reused 156
Receiving objects: 100% (754/754), 159.24 MiB | 4.51 MiB/s, done.
Resolving deltas: 100% (124/124), done.
nicknall@Nicks-Mac-mini GWGRepo % ls
GoWIthGIG
nicknall@Nicks-Mac-mini GWGRepo % cd GoWIthGIG
nicknall@Nicks-Mac-mini GoWIthGIG % ls
backend client gowithgig (1).sql package-lock.json
nicknall@Nicks-Mac-mini GoWIthGIG % git branch -a
* main
remotes/origin/Bugs
remotes/origin/Features
remotes/origin/HEAD -> origin/main
remotes/origin/develop
remotes/origin/main
nicknall@Nicks-Mac-mini GoWIthGIG %
nicknall@Nicks-Mac-mini GWGRepo % git clone https://github.com/jordanholmesmotors/GoWIthGIG.git
Cloning into 'GoWIthGIG'...
remote: Enumerating objects: 754, done.
remote: Counting objects: 100% (598/598), done.
remote: Compressing objects: 100% (460/460), done.
remote: Total 754 (delta 105), reused 595 (delta 104), pack-reused 156
Receiving objects: 100% (754/754), 159.24 MiB | 4.51 MiB/s, done.
Resolving deltas: 100% (124/124), done.
nicknall@Nicks-Mac-mini GWGRepo % ls
GoWIthGIG
nicknall@Nicks-Mac-mini GWGRepo % cd GoWIthGIG
nicknall@Nicks-Mac-mini GoWIthGIG % ls
backend client gowithgig (1).sql package-lock.json
nicknall@Nicks-Mac-mini GoWIthGIG % git branch -a
* main
remotes/origin/Bugs
remotes/origin/Features
remotes/origin/HEAD -> origin/main
remotes/origin/develop
remotes/origin/main
nicknall@Nicks-Mac-mini GoWIthGIG %
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
ghostmonkey
ghostmonkey14mo ago
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
thethingisback
thethingisbackOP14mo ago
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
Jochem
Jochem14mo ago
if you're going to be relying on that, please read up on how git remotes work
thethingisback
thethingisbackOP14mo ago
It just looked weird to me, all of the 'remotes/origin/' branches written in red like that
thethingisback
thethingisbackOP14mo ago
Previously, working in another repo, my branches were listed like this:
No description
Jochem
Jochem14mo ago
those are local branches
thethingisback
thethingisbackOP14mo ago
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
Jochem
Jochem14mo ago
you'll get a helpful error message when you try:
fatal: The current branch test has no upstream branch.
To push the current branch and set the remote as upstream, use

git push --set-upstream origin test

To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.
fatal: The current branch test has no upstream branch.
To push the current branch and set the remote as upstream, use

git push --set-upstream origin test

To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.
unless you checked out an existing remote branch, then that setup would've been done already
thethingisback
thethingisbackOP14mo ago
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
Jochem
Jochem14mo ago
git clone <repo>
git checkout Features
... do your work ...
git pull
git add .
git commit -m "Your commit message here"
git push
git clone <repo>
git checkout Features
... do your work ...
git pull
git add .
git commit -m "Your commit message here"
git push
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 git
thethingisback
thethingisbackOP14mo ago
okay, that works then, thank you thanks
Jochem
Jochem14mo ago
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
ghostmonkey
ghostmonkey14mo ago
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.
thethingisback
thethingisbackOP14mo ago
Yes, that's how I'd been pushing previously in my other repo; git push -u origin gwg-Nick
Want results from more Discord servers?
Add your server