Guide To Using GIT On The Command Line - JonDJones Idiots Guide

This post is a quick and dirty guide of how to use GIT on a day to day basis.  Over the years I've been using source control and one easy and lazy path to go down is to use a tool point and click tool - like SourceTree or GitKraken - to get the work done for you.  

On simple projects, tools can make your life easier, however, the chances are highly likely that sooner or later you'll bump into a project where only the command line will cut it.  Maybe the repo uses sub-modules, maybe you need to run some pre-commit hooks beforehand and the tool doesn't like, maybe you always need to do a rebase instead of webhooks.  Whatever the reason for learning to use GIT via the command prompt will make you more productive and give you less source control conflicts.

Getting Started

Forcing yourself to use the command line, really is as simple as making the decision now not to use a tool.  At first, it will take you longer and you will find yourself frustrated when you can't remember a command, or you're not sure what you've done, but like learning a second language, if you're forced to use it as your only way of communicating you will learn it much quicker. One way to speed this process up is forcing yourself to try and figure out which command you need. If you use the '--help' flag against any command and figure out what you need to do yourself, you will retain that information more completely.

Quickstart GIT commands

Ok, so the first command is checking out/cloning a branch.  In your command prompt navigate to the area you want to pull to and use the 'clone' keyword:

$ git clone URL

Before you start work, it's probably easier to work in a new branch first.

$ git branch MyBranch
$ git checkout MyBranch

Next, you will need to add files and commit them

$ git add .
$ git commit -M 'Check-in'

If you bump into any issues, you can get yourself out of trouble with:

$ git reset ~HEAD

You can also try:

$ git reset --force

If you work in a big team, it is likely that you will need to create pull-requests to have people peer review your code. To push a commit, you want to first get all the latest changes from master.

$ git checkout MyBranch
$ git rebase master
$ git checkout master
$ git merge MyBranch --ff

You should also squash your commits. Let's say as you've been working, you have 6- commits with meaningless names like 'test, 'fix test'. Instead of checking this all in as you have a really ugly and confusing commit history, squashing your commits into a single commit will make your life a lot easier. You can do this by:

$ git checkout master;
$ git merge --squash MyBranch;
$ git commit -M 'my squashed commit message';

Now, you can create your pull-request and push it:

$ git push origin BRANCHNAME

Now, you can create your pull-request and push it:

$ git push origin BRANCHNAME

Lastly, if you have any issues or unsure about your current GIT status, you can use these commands:

$ git status
$ git log

submit to reddit

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top