How to make the second [git] command execute only after the first command is successful?

I know some js, can you tell some step/tip about it?

I then suspect QuickAdd might be the better option for you.

If you find that hard to use, I could write up some stuff later today using javascript, but for most it’ll be cleaner to use QuickAdd.

I will try it, thanks

1 Like

Just one question, though, what do you want to do in addition to the Create backup command? Doesn’t that command both commit and push all changes already? (Given you’ve enabled that option)

Just like I said:

Blockquote
I don’t want commit and wait success message then click push, I want one click or auto commit and push. So I got stuck on how to implement it using the command line or plugin

There is 3 steps to backup my things to github, commit ->wait → push, I want make it one click, I click one button or cmd, then leave it to git.exe
In fact, I don’t really care about this little bit of optimization. I have just been using observer for 3 days and I want to improve my familiarity with observer through these small tasks

I’m feeling a little dumb, and like I’m misunderstanding something, but this is quoted directly from the documentation:

Create Backup: Commits all changes. If “Push on backup” setting is enabled, will also push the commit.

Doesn’t that do all of those operations? What am I missing?


There should be no need to start messing with external shell execution like Gino suggests, since all needed command are available through Obsidian commands. And Templater’s javascript block could be used, but it do set a small limit opposed to QuickAdd since Templater would require an active editor to be able to execute the template.

2 Likes

Thank you, I will try it.

You state that you don’t recommend using those plugins, but you don’t say anything on why you don’t recommend them.

Furthermore you offer untested ideas on executing child processes which is considered bad practice when your infrastructure (in this case Obsidian commands) provides ample options. You’ve also stated before that you don’t really do javascript, yet you continue to provide code like this which I wouldn’t consider using before quite a few other options has been exhausted. This due to the security implications of executing child processes.

I create a file to same the code , then call templater open insert template modal, then shows “await is only valid in async functions and the top level bodies of modules” error in console,
I don’t know how to use templater to call js code, I’m a beginner

Git is not a good cross-platform plugin. Also, why install yet another plugin if one can install system-wide git and use shell or javascript from Obsidian. Isn’t what this forum is all about? Broaden our scopes?
Of course, I can be an outsider on the forum just looking in… as I told you a few weeks ago. But when I get involved (even if I’m not a pro), I am more likely to brainstorm usecases for myself or learn a trick (while pressured by someone who waits for my input on the other end) I otherwise wouldn’t have learned. But I don’t do this for likes or coffee money. I do this to get flak. Which is fine.

Commander I’ve seen to not wait for one command to execute and start another and a wait operation is not good enough.

Ok, I misunderstood the meaning of “backup” in the git plugin, which was caused by my laziness to read the documentation. I should break this bad habit. Thank you for your patient explanation

As for the git part and using Obsidian to handle it.
I don’t think if I cooked up a working script with Templater I would use it.
I want to see my files in Github Desktop and just type in some short commit message and I don’t really gain any time by automation with a script.
So for me, it’s an unwanted feature. This is why I offered the GH Desktop way. That a plugin exists…well let everyone enjoy that plugin while they have 500-600 files counting how many icecreams they ate that day…

I don’t want to say I don’t recommend this scripting… again.

But I do say it’s possible. And it’s not destructive so no harm done running scripts like this. To just add + commit + push. No harm done.

My question caused some quarrel, I’m sorry, I will continue to explore your ideas. As a beginner, it’s great to be helped, Thank you

He may have to install git first, though.

Explore the plugin…

I think we’ve already established that your original request of commit&push is best done through throught a create backup command from the Git plugin, or either of the very similar variants:

So I’m not going to say so much more about that, other than that since the plugin offers these commands some thought and consideration on how to perform this operation has been taken before including it into the plugin.

Using QuickAdd

  • Open Settings > QuickAdd
  • Close to Add choice button, select Macro, add a name like “gitDouble”, and hit the Add choice button
  • Click the lightning icon
  • Click the gear icon, and then the plus icon to create a new macro (with the same (or another name)). You should now get an image resembling the following:

  • Find the commands you want to execute in the Obsidian command line, and hit the Add button. I’ve done that for two “random” git commands of opening the history and the diff view
  • Close off the dialogs
  • Go to Settings > Hotkey, search for your name, and assign a hotkey to it

Using Templater

If you instead of relying on the “internal” git plugin, and rather would want to use external/shell commands, instead of writing a javascript variant to execute those in child processes, I would rather use the User system command functions found at the bottom of Settings > Templater, and define the commands I would like to do in sequence there.

This would allow for a slightly better handling of the commands, with some error handling and encapsulation provided by the plugin so that your commands should be safer to run from within Obsidian.

Using javascript

If neither of the above are appropriate for you, and you would need/want to use javascript the first choice would be related to how to execute that javascript. Just a few options for that could be:

  • Either of the dedicated javascript plugins like RunJS or CustomJS or JS Engine or Javascript Init or … Plenty of options exists
  • Reuse one of your already installed plugins, like Templater, QuickAdd or User Plugins (which I’ve got installed due to other personal preferences which that plugin helps me with)

Further since we’re talking about Obsidian commands, I would use app.commands.executeCommandById, after locating the command id:

And the resulting javascript snippet would most likely look something like the following untested code:


await app.commands.executeCommandById("obsidian-git:open-history-view")
await app.commands.executeCommandById("obsidian-git:open-diff-view")

Or something similar… :slight_smile:


I’m not going to do the full javascript explanation for that last one, as I consider the other options as better suited in most cases, but this post should now show some of the vaste opportunities you’ve got for doing such a task as executing two Obsidian commands “at once”. Hope either of these options suits your needs, or gives you enough information to move forward.

1 Like

Yes, that you can do alright and then you don’t need to use the dreaded child process and the Commander plugin.

But now this way one needs to remember to fire the Templater command while the built-in plugin (yes, the plugin’s name is now solely ‘Git’) can be configured to do its job as per a timer built in so it can push (sync to the remote) automatically. Which I don’t like to do (my preference, of course) because I don’t like to clutter my git history with commits made every 5-10 mins, but that’s just me.

My comments about how I’d like to hear how power users (with upwards of 10k notes) using this plugin fair on desktops (we know of limitations on mobile) I’d also like to keep to myself now because 1) I’m still trying to plead my case with using GH Desktop 2) there’s been enough (including a flagged post of mine – “what could it have contained?”) on this thread to keep others away.

Wow, I need to digest it

A great answer, I learn a lot, Thanks

1 Like

:eyes:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.