Git submit script, fast way to submit code


foreword

When using the tool, you need to manually type , , , and commands Gitevery time you submit the code . If you use the graphical interface, you need to manually click the corresponding button. So we are used to configuring some scripts to execute commands such as submitting and pulling for quick operations. In this article, we will deal with script files ( ending with ). Of course, this method was also learned from the boss when I first came out to work~~add commitpullpushmerge.sh


1. What is a .sh file?

  • Known as scripting Bash applications and using developer files.

2. How to use

First of all, the script configuration needs to know the specific branch that needs to be submitted, pulled, and pushed. There are three ways to get the current branch name

  • git branch | grep "*"
  • git symbolic-ref --short -q HEAD
  • git rev-parse --abbrev-ref HEAD

Maybe the terminal in vscode will prompt an error, just select the corresponding executable method
insert image description here

The specific configuration is as follows

We are generally used to creating new files in the project directory sh, and then creating add.sh, build.sh, pull.sh, merge.shand rebase.shfiles respectively. The specific contents are as follows:

submit script

  • add.sh– Script file executed when code is submitted
#!/usr/bin/env bash
echo "请输入提交的原因:"
read REASON

if [ "$REASON"x = ""x ]; then
echo "请输入提交原因:"
exit 0
fi

cd ../

branchName=`git rev-parse --abbrev-ref HEAD`
git add .
git commit -m "${REASON}"
git pull origin $branchName
git push origin $branchName

easy to use

First use the terminal to enter shthe directory and execute sh add.shthe command. Generally, we are used to input sh aand then press Tabthe key to find the correspondingadd.sh

EnterThen it will output the reason that needs to be submitted, just follow the actual submission content, and press the key after inputting

It can be seen that the command will first prompt the reason that needs to be submitted, that is, the code submission remarks. And automatically get the current git branch, then submit and push.

build script

  • build.shpackage.json– Name the build, and output prompts according to the actual configuration. Generally, there are sittest environment, uatbusiness acceptance environment and prodsurvival
#!/usr/bin/env bash
echo "请输入构建环境sit/uat/prod:"
read ENV

if [ "$ENV"x = ""x ]; then
echo "请输入构建环境sit/uat/prod:"
exit 0
fi

cd ../

npm run "${ENV}"

Execute sh build.sh, and then enter the corresponding environment that needs to be built

pull script

  • pull.shPull the latest code
#!/usr/bin/env bash

cd ../

branchName=`git rev-parse --abbrev-ref HEAD`
git pull origin $branchName

Execute sh pull.sh_

merge script

  • merge.shThis script is used when merging branches. Specifically, first confirm the current branch and then confirm the branch that needs to be merged
#!/usr/bin/env bash
cd ../
git branch

echo "请输入被合并的分支名称:"
read BRANCH

if [ "$BRANCH"x = ""x ]; then
echo '请输入被合并的分支名称:'
exit 0
fi
branchName=`git rev-parse --abbrev-ref HEAD`

if [ "$BRANCH"x != ""x ]; then
    echo "请确认当前分支是{${branchName}}分支,并确认当前分支需合并的分支名称为{${BRANCH}} ----  并确认在输入yes Or no?";
    read BOOL
    case "$BOOL" in
        "yes") echo "确认合并";
        git merge ${
    
    BRANCH};
        echo "合并成功";
        ;;
        "no") echo "取消合并";
        ;;
    esac
exit 0
fi

rebase commit script

  • rebase.shThis script is used when you need to use your own branch. It is generally used less frequently, and it is also a better way to record the current developer's code volume. I won't introduce it here. If you are interested, you can check relevant information by yourself. learn
#!/usr/bin/env bash

branchName=`git rev-parse --abbrev-ref HEAD`

cd ../

git fetch upstream
git rebase upstream/$branchName

Execute sh rebase.sh_

3. Summary

I personally feel that after using the above script configuration, you can quickly operate git-related operations without remembering the branch name (some project teams create new branch names that are difficult to remember, and the current command is very good to solve the case of wrong branches)

Guess you like

Origin blog.csdn.net/weiCong_Ling/article/details/131091041