Git commands with examples

There are plenty of websites about Git and its commands on the internet. This one will be plenty plus one. If you’ve read my previous post you probably know that I’ve switched from Git Flow to GitHub Flow lately. During that switch, I had to create and delete some branches using console commands in Git Bash. They were very simple commands which can be very quickly found on the internet. But I decided to write this post to have this commands archived for me in one place and maybe it would be helpful for someone to see live examples of these commands usage.

Latest versions of Visual Studio and continues integrations tools are quite well integrated with Git, so sometimes I haven’t been opening Git Bash for weeks. But at the moment I’m using it more often. Firstly, because integration of Team Services and GitHub isn’t finished yet and I don’t use any other continues integration tool. Secondly, I simply want to be more familiar with the console because there are still things you can’t do using GUI tools. Git Bush is simply more powerful.

As a quick reminder, when I started working on the GoogleTasksManager project I was implementing GitFlow as a branching strategy model but later I switched to GitHub Flow which is much simpler and more convenient for me. So firstly I had created develop branch according to Git Flow rules and later I had removed it because it wasn’t longer needed after I switched to GitHub Flow strategy. Below are commands I used then.

  • Creating a new branch and switching to it: git checkout -b <branchname>
    (the command will create a new branch with current uncommitted changes)
  • New branch was created locally and then I had to push it to the remote repository:
    git push origin <branchname>
  • After I started following GitHub Flow rules, develop branch was no longer needed, so I deleted it. But before you are able to delete the branch you have to switch to another one.
    Switching from one branch to another: git checkout <branchname>
  • Deleting branch: git branch -d <branchname>
  • Viewing all existing branches: git branch -a
  • In the example above the develop branch is still on the list of branches. So let’s remove local reference to deleted branch:
    git fetch -p

    And finally, after the fetch the develop branch isn’t listed any longer.

Sources:
Basic Git commands
stackoverflow
How To Use Git Branches
stackoverflow

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

*