Git Tutorial For Beginners

Vitaliy Dotsenko
Legacybeta
Published in
3 min readOct 12, 2020

Git is the most widely used Version Control System (VCS) in the software development profession. The best way to learn Git is to dive into the Command Line Interface (CLI). To get started, you need just open a terminal then type a command. The first step before we can do that is to make sure you have it installed.

Git Installation

Ubuntu

sudo apt install git

MacOS

For MacOS there are several ways to install git on your computer:

  1. Use Homebrew:
    brew install git
  2. Use a binary installer https://sourceforge.net/projects/git-osx-installer/
  3. Out of the box with XCode.

For more details check the link https://git-scm.com/download/mac

Windows

For Windows it can be installed via executable installer https://git-scm.com/download/win

Basic git commands

To use git you should know at least these basics and for any additional commands you can check official documentation.

First of all is how to create a new git repository

git init

Clone if the repository already exists, usually it’s on GitHub or GitLab

git clone URL

Show changes in the local directory

git status

To add a file to the local repository

git add FILE_NAME

To commit (apply) your changes to the local repository

git commit -m "Commit message"

Or open a text editor to set the commit message

git commit

To see the list of commits for the current branch

git log

Create a new branch with name BRANCH_NAME and switch to it

git checkout -b BRANCH_NAME

Or just switch to the branch if it’s already exists

git checkout BRANCH_NAME

Branch names that are typically used: dev, master, staging and feature branch names. GitHub and many other places are now adopting main as the primary branch name over master.

Usually if you switch branches and you have committed changes, you should “stash” your local changes. Then you can “un-stash” when you switch back to the branch you were on.

Save the file changes into the stash storage

git stash

Get the list of stash items

git stash list

Apply and remove the last state from the stash to the current working directory

git stash pop

Clear all stash items

git stash clear

Download the remote commits to the local repository

git pull origin BRANCH_NAME

Upload the local commits to the remote repository

git push origin BRANCH_NAME

Git Workflow

The first step will be individual for a new repository or exist.

If the repository exists on the remote repository provider like GitHub or GitLab

  1. You should clone a git repository:
    git clone URL
  2. If the repository doesn’t exist, First of all you should initialize git repository by the command:
    git init
  3. The next steps will be the same for both. Make sure you are on the right branch, and if needed switch to the right branch:
    git checkout BRANCH_NAME
  4. Make some file changes are in the project directory.
  5. Push your changes to the remote repository. Usually BRANCH_NAME is dev or master
    git push origin BRANCH_NAME

Git CLI vs Git GUI

Here are three Git GUIs that we recommend for the times when you just can’t figure out the right command on the CLI.

GitKraken — cross platform tool with merge conflict resolver. Has free license for open source developers.

GitHub Desktop — cross platform and open source git tool, created by GitHub.

Sublime Merge — cross platform and Freemium git tool, created by the company behind Sublime.

Modern IDEs have git support directly in the application including; VSCode, PhpStorm, WebStorm, Atom and others.

Links

https://git-scm.com/docs

https://try.github.io/

https://www.tutorialspoint.com/git/index.htm

https://www.atlassian.com/git/tutorials

--

--

Vitaliy Dotsenko
Legacybeta

I like coding, open-source software and hi-tech 🚀