1

In a script I want to check that there are no uncommited changes so that I can switch to a different branch (and then switch back). What is the proper way how to do such a check?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Petr
  • 62,528
  • 13
  • 153
  • 317
  • 1
    Have you tried `git status`? – Viacheslav Kondratiuk Nov 07 '13 at 07:31
  • @viakondratiuk I'm hoping for a git command whose exit status would show if there are any changes or not. I could use something like `git status | grep -q modified`, but I'm not sure if this is really safe even in corner cases (like if there is an untracked file named `modified` etc.). – Petr Nov 07 '13 at 07:40
  • @PetrPudlák Are you interested only in *modified* files, i.e. don't care about untracked files? – devnull Nov 07 '13 at 07:44
  • @devnull Yes, I'm primarily interested only in _modified_ files. – Petr Nov 07 '13 at 07:45
  • @PetrPudlák You could say `git status -s | grep -q "^ M" && echo Modified files exist!` – devnull Nov 07 '13 at 07:47
  • Do you need to check this explicitly? `git checkout` will fail if cannot switch branches because switching would cause an uncommitted change to be overridden. (You can still carry uncommitted changes to the new branch and back again.) – CB Bailey Nov 07 '13 at 07:48
  • 1
    @CharlesBailey don't want to carry changes and accidentally commit them there. – jthill Nov 07 '13 at 09:06

0 Answers0