push --force-with-lease by default

后端 未结 6 504
[愿得一人]
[愿得一人] 2020-11-29 17:27

I just learned about git push --force-with-lease. It\'s pretty awesome. But, of course, I don\'t use force that often, and so I\'m worried that I might forget a

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 18:19

    My solution was to create a wrapper script, and use an alias so that I always use it in place of the real git.

    Whenever I try to git push -f, I see the following:

    ⚡ git push -f
    use this instead so you don't cause race conditions in the 
    repo: git push --force-with-lease
    

    Some advantages of this script are:

    • it trains me to habitually use --force-with-lease, so i don't get nagged when i get it wrong
    • if, for some reason, we really do need to force push, git push --force will work.

    How to implement it:

    1. create a custom script that will pass through any params to git, except for -f
    2. alias that script so we use it instead of git

    These instructions assume Linux or Mac, running bash. I haven't tried this with zsh or Windows, but I assume it'll work there too.

    ~/.bash_profile:

    alias git=~/.git_wrapper.sh
    

    ~./git_wrapper.sh:

    #!/bin/bash
    for arg in "$@"; do
        if [ "$arg" = "push" ]; then
            ispush=1
        elif [ "$ispush" = 1 -a "$arg" = '-f' ]; then
            echo "use this instead so you don't cause race conflicts in the repo: git push --force-with-lease"
            exit 1
        fi
    done
    
    git "$@"
    

    With those changes, restart your terminal and git should now get uppity when you try to force push.

提交回复
热议问题