profile

Néstor's Blog

Git Update Branch Alias

git cli productivity

Create a shell alias that does it all:

alias gub='git symbolic-ref --short HEAD > /tmp/current-branch && \
  git checkout $(git remote show origin | awk "/HEAD branch/ { print \$NF }") && \
  git pull && \
  git checkout $(cat /tmp/current-branch) && \
  git merge --no-edit $(git remote show origin | awk "/HEAD branch/ { print \$NF }") && \
  rm /tmp/current-branch'

How to Install

Add to your ~/.bashrc or ~/.zshrc:

echo "alias gub='git symbolic-ref --short HEAD > /tmp/current-branch && git checkout \$(git remote show origin | awk \"/HEAD branch/ { print \\\$NF }\") && git pull && git checkout \$(cat /tmp/current-branch) && git merge --no-edit \$(git remote show origin | awk \"/HEAD branch/ { print \\\$NF }\") && rm /tmp/current-branch'" >> ~/.bashrc

source ~/.bashrc

Usage

From any feature branch, simply run:

gub

Handle Merge Conflicts

If there are conflicts, the alias stops and you can resolve them manually:

# Fix conflicts in your editor
git add .
git commit

Alternative: Rebase

If you prefer rebasing:

alias gur='git symbolic-ref --short HEAD > /tmp/current-branch && \
  git checkout $(git remote show origin | awk "/HEAD branch/ { print \$NF }") && \
  git pull && \
  git checkout $(cat /tmp/current-branch) && \
  git rebase $(git remote show origin | awk "/HEAD branch/ { print \$NF }") && \
  rm /tmp/current-branch'