Search notes:

GIT: Switch to remote branch after cloning a repository

This example tries to demonstrate how a freshly cloned local repository can be switched to a remote branch.
First, we need to create a repository that acts as the remote one:
git init -q repo
Github repository about-git, path: /commands/switch/remote-branch-after-cloning/init-repo
A file is added and commited to the repository's master branch:
touch file-1
git add file-1
git commit -q file-1 -m '+ file-1'
Github repository about-git, path: /commands/switch/remote-branch-after-cloning/create-master-branch
A new branch, named dev is created (git switch -c), another file added and commited to it:
git switch -q -c dev
touch file-dev
git add file-dev
git commit -q file-dev -m '+ file-dev'
Github repository about-git, path: /commands/switch/remote-branch-after-cloning/create-dev-branch
The repository is switched back to the master branch so that the repository, when cloned will check out this branch:
git switch -q master
Github repository about-git, path: /commands/switch/remote-branch-after-cloning/switch-to-master
We're now ready to clone the «remote» repository:
git clone -q repo repo.clone
Github repository about-git, path: /commands/switch/remote-branch-after-cloning/clone-repo
A simple git switch dev figures out that the repository should switch to the branch with the same name in the remote repository:
git switch dev
Github repository about-git, path: /commands/switch/remote-branch-after-cloning/switch-to-dev
ls -1 shows both files that are also present in the dev branch of the remote repository.
ls -1
Github repository about-git, path: /commands/switch/remote-branch-after-cloning/ls-1
The output of git branch confirms we're in the «right» branch:
git branch
Github repository about-git, path: /commands/switch/remote-branch-after-cloning/git-branch

Index