0

TL;DR I can't revert a file with git checkout if a branch has the same name.


I was working on my GIT repository when found a funny problem. We've a branch called "build-upload" where we are creating a new "upload" feature. We also have a file called "bin/build-upload", it's a script than builds the project and uploads it to production.

The thing is I was on the "bin/" directory and modified "build-upload" file and I want to revert it. So I typed

git checkout build-upload

And the result was

amatiasq:~/repo/bin$ git checkout build-upload
M   bin/build-upload
Switched to branch 'build-upload'

I didn't pay enought attention to the result, and continued working without realizing I was on another branch. Fortunately before I commit the new changes I saw "bin/build-upload" was modified and this led me to found I switched branch.

The question is. Is there a way to prevent this ambiguity? How can I tell git when I do "checkout" if I want to switch branch or revert a file?

A. Matías Quezada
  • 1,886
  • 17
  • 34

3 Answers3

7

According to git help checkout, everything after a -- on the command line will be interpreted as a path, not a branch or tag. So, this should probably do what you need:

git checkout -- build-upload
twalberg
  • 59,951
  • 11
  • 89
  • 84
3

You have to put a '--' before the path part to eliminate ambiguity with branch names.

git checkout <branch> -- <paths..>
antlersoft
  • 14,636
  • 4
  • 35
  • 55
2

You could do git checkout ./build-upload to ensure that build-upload is treated as a filename. This is analogous to classic Unix tricks like rm ./-r to disambiguate a file named -r from the option -r.

jjlin
  • 4,462
  • 1
  • 30
  • 23