6

I'm using Git and being an old dude, I prefer to punch my will on the keyboard rather than send a rodent on a clicky trip. So, usually when I finish off my work, I'll go something like this in the console.

git add .
git commit --message "Cure for cancer (or so it feels like)"
git push

I could have a static script doing that but, regrettably, not every day's contribution is as awesome as the cure for cancer, so I'd need to have a script that takes a parameter (and if none is provided, it could be substituted by e.g. "donkey".

How would I go about creating such script?

I've googlearched it a bit but got a lot of different suggestions and at my competence level with PowerShell, I have the fear that I'll screw something up really badly. Creating a batch file would be an option but now that I do the magic in PowerShell, I fell I ought to learn it a bit more. As long as we can keep the learning curve not very steep, hehe.

Suggestions?

5
  • create a function that has all the commands you want to execute. the function can have parameters that you can use to perform your logic. just make sure you are in a git shell when executing the function and you should be good to go. Commented Dec 11, 2016 at 13:50
  • @Nkosi I will not be in a Git shell. I'll be doing that from the plain PowerShell. However, I've configured the system already so that git is a known command. Commented Dec 11, 2016 at 14:20
  • 1
    OK cool. then just create your function. you could go as far as creating a module and have it loaded in your session so you can just call the the cmdlt Commented Dec 11, 2016 at 14:53
  • Perfect! Sounds easy enough. Is the suggestion with module anything like the answer below from @sodawillow by any chance? Commented Dec 11, 2016 at 15:03
  • Yes. module is just a collection of functions. that answer is basically what I was suggesting. Commented Dec 11, 2016 at 15:05

2 Answers 2

10

To extend Sergiu Vidrascu's answer, you can wrap the code in a function :

function Finish-WorkAndGoHome {
    param([string]$mes = "Curing cancer with every commit")

    git add .
    git commit -m $mes
    git push
}

and add it to your profile so that the function is available in every Powershell session you start.

In a PS console, run notepad $profile, paste the code in the notepad window and save the file, for instance (then use a new console window to load the new profile).

You can learn more about Powershell profiles here. Setting the scripting rights is described here.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm heading out to the movies right now but I'll make sure to try that in the evening. If I forget to accept it by Monday, please kick me to remind. :)
6

You can use simple parameters In PowerShell scripts.

Have a magic.ps1 file with something like this:

param ( [string]$mes = "Curing cancer with every commit")

git add .
git commit -m $mes
git push

When you run it without arguments it picks up the default message.

When you run it like

magic.ps1 -mes "did something here"

It will use your message.

4 Comments

And where/how do I save the PS1 file so that it's accessible from anywhere on my system?
Add it anywhere and append the parent folder path to the PATH environment variable
Putting a function in the user's profile is preferable to needlessly adding folders to the PATH.
True that, i agree, It is preferable, but since he is fairly new to powershell i wanted to show him something he would maybe relate much easyer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.