Version control with Git

Definition

The condensed definition: Git is a free and open source distributed version control system.

What does it mean? Git keep track of changes made to a project over time. It is usually used for collaborative projects to allow teams to collaborate seamlessly. Git is largely used among programmers developing source code. Git is agnostic in the sense that it can handle any set of files (text, binary, etc.). For this reason, a writer could also benefit from using Git to manage his book revisions.

Setup

Install Git and Create a GitHub Account

Follow the instructions here to install git.

Create a GitHub account here.

There are many other alternatives to GitHub such as GitLab, BitBucket or Google Cloud Source Repositories.

Your First Git Project

  1. Open the terminal
  2. Go to the location where you want to store your project. Create a new directory.
cd <directory>
mkdir <project name>
cd <project name>
  1. Initialize a Git repository with a README file.
git init
touch README.md
  1. Stage the README file.
git add README.md
  1. Commit the changes.
git commit -m "Initial commit"
  1. Push the changes.
git push -u origin master

git init hello_world
cd hello_world
touch README.md
git add README.md
git commit -m "Initial commit"
git push -u origin master

Git Basics

git init: Transform the current directory into a Git repository.

git init <directory>: Initialize an empty Git repository in specified directory.

git clone <url>: Clone (download) a repository that already exists on GitHub at specified url.

git push: Upload local repository content to the remote repository.

git pull: Fetch and immediately merge remote's copy of current branch.

git add <file 1> <file 2>: Stage changes for the specific files.

git add .: Stage all files in the current directory.

git commit -m "<message>": Save your changes locally along with a custom message.

Workflows

Basic Workflow

  1. Make progress / develop your project.
  2. Stage new and modified files.
git add <file 1> <file 2> <etc.>

Or stage all files at once (notice the dot):

git add .
  1. Commit your changes.
git add commit -m "New feature"
  1. Push changes.
git push
Categories: software
Find an error or a bug?

Send me an email including the article's URL.

Go back to the homepage