DEV Community


Posted on

New to Git?

This post aims to give a basic overview of the Git version control system and explain some basic commands to help manage Git repositories.

What is a Version Control System?

A Version Control System is a system that keeps track of changes made to files over time, so that we can revise different file versions anytime.

Why do we need to use a VCS?

Have you ever found yourself in a position where you saved different versions of a document like this?
“thesis-first-draft.doc”, “thesis-DraftwithFigures.doc”, “thesis-final.doc”, “thesis-final2.doc”, “Master_Thesis_Final.doc”.
You then probably tried to track some changes but couldn’t remember where you made them, tried to spot differences between the different versions (that were probably unrelated to the title you gave to your file), and ended up feeling a little bit like this:

Using a VCS can take all the frustration away, since it allows you to recall specific versions of your files. It also allows you to compare versions, revert files back to a previous version, check who edited specific parts, and more.

Git as a VCS

Git is a free and open-source distributed VCS. Git thinks of data like a series of snapshots. With Git, your files can be in one of the following three states:

  1. Modified: the state of a file that has been changed but has not yet been committed to your database.
  2. Staged: the state of a modified file that has been marked to proceed to the next commit snapshot.
  3. Committed: the state of a file that has been safely stored to your local database.

Every time a commit is made, Git will take a snapshot of what all the files look like at that moment and will store a reference to that snapshot.
If you choose Git as your VCS, here are some basic Git commands you will be using a lot:

$ git init
Will initialise a Git repository in your working directory.
$ git add <file>
Will make your file staged. Your file will be added to the staging area.
$ git status
Will tell you the states in which your files are.
$ git diff
Will tell you which files have been changed but not yet staged.
$ git diff --cached
Will tell you which files have been staged so far.
$ git commit -m "commit message"
Will commit all staged files. Git will save a snapshot of what files in your staging area look like at this moment.
$ rm <file>
Will remove a file from the staging area and then commit. This will also remove the file from your working directory.
$ git log
Will tell you the commit history of the Git repository in reverse chronological order.

Remote Git repositories

Working on a remote repository allows collaboration in a Git project. A remote repository contains versions of your files hosted on the Internet. A common Git repository hosting service is GitHub.
Here are the most commonly used commands to help manage a remote repository:

$ git clone <url>
Will clone an existing Git repository in your working directory.
$ git remote
Will tell you which remote servers you have configured.
$ git remote add <shortname> <url>
Will add a new remote Git repository named after your specified shortname to the host.
$ git fetch <remote>
Will fetch changes made to the remote repository to your local repository.
$ git push <remote> <branch>
Will push the remote upstream. Before running this command, you will have to fetch any changes pushed by someone else.
$ git branch <branchname>
Will create a new branch, which is a pointer to the same series of commits you are on at the moment. This enables to work outside of the main line of development.
$ git checkout <branchname>
Will switch to the new branch.

If you are new to Git, I hope this post helped you understand some basic concepts around it. Using a DVCS as powerful as Git has a lot more to offer than what was covered in this post, so it is worth investing some time learning more about Git here.

Top comments (0)