What is Git

Git is a powerful version control system for tracking changes made to source code over time.

How do programmers keep track of different versions of their code?

How do they collaborate?


Subscribe to my Newsletter


How do they make sure that they can roll-back errors introduced in the code?

They use Git.

Become a Git Master

Although very powerful, Git is very complex. I highly recommend that you follow Datacamp’s Git course to really become comfortable with Git commands and avoid painful mistakes.

What is Version Control?

Version control is used to maintain multiple versions of source code. This helps to keep track of changes to the source code over time, collaborate with others, and more.

This way, you know the history of your project and how it’s developed from the beginning up until today.

What Git Does for You?

  1. Keeps track of changes to the code;
  2. Synchronize code between different users;
  3. Allows testing changes to the code without losing the original;
  4. Allows to rollback to old versions of code.

Why use Git

Git is an incredibly popular version control system and for a good reason.

Why Git is useful:

  • Git is Free
  • Git is Fast
  • Git is Distributed
  • Git Allows Staging
  • Git Prevents Losses

Is Git Free?

Git is an open source version control system, which means that it is free. Anyone can download Git, any can use it and anyone can contribute to it.

Why is Git is Fast?

Since all changes are made on your local computer, you don’t need to rely on a network connection to view history and changes to a file

What Does in Mean When We Say that Git is Distributed

Instead of a copy of a single file, you have the entire project and its history on your machine. This means that all developers have a copy, which means there are a lot of backups for a project. It also mean that developers are not necessarily blocked in case something breaks on the main branch.

Why is Staging Useful in Git?

Git allows to save files and stage them in a series where you can evaluate all the changes prior comiting the work.

How Git Helps to Prevent Losses

Having multiple copies on multiple branches and computers, as well as a copy of an entire history of changes makes Git a powerful tool for data integrity.

What is Github

GitHub is a website that lets you store Git repositories on the internet.

It lets you to manage public and private repositories that can be shared with any users.

Basic Git Commands

Git Status

The git status command allows you to check the status of your repository.

$ git status

Git Clone

After you have installed Git, one of the first commands that you will use is git clone. The git clone command has just one purpose: to take a repository store somewhere (like on Github) and download it to your computer.

$ git clone <url>

The <url> is the URL of the location of a Github repository.

Git Add

The git add command is used to tell which files you want to add to your next commit.

$ git add <filename>

This command tells which files you want to track next time that you make a commit.

Git Commit

Simply put, “to commit” means to save. It takes a snapshot of the current repository and saves it. You can do that by using the git commit command.

$ git commit -m "message"

The -m (for message) is used as a flag to tell what is coming up and the "message" is where you can add a simple message describing what happened during this commit.

Git Push

Sending changes from your computer to Github is called a push. To push the local version of the source code to somewhere on the internet, use git push.

$ git push 

Git Pull

The opposite of a push is a pull. To download the latest changes from Github to your computer, use git pull.

Git pull lets you download the latest changes that people that share access to the repository have made to it.

By using git pull, you will have the up-to-date history of what was pushed to Github by your co-workers.

$ git pull 

Merge Conflicts

When it can, Git will automatically take any changes made and will combine them for you.

However, in some cases, if you and someone else have made different changes to the same lines, you will run into a merge conflict.

You’ll get a message like this:

CONFLICT (content): Merge conflict in file.py Automatic merge failed; fix conflicts and then commit the result.

By opening the file in VSCode, you will see this kind of error that results from the merge conflict.

What does it mean?

It means that anything between <<<<< HEAD and the equal sings ======= are the changes that you made before you tried to do the pull.

Anything between the equal signs and the >>>>> and gibberish characters, are the changes that you pulled from the remote location.

The gibberish characters (hash) will let you identify exactly which commit in Github caused the conflict.

To solve this, remove all the lines that you don’t want to keep from the file, and then commit and push the repository.

Git log

git log shows you a history of all the commits that you made.

By using git log, you will see each commit with a unique hash to identify the commit, the name of the author, the date of the commit and the message of the commit.

commit a2293229dbaca2293e0e8365c01f8ed
Author: jcchouinard <example@gmail.com>
Date:   Sat Jun 20 16:55:09 2020 +1000

    Update hello.py

commit 4dbaca229dbaca2293e0e8365c01f8ed
Author: jcchouinard <example@gmail.com>
Date:   Sat Jun 20 16:54:34 2020 +1000

    add a line

Git reset

If you committed something that you wish you haven’t, use git reset.

$ git reset --hard <commit>

Replace the <commit> with the hash number next to the commit you made to reset to that version of the code. Again, you can find that hash using git log.

You could also run the command below to reset to the origin where you cloned the repository from.

$ git reset --hard origin/master

Example, if you cloned your repository from Github, running git reset --hard origin/master will roll-back to the version that was previously on Github.

Where Does Git Stores Information?

Git stores information in a .git directory. This directory is located at the root directory of the repository.

Other Version Control with Git and Github Posts

Learn Git and Github (Complete Guide)
Get Started With Github

How to Use Git and Github with VSCode

This is the end of this introduction to Git.

4.7/5 - (4 votes)