Making commits simpler in Git using PowerShell

二次信任 提交于 2020-01-14 11:53:28

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/41086814/making-commits-simpler-in-git-using-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!