DEV Community

Muhammad Umar Khan
Muhammad Umar Khan

Posted on

Power of - (hyphen) and git

The use of - with some git commands can really make our life easy. I am listing down some of the commands with which you can use - and make your life a lot easy.

1. Fetch something from another branch of git

Let's say you have two different git branches in your working directory.

  • master
  • develop

You have a file/folder named extras in master branch. Now, you want to have that same file/folder in develop branch as well. There are two different trivial solutions of doing it.

  1. You can merge master into develop
  2. Or you can copy paste that file/folder

But we are very lazy we don't want to merge or copy-paste either.
We have a very useful command for that
Before run this command you should be on the destination branch

git checkout master -- extras
Enter fullscreen mode Exit fullscreen mode

2. Checkout to the previous branch

Let's say your have two branches named:

  • abc/add-feature1-to-add-accessibility
  • abc/add-feature2-to-add-scalibility

Let's say, currently, you're on feature1 branch. Then you switch to feature2 branch using git checkout <BRANCH_NAME> command.

Now you want to go back to feature1 branch. Well what you will do is use the same checkout command we used earlier with the feature1 branch name.

But WAIT!!!

There's another way of doing it. Just type the checkout command with - instead of branch name.

git checkout -
Enter fullscreen mode Exit fullscreen mode

And here we go we will simply checkout to the previous branch where we were before checking out to the current branch.

Top comments (0)