Getting Started with Git

This is a quick step-by-step tutorial on getting started with Git version control. Step by step instructions will guide you to create a new free Git repository, stage, commit and push changes from your local filesystem to remote in less than few minutes.

In case you are totally new to the concept of Git version control system, we would suggest you to have a quick read on : What are version control systems.

To make it easy to understand we are solving 3 scenarios:

Scenario 1: You are working on a project with a friend who is abroad. Both of you will be working on the same code-base. You are required to create a new repository and share it with your friend so he can start contributing.

  • Signup with a free Git service provider and create a remote repository. For this example we are going ahead with Github but there are many free service provides available. See a comparison between the features provided by free Git service providers.
Creating a new Github Repository.
  • Open a Command Prompt (Windows)/ Terminal (iOS or Linux) and navigate to the directory where you have the currently working files. (New to command prompt ? Read our quick cheat-sheet on command prompt)
Using Git commands.
  • Use the command prompt to initialize a new local repository :
git init
Git folder structure.

NOTE: Your local git repository will be created inside your workspace folder with the name `.git` in most operating systems this will be a hidden folder.

  • Check what are the files or folders to be added to the local repository:
git status
  • Stage all the resources to the local repository.
git add .
  • Commit the staged changes with a meaningful message
git commit -m “initial commit with v1 changes”
  • Next we are going to connect our Local Repository to the Remote Repository we initiated in Github.
git remote add origin <YOUR REPOSITORY URL> 
  • Push the committed changes to the remote repository.
git push -u origin master

At this stage all your local changes are pushed to the remote repository and anyone with access should be able to simply clone the repository.

Scenario 2: Your team mate has invited you to work on a project and she has sent you the URL of the remote Git repository.

  • To get started, you need to get a copy of the repository to your local machine.
  • Open a Command Prompt (Windows)/ Terminal (iOS or Linux) and navigate to the directory where you have the currently working files. (New to command prompt ? Read our quick cheat-sheet on command prompt)
  • Create a copy (clone) of the remote repository in your local machine:
git clone <REPOSITORY URL> 

Scenario 3: Your team mate has done some changes to the file that both of you are working on. You need to obtain the latest changes to your local repository.

In Git world we call it syncing the local repository with the remote .

  • To bring your local repository up to-date with the remote repository you need to run the pull command inside the git workspace.
git pull
  • In case your files are having conflicts with the remote files you will be asked to resolve the changes before merging with up coming changes.

That is pretty much all you need to do to get started with git version control system. To explore more on Git based version control read our related articles on the topic:

Cite this article as: Sam Muller, "Getting Started with Git," in Cyber Memos, April 13, 2019, https://cybermemos.com/developent/version-control/getting-started-with-github/.