Why the Double Dash in Git Checkout?

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

You may be wondering why there is a double dash in the use of the git checkout command as in …

git checkout — index.html

when you want to revert back a change to index.html file in your working directory to the copy that is in the repository.

The reason is that git checkout command is used in two context.  The first is to revert changes as in above.  But git checkout is also used to switch to a branch.   To avoid ambiguity, it is best practice to put in the double dash to indicate that we are not switching branch and that we want to stay on current branch.  The double dash is used when we use git checkout to revert or discard changes.

While …

git checkout index.html

without the double dash will work in our case to revert back the index.html file, it is conceivable that there can be ambiguous cases such as …

git checkout about

Here it could mean that we want to revert back the “about” directory.  Or it could mean we want to switch to the “about” branch.

By using the double dash like this …

git checkout — about

then there is no doubt that we want to revert back the “about” directory and not switch branch.

Just think double dash as staying on same branch.