Introduction to Git

What is Git?

Git is a distributed version control system that allows you to track changes to files and collaborate with others.

What is a version control system (VCS)?

A version control system is a tool that allows you to track changes to files and collaborate with others.

Setup Instructions

Git is available on most operating systems. You can download it from git-scm.com/downloads.

You can also install it using a package manager, if you are using one. For example, on Ubuntu:

sudo apt install git

Checking install

You can check if git is installed by running the following command in your terminal:

git --version

Configuring Git

Git needs to know who you are before you can start using it. You can configure your name and email address using the following commands:

              
                $ git config --global user.name "Your Name"
                $ git config --global user.email "your@email.com"
            
            

Repositories

A Git repository is a container for a project that is tracked by Git. Treat it like a folder that contains all the files for your project and a hidden folder that contains all the information about the project.

Types of Repositories

There are two types of repositories:

  • Local
  • Remote

Some Basic Commands

Some of the basic linux commands that you will need to know to use:

              
                  $ pwd
                  $ ls
                  $ cd
                  $ mkdir
                  $ mv
              
              

Git Basic Workflow

Initializing a repository

You can create a new repository by running the following command in your terminal:

                  
                      $ git init
                  
                  

Making First Changes

Checking Status

To check the changes that you have made to your files, and the overall changes to your repository, you can run the following command:

                  
                      $ git status
                      $ git diff
                  
                  

Staging Changing

To stage changes to your files, you can run the following command:

                  
                      $ git add <file-name>
                  
                  

Commiting Changes

You can make a checkpoint of your stagged changes by commmitng them. You can do this by running the following command:

                  
                      $ git commit -m "commit message"
                  
                  

Try making changes to the file and commiting it

Git History

Git Log

You can view the history of your repository by running the following command:

                  
                      $ git log
                  
                  

It will show you the commit history of your repository.

Removing Stages

You can remove a file from the staging area by running the following command:

                  
                      $ git reset <file-name>
                  
                  

Undoing Commits

You can use the commit hash to undo a commit

                  
                      $ git revert <commit-hash>
                  
                  

Stashing

When you want to switch to a different branch, but you have some changes that you don't want to commit, you can stash them.

                  
                      $ git stash
                      $ git stash list
                  
                  

You can get back the changes that you stashed by running the following command:

                  
                      $ git stash pop
                  
                  

Starting with GitHub

Install GitHub Desktop Client

You can download the GitHub Desktop Client from desktop.github.com.

Working With Remote Repositories

You can a remote repository in your github account and push your local repository to it after you have made some commits.

                  
                      $ git remote add origin <repository-url>
                      $ git push -u origin main
                  
                  

You can pull changes from a remote repository by running the following command:

                  
                      $ git pull origin main
                  
                  

Branches

Branches are used to work on different versions of a repository at the same time. The default branch is called the main branch.

You can create a new branch by running the following command:

                  
                      $ git branch <branch-name>
                  
                  

Working with Existing Projects

You may not always be starting a project from scratch. You may want to work on an existing project. You can do this by cloning the repository.

                  
                      $ git clone <repository-url>
                  
                  

Forking and Pull Requests

What's Next

There are a lot of things that we didn't cover in this workshop. Here are some of the things that you can learn next:

                
                    $ git merge
                    $ git rebase
                    $ git cherry-pick
                    $ git reflog
                
                

Thank You