1

I find that every time I checkout a local branch, I do a git status. I want to add a git status to my alias for checkout to be more efficient.

I already have the following simple alias for checkout:

alias.co=checkout

I'd like to modify it so that no matter what arguments I provide to 'git co', it will always perform:

git co && git st

So for example, I could any of the following, and the alias should perform a git status afterwards:

git co -b newbranch
git co anotherbranch
git co -b andanother --track newbranch
git co -- "*.c"
well actually
  • 11,810
  • 19
  • 52
  • 70

2 Answers2

4

To run multiple Git commands in an alias you'll need to modify your alias to use !, which runs a shell command, e.g.:

[alias]
    co = "!git checkout \"$@\" && git status"

The $@ should propagate any arguments to git co through to git checkout.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
1

You're probably better off writing a bash script for something like this. Something like:

#!/bin/bash

if [[ $# == 0]]
then
    echo 'No branch name'
    exit 1
fi
git checkout "$*"
git status

Then whatever you save that file as will be the commands name, and then the first command would be the name of the branch.

Peter Foti
  • 5,526
  • 6
  • 34
  • 47