cannot see the pushed branch using `git branch -r`

Viewed 57

I cloned a git repository from github, created a new branch branch1, commited some changes in it, then pushed the new branch into a remote gitlab repository. Then created another branch from branch1 named branch2, and pushed to remote gitlab repo. Now, when I use git branch -r I only see branch1. But if I clone the remote gitlab repository in another folder I can see both branches.

I also observed when I pushed for the first time into the remote repository, only branch1 was pushed, but not master branch for example.

One more observation, I can see branch2 online in my gitlab account.

I am really confused why this happens. Any help is highly appreciated.

3 Answers

Depending on your configuration, git push without specifying a branch can be interpreted in different ways, be it a global push for all branches, or a narrow push of the currently checked out branch, among others.

Check your config with git config -l for the push.default entry. You can choose between possible modes, take a look at the doc. (simple is the default since git 2.0, but before that it was matching)

To set it, just do git config --global push.default <yourPickedMode>

Also, remember that git branch -r does not check remote branches directly but local refs for them. Be sure to git fetch beforehand to get them in sync with your distant repo if needed)

I don't think you're setting the remote-tracking for your branches when you push so this is the expected behavior. See the help for git branch -r;

-r
--remotes

List or delete (if used with -d) the remote-tracking branches.

Try pushing with git push -u which sets the upstream tracking branch. Try git branch --all to see all the branches.

I got the answer thanks to the above answers. The problem was that I didn't specify the remote for push/fetch to/from the new gitlab repo. When I fetched, the data from the previous github repo were downloaded. First, I have to add a remote or replace the current origin by setting URL: git remote add new_remote <URL/remote_repo>

Then I can fetch to update: git fetch new_remote

Then checkout a branch in the new_remote: git checkout new_branch

Related