LoginSignup
1
1

More than 5 years have passed since last update.

ボクの考えた git pull より強いコマンド

Last updated at Posted at 2018-02-06

ボクは git pull は使いにくいと思います。なぜなら、

「ローカルとリモートに両方変更があった時、勝手に(自動的に) merge や rebase をしてしまう」
「自動的な merge や rebase は時々失敗する」
「というより、ローカルとリモートに両方変更があるのは、大抵作業ミスなので1、merge も rebase もするべきではない」

からです。なので git pull より使いやすいコマンドを作りました。

代替案

ボクが本当に必要なのは以下のコマンドだと思いました。

$ git fetch && git reset --hard origin/master

でも、このままだとローカルブランチにリモートには無い変更があった時に、問答無用で上書きされてしまいます。「変更があります」と表示して中止してほしいです。

git-latest

使い方:

$ git checkout master  # 更新したいブランチを checkout
$ git latest  # origin/master と同期される

ソースは以下の通り。このソースを git-latest という名前でPATHの通った場所においてください。

#!/bin/bash
set -euC

usage_and_abort() {
  echo 'usage: git latest [-f]' >&2
  exit 1
}

if [ "$#" -gt 1 ]; then
  usage_and_abort
fi

force=false
if [ "$#" -eq 1 ]; then
  if [ "$1" = "-f" ]; then
    force=true
  else
    usage_and_abort
  fi
fi

git fetch origin

r="$(git rev-parse --abbrev-ref --symbolic-full-name '@{upstream}')"

if [ -n "$(git log -n 1 --oneline "$r"..)" ] && [ "$force" != "true" ]; then
  echo 'There are commits which have not been pushed.' >&2
  echo 'Please run "git latest -f" when you ignore these commits.' >&2
  exit 1
fi

git reset --hard "$r"

  1. ボクのチームの開発環境においては。 

1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1