Search code examples
vim

Git hub commit issues


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.


Solution

  • 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.