一个脚本 push 代码到代码库

Author Avatar
Teeeemoji 12月 08, 2017
  • 在其它设备中阅读本文章

背景

代码 push 到 BD 的 XCode 的时候, (不能使用 sourceTree => 可以使用, 但是配置不可保存, 就是说每次使用都要重新设置, 更费心费力)

需要在 shell 中输入这么一串东西: git push origin branch_name:refs/for/branch_namme

要打的字就变多了, push 代码的动作变得不流畅, 没有幸福感

优化步骤

v2

更新 v2 版本, 添加更简单地功能, 运行脚本自动提交当前分支代码


#! /bin/bash

# 使用方法: npm run push[ branch_name]
# branch_name 存在: push branch_name to icode
#             不存在: push current branch_name to icode

if [ -n "$1" ];then
curBranch=$1
echo "pushing branch is ${curBranch}"
else
curBranch=$(git branch | awk '/\*/ { print $2; }')
# 另一种获取当前 branch_name 的方法
# git symbolic-ref --short HEAD
echo "current branch is ${curBranch}"
fi

git push origin ${curBranch}:refs/for/${curBranch}

两种获取当前分支名的资料来源


$ git branch | awk '/\*/ { print $2; }'
$ git rev-parse --abbrev-ref HEAD

https://stackoverflow.com/questions/1417957/show-just-the-current-branch-in-git

v1

  1. 编写自动化 shell 脚本 push.sh , 可以放在本地, 也可以放在项目中

# 使用方法: ./path_to_shell/push.sh branch_name

git push $1:refs/for/$1

注意: 使用 touch 生成的 push.sh 可能不具备执行权限

  • 可以使用 chmod 777 push.sh改一下权限

  • 也可以使用 sh ./path_to_shell/push.sh branch 来执行 shell 脚本

  1. 在 package 中增加 script

这样, 执行 npm run push branch_name就能执行 push 到对应 branch_name 的动作


"push": "./path_to_shell/push.sh",
  1. 在 bash_profile 中增加一个 alias

alias nrp="npm run push"

这样提交代码的时候, 要打的字就变成 “nrp branch_name”

最后

感觉舒服多了

This blog is under a CC BY-NC-SA 3.0 Unported License
本文链接:http://teeeemoji.xyz/2017/12/08/one-script-push-to-icode/