6

I have a repository of org files which I mostly use for note taking and tracking, and I track it using git. I want to add a shortcut that would quickly commit all current content of the repository (both changed, new and deleted files) as a new commit with a predefined commit message (say "update"). How can I achieve this programmatically with magit?

Thanks in advance!

10

There are two parts to this task.

  1. Figuring out how to do this on the command line.

    $ git add .
    $ git commit -m "the message"
    
  2. Figuring out what Magit functions can be used to call Git commands.

    You can either use commands or you can use low-level functions. I would recommend doing the latter, but looking at the definitions of the former might help locating the latter. The manual section named Calling Git would also come in handy.

    Looking at that page you will learn that you should probably use magit-call-git and/or magit-run-git. The difference is that the latter also refreshes the current Magit buffer and the status buffer and doing that twice would be wasteful. So either use each function once or the latter twice and call magit-refresh explicitly.

    (magit-call-git "add" ".")
    (magit-call-git "commit" "-m" "the message")
    (magit-refresh)
    

Now wrap that in a command and bind a key to it. You might even want to add the command to the commit popup by Customizing [this] Existing Popup.

  • Since I had trouble with this at first, if anyone wants to make a keybinding which executes multiple commands, you could do some eval magic, or use progn. – Nathaniel Pisarski Nov 27 '18 at 13:13
  • 1
    Part 1 can be collapsed to git commit -a -m "the message" – Andrew Swann Nov 30 '18 at 7:47
  • It seems like magit-call-git is no longer a thing? How do I do this with an up-to-date magit? – vmalloc Dec 19 '18 at 9:39
  • magit-call-git is still a thing. It is not autoloaded though, so I guess you tried to find its definition before actually loading magit. – tarsius Dec 19 '18 at 10:48
  • Doh! stupid me. You're right. Sorry for the hassle and thanks! – vmalloc Dec 30 '18 at 6:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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