Git — commands you need to git going!
Tags: github, git, versioncontrol
Published: 2018-01-23 16:15:14

---
title: "Git — commands you need to git going!"
published: true
tags: github,git,version-control
canonical_url: https://medium.com/@davedodea/git-commands-you-need-to-git-going-b98adc42182e
cover_image: https://image.ibb.co/e34LYb/git_Article.png
---

First off, let’s ensure git is installed — if not, check the relevant link below for your&nbsp;system:

- MacOS [here](https://git-scm.com/download/mac)
- Windows [here](http://gitforwindows.org/)
- Linux here&nbsp;[here](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)

For the purposes of this post, I will be following a flow that you would most likely use for a _new_ repository, although some steps will be used for existing&nbsp;ones.

Also, for other commands — for example when making a new directory, I will be using BASH shell commands. Here is a [handy reference for&nbsp;those](https://courses.cs.washington.edu/courses/cse390a/14au/bash.html).

#### Create a new repository:

First of let’s make a new directory in which to keep our repository:

```
mkdir newRepo
```

Now we'll move into that new directory:

```
cd newRepo
```

We create this as a new git repository by&nbsp;issuing:

```
git init
```

#### To clone an existing repository:

#### From a server i.e. GitHub:

```
git clone https://github.com/path/to/repository.git
```

For example, to clone a bootstrap starter template from&nbsp;[here](https://github.com/BlackrockDigital/startbootstrap-creative):

```
git clone https://github.com/BlackrockDigital/startbootstrap-creative.git
```

You will look for the green button saying “Clone or download”:

![](https://cdn-images-1.medium.com/proxy/1*bb6So7Y0zC5tcqyZm3LZQg.png)

Or&nbsp;…

#### From an existing repository on your local&nbsp;machine:

```
git clone /path/to/repository
```

#### Adding a file to&nbsp;git:

Now that we have a repository set up, we need to add some files for git to&nbsp;track:

add all new and modified/edited files in the directory:

```
git add .
```

Now, we asked git to add files, we then need to _commit_ those&nbsp;files:

```
git commit -m "add these changes" 
```

In the above command we are saying a couple of&nbsp;things:

```
git commit ... //commit the added files.
```

``` 
... -m "add these changes" //-m will precede a message which will let people know what changes are included in this commit.
```

#### Pushing our commits/changes to the remote repository:

Once we have _added_ then _commited_ our files&nbsp;, we now need send or _push_ those changes to our repository:

```
git push origin master
```

In the above command are saying the following:

```
git push ... //send our changes.
```

```
... origin ... //the location of our repository.
```

```
... master //the _branch_ name, we will talk more about branches shortly.
```

#### Branches:

Let’s say that you are building an app or project which may have several different features. Good practice is to split the app development into the separate features.

Let’s imagine we are building an app and one of the features we need to code for may be the login system:

With _git_ we can make a _new branch_ called _login_:

```
git checkout -b login
```

In the above command:

```
git checkout //switch to the following branch name.
```
```
... -b login //-b will precede the new branch name.
```


The above command is actually a condensed one in the sense that it is actually _checking out_ and _creating a new_ branch in one&nbsp;pass.

If we already had a branch created called _login_ we could switch to it simply by:  

```
git checkout login
```

#### Pushing a branch:

What we did above was simply make a _local_ branch. In order for it to be accessible to others, we will need to push it to our remote repository:

```
git push origin login
```

#### Pulling:

Let’s say we need to retrieve all the latest updates to our remote repository — for example if you and your team are working on the same project from different machines, another team member may have made some changes and you want to make sure you are up-to-date:

```
git pull // tells git to retrieve the latest version.
```

#### Merging:

If we want to _merge_ or combine our commits made in our _login_ branch we made earlier into the _master_ working branch:

Let’s first _checkout_ our _master_ branch:

```
git checkout master
```

Then we can merge the new branch with:

```
git merge login
```

After issuing a merge command, the git system will merge the branches, but this is not always possible for git to achieve _automatically_ .

If you need to resolve these conflicts yourself, there is an easy-to-follow guide [here](https://githowto.com/resolving_conflicts).

#### Undoing changes:

You will most certainly face an occasion where you need to undo changes to the local repository — for example you want to remove changes you made to a file called _index.html_ :

```
git checkout -- index.html
```
  
If you would like to remove _all_ changes and revert back to the latest remote version:  
```
git fetch origin //get the latest remote version.
```

```
git reset --hard origin/master //remove all local changes.
```

#### Helpful links:

These are some worthy resources when you are comfortable exploring _further features_ (there are many) of git:

- [http://rogerdudler.github.io/git-guide/](http://rogerdudler.github.io/git-guide/)
- [https://git-scm.com/documentation](https://git-scm.com/documentation)
- [https://guides.github.com/introduction/git-handbook/](https://guides.github.com/introduction/git-handbook/)

I will do further posts on more advanced features, but for now, that’s all folks …

If you have any questions or additions to make, please let me know in the comments below or get in touch on [Twitter](https://twitter.com/DaveDODea).

~ Dave.