What is Git

Share this post

This post is part of the complete guide on Git and Github

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

How do they collaborate?

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

They use Git.

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

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. It also helps to do more that. Version control can be used to collaborate with others, but also for deploy source code from your local machines to some staging server, then to some testing server and ultimately to production.

It is an integral tool that also lets you deploy your code to the cloud.

Git is good at making sure that when you are making changes you have access to the latest version of your code, but also that you can keep track of all the different versions (or variations) of the source code.

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.

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 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 <[email protected]>
Date:   Sat Jun 20 16:55:09 2020 +1000

    Update hello.py

commit 4dbaca229dbaca2293e0e8365c01f8ed
Author: jcchouinard <[email protected]>
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.

This is the end of this introduction to Git.