Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I deal with vim buffers when switching git branches?

So, I've got a ton of files open in my vim buffer, and I'm working on a feature branch with git. Suddenly, I realize that I need to revert back to my master branch to make a quick fix.

After making my commits, I leave my vim session open and switch back over to the master branch. However, when I try and load the files I need from the buffer, I now get the message

Warning: File "<filename>" has changed since editing started

This makes complete since to me. The file in the buffer is from my feature branch, and now that I have switched back to master branch, there are understandably some deltas to deal with.

My question is, how is this situation dealt with on a professional level?

Thinking of solutions, I could just close all the buffers and re-open the files. However, I was hoping there is a "smart" way to do this. Is there a way to replace the files in the current vim buffer with their counterparts in the desired branch? Is there a way of maintaining multiple buffers, one per git branch?

Ultimately, I want to be able to switch between git branches, and have the open vim session just "know" which version of the file should currently reside in the buffer.

Part of this is that I am still learning about how vim buffers work, and have just started to use them extensively. So if I made any assumptions that don't hold, please correct me where appropriate.

like image 572
Zack Avatar asked Dec 03 '15 15:12

Zack


People also ask

What happens when you switch branches in Git?

When you switch a branch, all files that are under control of Git will be replaced with the state of the new branch. That includes changes to files as well as additions and deletions. In your case this means that you have some files in your current 'local' branch that simply do not exist in the master.

Does switching branches change files?

When you switch branches, files that are not tracked by Git will remain untouched. Since Git does not know about new_file. dat , it will not just delete it.

Which command can be used to switch between the branches?

Git checkout works hand-in-hand with git branch . The git branch command can be used to create a new branch. When you want to start a new feature, you create a new branch off main using git branch new_branch . Once created you can then use git checkout new_branch to switch to that branch.


2 Answers

I would recommend you :set autoread in your ~/.vimrc:

When a file has been detected to have been changed outside of Vim and it has not been changed inside of Vim, automatically read it again.

like image 114
Ingo Karkat Avatar answered Sep 28 '22 04:09

Ingo Karkat


You can do a :bufdo e to force vim to reread all the open buffer from the filesystem.

bufdo is a way to apply a command to multiple buffers. (Similarly there is a tabdo if you are using tabs)

So it will replace the version of the old branch by the version of the new branch.

(Similar question & answer there : Git branching while files are opened )

like image 34
Xavier T. Avatar answered Sep 28 '22 03:09

Xavier T.