Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a personal git function, usable every time I launch Bash?

I am developing a web site and I use git to update the site, and I have two branch so it goes:

  • git add .
  • git commit "something"
  • git push
  • git checkout "prod"
  • git merge --no-ff dev
  • git push
  • git checkout "dev"

I need a lazygit function which would looks like

function lazygit(){
    git add .
    git commit "$1"
    git push
    git checkout "prod"
    git merge --no-ff dev
    git push
    git checkout "dev"
}

And would be use like

lazygit( "CSS UPDATE" )

Now my question is, how can I save this function into file or whatsoever so I can use it anywhere ?

Thank's alot

like image 949
CookieThief Avatar asked Dec 23 '22 12:12

CookieThief


1 Answers

Another option is to define a Git alias instead of a shell function.

Aliases are part of configuration, so let's look at the gitconfig(7) manual page (run git help config locally):

alias.*

Command aliases for the git(1) command wrapper - e.g. after defining "alias.last = cat-file commit HEAD", the invocation "git last" is equivalent to "git cat-file commit HEAD". To avoid confusion and troubles with script usage, aliases that hide existing Git commands are ignored. Arguments are split by spaces, the usual shell quoting and escaping is supported. A quote pair or a backslash can be used to quote them.

If the alias expansion is prefixed with an exclamation point, it will be treated as a shell command. For example, defining "alias.new = !gitk --all --not ORIG_HEAD", the invocation "git new" is equivalent to running the shell command "gitk --all --not ORIG_HEAD". Note that shell commands will be executed from the top-level directory of a repository, which may not necessarily be the current directory. GIT_PREFIX is set as returned by running git rev-parse --show-prefix from the original current directory. See git-rev-parse(1).

So you can do

$ git config --add alias.whatever '!set -eu; git add . &&
    git commit "$1" &&
    git push &&
    git checkout prod &&
    git merge --no-ff dev &&
    git push &&
    git checkout dev'

and then just

$ git whatever "commit message"

The set -eu; would make the whole thing crash unless you submit the required parameter. Another approach would be to stick something like test $# -gt 0 || exit 1; there instead.

like image 178
kostix Avatar answered Dec 26 '22 09:12

kostix