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.

Alex Shroyer
  • 627
  • 4
  • 12
  • 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 '19 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 '19 at 16:28
  • This old question (and answer) are related: https://emacs.stackexchange.com/questions/34589/how-to-revert-buffer-after-magit-checkout – MTS Sep 10 '19 at 16:30
  • @MTS I saw that but didn't understand it was related, thank you for the pointer. – Alex Shroyer Sep 10 '19 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. – Alex Shroyer Sep 10 '19 at 16:34
  • First make sure Magit is up-to-date--I remember there was a bug related to this a while back. Then make sure either `magit-auto-revert-mode` or `global-auto-revert-mode` is enabled. The former should be enabled by default. Make sure one of these modes is enabled before you open a file. – tarsius Sep 10 '19 at 18:07
  • I want to clarify the question. Does the buffer itself not change, or are you just concerned about the git information displayed in the mode line? The mode line in a buffer will only update automatically if the file underlying that buffer changes. If you want the mode line to update automatically, see this answer: https://emacs.stackexchange.com/a/48091/6692 – MTS Sep 10 '19 at 19:04
  • @MTS My question was regarding the buffer itself not changing. The mode line tells me what version of the file I'm looking at (file A on branch X or file A on branch Y) and that also does not change. – Alex Shroyer Sep 11 '19 at 12:29
  • OK. Well, as tarsius (the author/maintainer of magit) says, as long as `magit-auto-revert-mode` is on, buffers that change should update automatically. You should try to bisect your settings to see if something is interfering with that. – MTS Sep 11 '19 at 15:23

1 Answers1

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
Alex Shroyer
  • 627
  • 4
  • 12