When starting to use git, it is common practice to use local copies of branches from remotes which are kept in sync against the remote ones. For instance, here are some workflows that are commonly used following this practice:
create a new feature branch
git switch main
git pull
git switch -c new-feature-branch
rebase on top of the latest changes of main branch
git switch main
git pull
git switch new-feature-branch
git pull -r # this works until we change the upstream branch, of course
rebase on top of the latest changes of main after the upstream is changed
git switch main
git pull
git switch new-feature-branch
git rebase main
You can see the effort of doing the switch/pull every time. Is it possible to avoid keeping the local copy of the branch in sync, or, even better, have no local copy at all?
CodePudding user response:
Yes, it is possible to avoid the local copy of the remote branch and having to keep it in sync. Here's how to do those 3 things without using a local copy:
create a new feature branch
git fetch origin
git checkout -b new-feature-branch origin/main
rebase on top of the latest changes of main branch
git pull
Use -r
if you have not setup pull.rebase
to true... and this works as long as we do not change the upstream
rebase on top of the latest changes of main after the upstream is changed
git fetch # did not provide a remote as I am _assuming_ it's a single remote.... specify it if needed
git rebase origin/main
And this way we can avoid having to create a local copy of main
in the first place.
CodePudding user response:
Rebase workflow is widely used for good reason but the command sequences you're showing for it can be improved.
git config pull.rebase true
once, to set your local workflow. Add --global
to make this your personal default.
To create a new branch off the upstream main:
git switch -t -c feature origin/main
and whenever you want to catch up with upstream:
git pull