0

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?

eftshift0
  • 26,375
  • 3
  • 36
  • 60

2 Answers2

0

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.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • A different way to put this is: branches aren't actually shared in the first place. Don't confuse yourself by creating names that make it *look like* they are... – torek Dec 11 '22 at 13:37
  • 1
    I would really recommend that you stop advising people to say `checkout`. It seems like years since I've said it. We live in a world of switch / restore and those are the verbs to show beginners, at whom this QA is explicitly aimed – matt Dec 11 '22 at 14:33
  • I do use the English words "check out" but I always tell Git to `switch`. See for example my essay here: https://stackoverflow.com/a/72201388/341994 One might suggest that your answer basically duplicates it, though it is explicit about more cases such as rebasing. – matt Dec 11 '22 at 14:38
  • Also I do not see what the `-a` is for. Again this seems to me to mislead beginners. – matt Dec 11 '22 at 14:39
  • 1
    Thanks for the tips, guys. Will review and update when I am sitting in a computer. – eftshift0 Dec 11 '22 at 14:46
0

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
jthill
  • 55,082
  • 5
  • 77
  • 137