gpt4 book ai didi

git - 如何创建个人 git 函数,每次启动 Bash 时都可用?

转载 作者:行者123 更新时间:2023-12-03 18:46:09 25 4
gpt4 key购买 nike

我正在开发一个网站,我使用 git 来更新该网站,
我有两个分支所以它去:

  • git 添加。
  • git commit “某事”
  • git push
  • git checkout "prod"
  • git merge --no-ff dev
  • git push
  • git checkout “开发”

  • 我需要一个看起来像的lazygit函数
    function lazygit(){
    git add .
    git commit "$1"
    git push
    git checkout "prod"
    git merge --no-ff dev
    git push
    git checkout "dev"
    }

    并且会像
    lazygit( "CSS UPDATE" )

    现在我的问题是,如何将此函数保存到文件或其他任何内容中,以便我可以在任何地方使用它?

    非常感谢

    最佳答案

    另一种选择是定义 Git 别名而不是 shell 函数。

    别名是配置的一部分,所以让我们看看 gitconfig(7)手册页(在本地运行 git help config):

    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).



    所以你可以做
    $ 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'

    然后只是
    $ git whatever "commit message"
    set -eu;除非您提交所需的参数,否则会使整个事情崩溃。
    另一种方法是坚持类似 test $# -gt 0 || exit 1; 的内容。那里代替。

    关于git - 如何创建个人 git 函数,每次启动 Bash 时都可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47172668/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com