The remote repository has several branches like origin/daves_branch:
$ git branch -r
origin/HEAD -> origin/master
origin/daves_branch
origin/master
I want to switch to daves_branch in my local repo so it tracks origin/daves_branch. I tried:
git fetch origin daves_branch
git checkout daves_branch
Is this the correct way to git pull a remote branch and set it up for tracking locally? Or is there a better approach to ensure it pulls future updates smoothly?
You’re almost there! If daves_branch doesn’t already exist locally, just run:
git checkout -b daves_branch origin/daves_branch
This creates a local branch named daves_branch
and sets it to track the remote one. After that, git pull will automatically pull changes from origin/daves_branch
.
I usually just run:
git checkout --track origin/daves_branch
This is a shortcut that both creates the local branch and sets up tracking in one shot.
It’s clean and avoids having to type out the -b
and remote path manually. Works great when I’m hopping around between multiple feature branches.