A. Matías Quezada
A. Matías Quezada

Reputation: 1906

git checkout overload

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?

Upvotes: 0

Views: 417

Answers (3)

jjlin
jjlin

Reputation: 4703

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.

Upvotes: 2

antlersoft
antlersoft

Reputation: 14786

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

git checkout <branch> -- <paths..>

Upvotes: 3

twalberg
twalberg

Reputation: 62369

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

Upvotes: 7

Related Questions