LoginSignup
1
2

More than 5 years have passed since last update.

サッと一時ブランチを作って作業して捨てる

Posted at

なにかちょこっとだけ作業するたびにトピックブランチをつくって終わったら消すということをしている。

  1. git branch <branch-name> [<start-point>]
  2. git checkout <branch-name>
  3. 作業
  4. add & commit & push
  5. git checkout <some-branch>
  6. git branch -D <branch-name>

めんどくさくなってきたので専用コマンドを作った。こういうものを書くのは初めてなのでこちらの記事のものを書き換えて作った。

.bashrc
tmpbranch() {
  (
  d=$(mktemp -d "${TMPDIR:-/tmp}/${1:-$2}.XXXXXXXXXX") && git worktree add $d $1 -b $2 && pushd "$d" || exit 1
  "$SHELL"
  s=$?
  if [[ $s == 0 ]]; then
    rm -rf "$d"
    popd
    git worktree prune
    git branch -D $2
  else
    echo "Directory '$d' still exists." >&2
  fi
  exit $s
  )
}

実行するとブランチが作成され、linked working treeが/tmpにできて、そこに移動する。exitするとディレクトリとブランチとworking treeが削除される。これによって作業は以下のようになった。

  1. tmpbranch <branch-name> <start-point>
  2. 作業
  3. add & commit & push
  4. exit

いい感じである。もしかしてこんなもの書かなくても別の便利な方法があったりするんだろうか?

1
2
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
2