Mark As Completed Discussion

What is Git?

In the past decades, software engineering started to become more complex than ever, requiring larger project structures, and more collaborators on the same project. These advancements in technology and software development showed an increased need of a version control system. This type of system is responsible for changes tracking in software projects, and other types of documents, and thus allows multiple collaborators to work on the same project.

Git is a type of version control system, and it is currenly the most widely used one across the globe. It was created in 2005 by the creator of the Linux kernel, Linus Torvalds. Today it is an open source project, and works on a wide range of environments and operating systems.

How it works?

Git is available to download and be used on any OS, usually from the command line, or sometimes integrated as an extension in some IDE, which in the background uses command line as well. It offers a variety of commands, to do anything you can imagine to manage the versions of your projects, and simplify the workflow of your team.

For example, these are the most common steps you should consider in working with Git as a version control system for your project:

  1. Create a project (often called "repository") with a git hosting tool (like Github for example)
  2. Copy (clone) the repository to your local machine
  3. "Pull" someone else's changes to your local machine
  4. Create a "branch" (new version of the project, on which you will apply your changes), make the change, and then commit it
  5. Open a "pull request" (propose changes to the main branch), which then your teammates can review and approve
  6. "Merge" your branch to the main branch your content.

    What is Git and How Does It Work?

In this tutorial, we are going to walk you through the most common and important git commands, to get you started on the right track!

Initializing and cloning a repository

In order to use git with your project, you first need to either create your own repository, or clone an already existing one, which is uploaded on some git hosting tool.

SNIPPET
1$ git init algoDailyProject
2Initialized empty Git repository in /Users/test/algoDailyProject/.git/

This will initialize a new repository with the given name on your local machine, on the path provided.

SNIPPET
1$ git clone https://github.com/algoDaily/testRepo.git
2Cloning into 'testRepo'...
3remote: Counting objects: 94, done.
4remote: Total 94 (delta 0), reused 0 (delta 0), pack-reused 94
5Unpacking objects: 100% (94/94), done.
6Checking connectivity... done.

This command as shown, clones an already existing repository on the provided GitHub link, onto your local machine. It initializes a git repo, and also stores all the files from the project on a given location on your machine.

Working with branches

Branches are basically a copy of your project, where you make your changes. When you work on your newly opened branch, those changes stay there, until merged on the 'main' branch. Usually, each team establishes its own branching strategy for easier management, but the most common and simple one consists of: 1. Having a 'main' branch, that holds the production version 2. A 'develop' (or 'dev') branch, that has the beta version 3. A 'release' branch, that transfers the changes from the develop branch, to the main 4. And 'feature' branches, that are used for ongoing development that usually are followed by the name of the change (e.g. feature/add-a-subscribe-button)

What is Git and How Does It Work?

Let's test your knowledge. Click the correct answer from the options.

On which branch is the production version of the project stored?

Click the option that best answers the question.

  • develop
  • master

You can create a new feature branch with a given name with this command:

SNIPPET
1$ git branch feature/add-a-subscribe-button

You can list all the branches in your repository:

SNIPPET
1$ git branch

Switch to a desired branch, by providing its name:

SNIPPET
1$ git checkout my_branch

Merge a branch "_a" into branch "_b". It automatically selects the changes and applies to the branch _b. If conflicts occur (having changes on the same place in a certan file), you should resolve them manually. by picking the changes you want to leave or remove.

SNIPPET
1$ git checkout branch_b 
2$ git merge branch_a

And if you want to see the differences you made inside your branch, compared to the parent branch, you can use:

SNIPPET
1$ git diff

Or, to see changes between two specific commits, by using their unique commit IDs:

SNIPPET
1$ git diff commit1 commit2

You can also list the change dates, and the author of the changes, if you need to see when someone performed a change, on a specific file:

SNIPPET
1$ git show [commit]:[file]

Or simply view the full change history:

SNIPPET
1$ git log

Are you sure you're getting this? Click the correct answer from the options.

Which command would you use to enter a specific branch?

Click the option that best answers the question.

  • git checkout
  • git merge

Synchronize local and remote repositories

The changes you make to the repository, to the files, or the branches, will stay on your local machine unless you synchronize with the remote one. Same goes the other way - if you do not synchronize, you will not have the latest changes someone else did on the repo.

Synchronize repositories

The following commands will help you stay up to date, and manage your repositories:

Fetch This command takes the latest commits from the remote repository, but it does not add them to a local branch on your workspace.

SNIPPET
1$ git fetch

Pull Pull does take those changes, and it does it by performing a fetch, followed by a merge command, which puts the changes in a local branch.

SNIPPET
1$ git pull

Push By using push, you transfer the local changes, to the remote repository. Before using push, you have to use the commit and add command in order to prepare the changes for pushing.

SNIPPET
1$ git add .
2$ git commit -m "commit message"
3$ git push

Synchronize repositories

Are you sure you're getting this? Is this statement true or false?

The command git pull takes the changes from the remote repository, true or false?

Press true if you believe the statement is correct, or false otherwise.

Advanced commands

Despite the previously mentioned commands, there are plenty of other commands that can do wonders if you learn how to use them. We will cover some of the most useful and interesting ones.

To clean the repository from the untracked files (the ones not added with the git add command):

SNIPPET
1$ git clean

The command git bisect can help you find the error in some previous commit. It walks you through the past commits until you find the broken one!

SNIPPET
1$ git bisect

And another usage of the git commit command can be $ git commit --amend , which is a convenient way to modify the most recent commit. It lets you combine staged changes with the previous commit instead of creating an entirely new commit.

However, when in doubt, you can always use $ git command --help to see the usage of some commands!

Lastly, these commands can significantly improve your productivity and the perfromance in your work, you do not have to remember them all, but remembering the most important ones can save you some time!

Are you sure you're getting this? Is this statement true or false?

git show shows the changes inside a specific file, true or false?

Press true if you believe the statement is correct, or false otherwise.

One Pager Cheat Sheet

  • Git is an open source, widely used version control system, which simplifies the collaboration of software developers by allowing them to track changes, create branches, and merge them back to the main repository.
  • You can use git branch, git check and git merge to create, select and merge branches, and view diffs, commits and the change history with git diff, git show and git log.
  • Git checkout can be used to switch branches, providing the name of the desired branch to enter, for example git checkout my_branch.
  • Fetch, Pull and Push are the three Git commands used to synchronize local and remote repositories.
  • The git pull command fetches and merges the latest changes from a remote repository into the local branch of your workspace.
  • By learning the most important commands such as git clean, git bisect, git commit --amend and git command --help, you can significantly improve your productivity and performance.
  • The command git show is used to view the changes within a specific file, and optionally takes a commit identifier as an argument to display the changes for a specified commit.