使用 Git 时我离不开的两个 Bash 脚本

2025-05-27

使用 Git 时我离不开的两个 Bash 脚本

这么说可能有点夸张,但确实有点夸张。我用这两个简单的脚本已经五年多了,简直无法想象我的shell里没有它们会是什么样子。这到底是怎么回事?

安全清理未跟踪的文件

你的仓库里有一些未跟踪的文件,可能是一些构建产物或其他垃圾,但你不确定。你只是知道有东西,因为你在 ZSH 命令提示符中看到了一个小感叹号图标。你在做什么?

最有可能的是,你会首先git status看看那里到底有什么。

$ git status
?? tmp/
?? some_trash
Enter fullscreen mode Exit fullscreen mode

一切看起来都是不必要的,所以你决定将其删除。

$ git clean -f
Removing some_trash
Enter fullscreen mode Exit fullscreen mode

嗯,那怎么办tmp?它是一个目录!你没注意到/它名字旁边的小符号,所以你得用 switch 再重复一遍这个命令-d

$ git clean -f -d
Removing tmp/
Enter fullscreen mode Exit fullscreen mode

现在仓库已经清理干净了,但需要输入三个命令。如果你足够细心,其实只需要两个,但这仍然需要输入很多代码。我想你应该知道很多更好的方法来利用这五秒钟!

是的,所以我写了下面的脚本。为了让它在每个shell中都可用,只需将其放入~/.bashrc~/.zshrc类似的文件中即可。

function git_clean_untracked_safely {
  TO_REMOVE=`git clean -f -d -n`;
  if [[ -n "$TO_REMOVE" ]]; then
    echo "Cleaning...";
    printf "\n$TO_REMOVE\n\n";
    echo "Proceed?";

    select result in Yes No; do
      if [[ "$result" == "Yes" ]]; then
        echo "Cleaning in progress...";
        echo "";
        git clean -f -d;
        echo "";
        echo "All files and directories removed!";
      fi
      break;
    done;
  else
    echo "Everything is clean";
  fi;
}
Enter fullscreen mode Exit fullscreen mode

那么,它是如何工作的呢?

$ git_clean_untracked_safely
Cleaning...

Would remove some_trash
Would remove tmp/

Proceed?
1) Yes  2) No
?#
Enter fullscreen mode Exit fullscreen mode

您会看到哪些内容将被删除,如果您认为没有问题,您可以输入 继续1。或者使用 拒绝2

Proceed?
1) Yes  2) No
?# 1
Cleaning in progress...
Removing some_trash
Removing tmp/
Cleaning finished!
Enter fullscreen mode Exit fullscreen mode

编写函数名称会浪费按键,因此我建议创建一个别名,例如通过向~/.bash_aliases文件添加一行。

alias gcl='git_clean_untracked_safely'
# Use different name if you use GNU Common Lisp interpreter
Enter fullscreen mode Exit fullscreen mode

或者,如果您愿意,可以添加一个 git 别名。

$ git config --global alias.justclean '! bash -c "source ~/.bashrc && git_clean_untracked_safely"'
Enter fullscreen mode Exit fullscreen mode

这两个命令将以相同的方式调用脚本。

$ gcl
$ git justclean
Enter fullscreen mode Exit fullscreen mode

清理旧的本地分支

你是不是经常会在运行的时候git branch -vvv看到几十个你根本不记得的本地分支?我敢打赌至少发生过一次!遇到这种情况该怎么办?你可以先清理对已失效远程分支的引用,然后移除所有不包含上游的本地分支。但是,逐个选中分支并用(或)git remote prune origin移除听起来不太好玩。git branch -d-D

我们可以做得更好吗?猜猜看……一个脚本!

function git_clean_local_branches {
  OPTION="-d";
  if [[ "$1" == "-f" ]]; then
    echo "WARNING! Removing with force";
    OPTION="-D";
  fi;

  TO_REMOVE=`git branch -r | awk "{print \\$1}" | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk "{print \\$1}"`;
  if [[ -n "$TO_REMOVE" ]]; then
    echo "Removing branches...";
    echo "";
    printf "\n$TO_REMOVE\n\n";
    echo "Proceed?";

    select result in Yes No; do
      if [[ "$result" == "Yes" ]]; then
        echo "Removing in progress...";
        echo "$TO_REMOVE" | xargs git branch "$OPTION";
        if [[ "$?" -ne "0" ]]; then
          echo ""
          echo "Some branches was not removed, you have to do it manually!";
        else
          echo "All branches removed!";
        fi
      fi

      break;
    done;
  else
    echo "You have nothing to clean";
  fi
}
Enter fullscreen mode Exit fullscreen mode

用法如下:

$ git_clean_local_branches
Removing branches...

bugfix/foo
bugfix/bar
bugfix/baz
feature/qux
feature/tux
feature/fux

Proceed?
1) Yes  2) No
?#
Enter fullscreen mode Exit fullscreen mode

与上一个脚本一样,您可以继续或中止。默认情况下,分支会被优雅地移除,因此当分支未合并到主分支时(或者至少当 git 提示未合并时),命令可能会失败。

$ git_clean_local_branches
Proceed?
1) Yes  2) No
?# 1
Removing in progress...

Deleted branch bugfix/foo (was 7ff047995).
Deleted branch feature/qux (was cfad3e00c).
Deleted branch feature/tux (was 6529123af).
Deleted branch feature/fux (was b12ec9091).

error: The branch 'bugfix/bar' is not fully merged.
If you are sure you want to delete it, run 'git branch -D bugfix/bar'.
error: The branch 'bugfix/baz' is not fully merged.
If you are sure you want to delete it, run 'git branch -D bugfix/baz'.

Some branches were not removed, you have to do it manually!
Enter fullscreen mode Exit fullscreen mode

如果您无论如何都想修剪树枝,请使用-fswitch。

$ git_clean_local_branches -f
WARNING! Removing with force
Removing branches...

bugfix/bar
bugfix/baz
1) Yes  2) No
?# 1
Removing in progress...

Deleted branch bugfix/bar (was 7ff047995).
Deleted branch bugfix/baz (was cfad3e00c).

All branches removed!
Enter fullscreen mode Exit fullscreen mode

bash 和 git 别名也都可用。

alias glpo='git_clean_local_branches'
Enter fullscreen mode Exit fullscreen mode
$ git config --global alias.localprune '! bash -c "source ~/.bashrc && git_clean_local_branches"'
Enter fullscreen mode Exit fullscreen mode

就这样!尽情享用吧!


感谢@jsn1nj4@tgu@euphnutz的建议。

文章来源:https://dev.to/erykpiast/two-bash-scripts-i-cannot-live-without-when-working-with-git-44a1
PREV
我推荐一些书籍来提升你作为开发人员的软技能
NEXT
2020 年部署 Web 应用的 4 种方式