0

Hi I am trying to update one of my remote repositories. When I use commit in git bash it shows that a SWP file already exists and askes me if I want to (R)ead only the file (E)dit (D)elete. I tried editing the file, but I cannot save the changes and therefore cannot commit my changes. Any idea what is going on.

EDIT: It's working now thanks to those that helped. In terms of the question I will try to only show the relevant information when asking questions in the future.

  • 1
    This has nothing to do with Git: it's a vim issue. – torek Dec 28 '22 at 07:33
  • 1
    Does this answer your question? [How do I deal with vim's swap file system?](https://stackoverflow.com/questions/21794235/how-do-i-deal-with-vims-swap-file-system) – torek Dec 28 '22 at 07:33
  • Thing is I have already deleted the Swp file once. When I do that it just shows the same screen for the SWP file. Thanks but the link didn't really answer it. I will try researching more about VIM as I have no idea what that is. – Yaseen Latiff Dec 28 '22 at 07:56
  • FWIW, your screenshot doesn't seem to be related to the problem you encounter. – romainl Dec 28 '22 at 08:16
  • The screen shot is meant to display what I see after entering the commit command. When I checked a tutorial on what to do it suggested that I should add a command and it would need to be saved I tried entering those commands, but I was not able to save it. – Yaseen Latiff Dec 28 '22 at 08:35
  • Also this is not on a UNIX system I am using Windows 11 and I am not using Vim. – Yaseen Latiff Dec 28 '22 at 08:42
  • 1
    Git bash gives you a Unix system, that's the whole point of it, and yes, you are using Vim. – romainl Dec 28 '22 at 08:47
  • "what I see after entering the commit command" is however unrelated to the problem you are asking about. If you have to add a screenshot, at least make it one that actually shows the problem you are having. – romainl Dec 28 '22 at 09:33
  • Thank you I will remember this for next time – Yaseen Latiff Dec 28 '22 at 10:47

1 Answers1

0

When you edit a file, Vim stores your changes in a "swap file" so that you can recover your work if Vim's process is killed before you could save. This can be caused by something as problematic as a system crash or, simply, by closing your terminal window while Vim is running.

If Vim is quitted "normally", it deletes the swap file it created.

If it is not, the swap file is left behind and, the next time you open the same file, Vim will notice the presence of the swap file and offer you the possibility to recover the work that you "lost" the last time Vim quitted "abnormally".

That is the interactive screen you get with the "(R)ead only the file (E)dit (D)elete" prompt.

Now, when you are starting out with stuff like Git, Vim, the command line, etc. it may happen quite often that you find yourself in an uncomfortable situation, not knowing exactly what to do to fix it. This is frankly quite normal at this stage. In those situations, closing the terminal window might seem like a good first step in going back to a more comfortable situation to start again. In some cases, however, doing so might leave a trail of hidden files and broken states that might make it harder than you hoped to get to that comfortable situation.

When you do $ git commit, Git populates a specific temporary file located in your local .git directory:

.git/COMMIT_EDITMSG

with some text describing the commit you are about to make, and opens that file with your designated editor, which is the dreaded Vim by default.

When you start editing the file, Vim creates a swap file. If you insert your commit message, write the file, and quit Vim normally, the swap file is deleted and you won't ever be prompted about it. If you close the terminal window before writing the file, the swap file stays behind and Vim will prompt you about it the next time you try to make a commit.

From there you have quite a few options…

  • Go into your .git directory and delete the swap file(s) manually. They should be named .git/.COMMIT_EDITMSG.swp (or .swo, .swn, etc. see :help swap-files in Vim). This should give you a clean state for the next time you do $ git commit.

  • Don't close your terminal window when faced with a problem. Instead, try to analyze what went wrong and look for proper ways to fix it. If you have to close the terminal window, look for stray swap files just in case.

  • Learn Vim's basics so that you don't have to close the terminal window when you mess up your commits. Try $ vimtutor.

  • Tell Git to use a text editor you are more familiar with. Search Stack Overflow, I am sure there are dozens of Q/As about that.

  • Configure Vim to never create swap files. You can do it in Vim's configuration file:

    # in $HOME/.vimrc
    set noswapfile
    

    This won't tell it to ignore existing swap files, though, so you might want to delete them manually anyway.

  • Use a graphical Git client instead of the CLI.

romainl
  • 186,200
  • 21
  • 280
  • 313