Getting started with Git? 10 commands that would save you…
Tags: git, gitcommands
Published: 2017-11-22 16:25:38

---
title: Getting started with Git? 10 commands that would save you…
published: true
description: Getting started with git commands
tags: #git #gitcommands 
---


Pushing, Pulling, fixing Merge issues among many others, used to be insurmountable problems for me because I was not familiar with git. I was always scared that I would ruin something along the way, and cause my code to break or just disappear completely. It took a while getting to understand how these processes work. After getting a grasp of some of it, I thought I should share the knowledge.  

If you do not already know why Git is important, know this: 


> Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
> ~ https://git-scm.com

I’d talk about 10 Git commands that would help you work effectively.

<h4><b>1. git init</b></h4>
 
This is basically the starting point for you. You would need to choose a location on your system which would be used as your local Git repository. The 'init' command would create a new git repository for you. If you already have an existing project, 'init' would create a git repository in your working directory. On your terminal, cd into the directory path where your project is, and type git init.

```
  $ cd var/www/devProj
  $ git init

```

<h4><b>2. git clone</b></h4>

   If you are working in a team, and you need to work on an already existing repository, you might need to clone it so as to have a version on your local. Git clone simply replicates the content of an existing repository, and reproduces it in a new directory. On your terminal, cd into the the directory path where you would want the project to be, and clone it there.

```  
  $ cd var/www/
  $ git clone path/to/the/repo

```

<h4><b>3. git add </b></h4>
   If you have been working on a branch, you would need to add the files you have been working on to your index before you can stage for a commit. You can add a specific file using `git add` or you can add all your new and modified files at once using `git add .` You can git add several times before committing, as long as new changes are being made. 
  
 ```
    $ git add /name/of/file
    $ git add . //new and modified files

 ```


<h4><b>4. git commit </b></h4>
   After you have added all the changes, you can stage all of them for a commit. This updates the Head but not the remote repository. Every time you use `git commit`,what you are doing is to collate all the file changes you have earlier recorded using `git add .`,and making it into an object. 
  
 ```
    $ git commit -m "write-a-commit-message"

      //You can actually add and commit at once using -

    $ git commit -am "write-a-commit-message"
        
```

<h4><b>5. git branch </b></h4>
   Git branch shows a list of all the branches available in a repository. It also shows what branch you are presently on. A branch is a portion of a project that is independent of all others.Your first commit is usually made to Git’s default branch,`master`. If there is a need to create a new branch, you can use git branch new-branch. This new branch would be independent of all other existing branches until it is merged into another branch. 

  ```
      $ git branch branch-name
      $ git branch -D branch-name //This deletes the created branch

  ```


<h4><b>6. git checkout </b></h4>
   Git checkout is necessary when you want to leave a branch. Recall that git branch only creates a new branch, however, git checkout moves you to an already existing branch.

  ```
      $ git checkout branch-name //This switches you to another branch
      $ git checkout -b new-branch-name //This creates a branch and checks out to 
         it also.
  ```

<h4><b>7. git status </b></h4>
   This command tells you what branch you are currently on. It identifies all the changes that have been made, and all the changes that have not yet been added and committed to that branch.

  ```
      $ git status

  ```

<h4><b>8. git log </b></h4>
   It lists all the previous commits made on that branch, the author, the time and the date of commit. This helps you keep track your activities.
   
  ```
      $ git log
    
  ```

<h4><b>9. git push </b></h4>
   If there are several people working on a project,the several others would definitely need to work with the new changes you have just made on a branch. For them to access your changes, you would need to push your commits to the remote repository.This way, the remote version would be updated, and can be easily accessed by others.

  ```
       $ git push origin master 

  ```

<h4><b>10. git pull </b></h4>
As a developer, you might need to pull from a couple of repositories.Pulling takes files from a remote source and merges them to your local files. That way, you would have access to the same file contents as the remote repository. 

  ```
       $ git pull origin master

  ```
There are a lot of other git commands, and usually, you would find them out when you exactly need them. Nevertheless, the above listed git commands would definitely be of help to you especially if you are just getting acquainted with git.

No one knows it all, so, if there are other commands you would like to share, please don’t hesitate to drop them as comments.
If you like the article, please like and share.