有什么办法可以在终端上自动键入一些文本?

时间:2019-03-19 11:03:32

标签: bash git terminal

当我通过终端使用git时,我仅打开专用于git命令的选项卡,因此我不想每次都键入“ git”。有什么方法可以使每行中的某些文本自动输入吗?

2 个答案:

答案 0 :(得分:1)

您可以将每个git命令定义为一个别名,例如,键入diff mybranch将调用git diff mybranch。要调用普通的shell命令,请在其前面键入反斜杠,例如\diff file ../elsewhere/file调用/usr/bin/diff而不是git diff

将以下代码放入文件~/.git.bashrc中。配置您的git终端以运行bash --rcfile ~/.git.bashrc而不是运行bash

. ~/.bashrc
for c in $(COLUMNS=4 git help -a | sed -n 's/^  \([a-z]\)/\1/p';
           git config --get-regexp '^alias.' | sed 's/alias\.//; s/ .*//')
do
  alias "$c=git $c"
  complete -F _complete_alias foo
done

complete行需要使用_complete_alias function

答案 1 :(得分:-1)

I created this .bashrc function that pushes the code and tags it. All you need to give it is the comment you want for the push.

The alias of the function is "gp" (which stands for git push).

So if you want to push and tag some code all you need after you add this code to your .bashrc is:

$ gp "test my new git push function"

gpfunction() {
    git status
    echo [Enter to continue...]
    read a
    git pull
    git commit -am"$1"
    git push
    tag_major_min=$(git tag |sort -V|tail -1|awk -F. '{print $1 "." $2 "."}')
    echo Tag major min $tag_major_min
    latest_tag_number=$(git tag |sort -V|tail -1|awk -F. '{print $3}')
    echo Latest tag number $latest_tag_number
    next=$(echo $latest_tag_number + 1 | bc)
    echo Next $next
    new_tag=$(echo $tag_major_min $next | sed 's/ //g')
    echo New tag $new_tag
    git tag $new_tag
    git push origin $new_tag
}
alias gp=gpfunction

This script uses a major.minor.patch version standard and increments the patch version.

You can tweak it as you please.