4

I am looking for a single command where I can add commit & push using new Powershell for Windows.
Single command like lazygit my commit msg // I'd prefer w/o quotes if possible

I looked over few questions on SO like git add, commit and push commands in one? which provided solution but for bash:

function lazygit() {
    git add .
    git commit -a -m "$*"
    git push
} 
//use it like lazygit my commit msg

Another answer suggested a git alias: git config --global alias.lazy '!f() { git add -A && git commit -m "$@" && git push; }; f'

but i have to add quotes and can't use spaces in commit message (gives error error: pathspec 'commit message' did not match any file(s) known to git)


ofcourse, there's one solution to write multiple commands in one line using ; but I'm hoping for simple one word command

Community
  • 1
  • 1
GorvGoyl
  • 42,508
  • 29
  • 229
  • 225
  • use `"$*"` to quote the entire args list as a single word, the `"$@"` in the alias is quoting each remaining argument individually. …`'!f() { git add -A && git commit -m "$*" && git push; }; f'`. – jthill Nov 06 '19 at 14:34
  • Does replacing `"$*"` with `$args` in the function not work? – Bill_Stewart Nov 06 '19 at 14:54
  • How do you feel about writing a PowerShell script to encapsulate it into a single command? – Joshua Drake Nov 06 '19 at 15:29
  • @JoshuaDrake yes please if you can provide working snippet, i'm not comfortable writing shell scripts – GorvGoyl Nov 06 '19 at 15:40
  • @jthill didn't work,gives error when providing more than single word: pathspec 'second argument' did not match any file(s) known to git – GorvGoyl Nov 06 '19 at 15:41
  • @Bill_Stewart no, error: switch `m' requires a value – GorvGoyl Nov 06 '19 at 15:43
  • @JerryGoyal Remember that we can't see your screen. Please try defining a function like above (are you familiar with how to define a function in PowerShell?) and replace `"$*"` with `$args`. Run the command with the arguments you need. If it outputs an error, paste the exact error output into your question. – Bill_Stewart Nov 06 '19 at 15:51
  • @Bill_Stewart I thought you mentioned to replace the variable in git config --global alias, I'll run the powershell function now – GorvGoyl Nov 06 '19 at 16:34
  • @JerryGoyal please see my answer to your question. – Bill_Stewart Nov 06 '19 at 16:35

1 Answers1

6

Does this function work for you?

function lazygit {
  param(
    [Parameter(ValueFromRemainingArguments = $true)]
    [String[]] $message
  )
  git add .
  git commit -a -m "$message"
  git push
}

Running this:

lazygit my commit message

Will run these three commands:

git add .
git commit -a -m "my commit message"
git push
Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62
  • I didn't know that i can just paste the function in powershell to make it run, it worked but for the same session window. I think some extra work is required for making it as a global function. – GorvGoyl Nov 06 '19 at 16:36
  • Add the function to your PowerShell profile script and it will be available in all PowerShell sessions (with the exception of PowerShell sessions started with `-NoProfile`). See `help about_Profiles` for information about profile scripts. – Bill_Stewart Nov 06 '19 at 16:37
  • 1
    Yup, that worked. For future readers: I opened pshell as admin, created & opened profile by executing `notepad $PROFILE.CurrentUserAllHosts` and wrote the function there. – GorvGoyl Nov 06 '19 at 17:07