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.
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.
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.
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 .
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.
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
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With