git checkout master; use topic branches and detached heads.
It’s good to use so-called “topic” or “feature” branches to organize your work into individual features, instead of committing onto what I’ll call “mainline” or “persistent” branches directly. This is a fairly common practice, and I won’t go into full detail on why I think it’s important, but a few good reasons:
Even if we don’t commit directly onto a local master branch, we still often need to refer to the remote master branch to do things like:
So if we don’t checkout master locally, how do we these things? Easy – git
already allows you to refer to remote branches directly, via remote tracking
git diff origin/master
git merge origin/master(usually after a
git fetch origin; or just do
git pull origin masterin one step)
git branch my-new-branch origin/master
There’s no reason to have a local master branch to do any of these things.
Sometimes we need more direct access to the content at master. E.g., maybe we want to just do a build at master, without necessarily starting a new topic branch.
We can do this using detached
heads. In addition to
checking out branches, git lets you check out arbitrary commits. So you can
just checkout a remote tracking branch directly, with
Because it’s simpler not to:
origin/master) and your local copy of master (
master), which can diverge.
Merging with master is easier: you can just
git pull origin master. If you
have a local copy of master (and you don’t want it to become stale), you’d
instead need to:
git checkout master git pull origin master git checkout my-branch git merge master
↩ · January 22, 2019