0

When I checkout an existing remote branch, the current buffer remains on the old branch. However if I open a new file, that new file is on the checked-out branch.

My current workflow is this:

  1. editing file a.txt
  2. magit-checkout feature (a.txt exists on feature)
  3. close a.txt
  4. open a.txt

And the workflow I want to get is this:

  1. editing file a.txt
  2. magit-checkout feature (a.txt exists on feature)
  3. a.txt is now the version from feature

Is there a way to make the current buffer file be checked out on the new branch?

p.s. I'm using Spacemacs, don't know if that matters here.

9
  • I experience this too. I don't have a solution, but I have a workaround that's slightly faster. I have bound revert-buffer to F5, so instead of closing the buffer and opening another one, I just hit a single key to reload.
    – MTS
    Sep 10, 2019 at 16:23
  • Remember, a buffer is not the same thing as a file. Probably the right way to go about this would be to define some kind of hook that runs after magit-checkout, which would identify buffers associated to the repo and run revert-buffer on all of them.
    – MTS
    Sep 10, 2019 at 16:28
  • This old question (and answer) are related: emacs.stackexchange.com/questions/34589/…
    – MTS
    Sep 10, 2019 at 16:30
  • @MTS I saw that but didn't understand it was related, thank you for the pointer. Sep 10, 2019 at 16:32
  • I see that Spacemacs (or someone) also enables Global-Auto-Revert minor mode... and for some reason after using revert-buffer I now see the behavior I originally wanted, without any extra action on my part. Maybe the auto reverting only happens if magit knows the file exists in both branches. It's mysterious, but I have a workaround now, so thanks. Sep 10, 2019 at 16:34

1 Answer 1

1

(Disclaimer: I don't know if this is how Magit and Spacemacs actually work, but it fits my hypothesis.)

When you magit-checkout a remote branch, magit can't know if the current file you have open in the buffer also exists in the remote branch. However, git-checkout changed the file, so a workaround (pointed out in a comment by MTS) is to revert the file (as in, revert to the version that changed on disk) using M-x revert-buffer RET.

Once you've reverted the file once, checking out the previous branch again (or checking out a new branch) will automatically revert the file going forward.

At least, this occurs on my system (Spacemacs with the Global-Auto-Revert minor mode enabled).

So the modified workflow is:

  1. edit a.txt
  2. magit-checkout remote branch feature
  3. if the file does not update, force it using revert-buffer
  4. a.txt will be the version from feature
  5. subsequent checkouts will revert automatically

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.