Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to do "git checkout" and *force* the argument to be interpreted as a branch name?

You type the command "git checkout foo".

If there is a branch called "foo" yet no file by that name, it switches to the branch -- and if there is a file by that name and no such branch, it updates the file "foo".

But I wonder --- is there a way to enter the command so that Git will unconditionally interpret "foo" as the name of a branch no matter what? This includes (but is not limited to) the specification that if there is no branch called "foo", the operation will fail even if a file by that name exists.

So - is there any way to do this?

Thanks.

like image 286
Sophia_ES Avatar asked Nov 11 '14 01:11

Sophia_ES


People also ask

Can we force checkout in git?

Force a Checkout You can pass the -f or --force option with the git checkout command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs from HEAD ). Basically, it can be used to throw away local changes.

How do I force checkout to commit?

To pull up a list of your commits and their associated hashes, you can run the git log command. To checkout a previous commit, you will use the Git checkout command followed by the commit hash you retrieved from your Git log.

What is the right way to perform checkout in git?

In order to checkout a remote branch you have to first fetch the contents of the branch. In modern versions of Git, you can then checkout the remote branch like a local branch. Older versions of Git require the creation of a new branch based on the remote .

How do I override git checkout?

If you simply have a message like "The following untracked files would be overwritten..." and you want the remote/origin/upstream to overwrite those conflicting untracked files, then git checkout -f <branch> is the best option.


3 Answers

Yes, from the reference, there is this usage:

git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <paths>…

In your case, that boils down to:

git checkout foo --

Here, foo corresponds to <tree-ish> and -- says that anything after this is referring to file names. So, if you use the above command (with no file names after --) it will fail if there is no tree-ish (branch) named foo:

fatal: invalid reference: foo
like image 146
Yawar Avatar answered Oct 19 '22 13:10

Yawar


You can add an explicit check if the branch name exists first:

git show-ref --verify --quiet refs/heads/foo && git checkout foo

That will make sure the branch exists and if it does, check it out. Yes, there is a race condition (the branch could be deleted in between), but hopefully that's not a problem in practice.

Is there a better way to find out if a local git branch exists?

like image 34
John Zwinck Avatar answered Oct 19 '22 13:10

John Zwinck


Just checked the behaviour of git 2.1.2 and checkout always interprets the name as a branch name. You can force it to be a file instead using ./foo.

In your case git checkout foo will always try to switch to branch foo.

If you want to force the argument to be a branch, so that even if a file is matching it's not updated, you can try to trick git using git checkout -B foo foo. If foo exists, git will check it out and update the branch pointer to itself. If it doesn't, the new branch will not be created, because the start point is missing.

like image 24
viraptor Avatar answered Oct 19 '22 12:10

viraptor