The Ultimate Guide for beginners: Getting started with Git commands

One of the best version control that I have used so far for managing multiple repositories and files in a smooth and distributed . The open source tool which we are talking here is Git the best control version. Please follow along to know the more on on how to work with Git , setup Git on ubuntu and windows machine & best commands and setup guide.

This tutorial is going to add a lot of valuable knowledge on Git in your pockets, Stay tuned !!

Table of Content

  1. Getting Started with Git
  2. Create a new git repository using graphical interface on GitHub
  3. Creating a new git repository using command line on ubuntu machine
  4. Getting Started with Git Commands for beginners
  5. Summary

Getting Started with Git

What is Repository ?

Repository is a place where you keep all your source code it could be project wise or you can create it for a particular technology or you can create even for a single file, it all depends on you and your requirement. Its always a good idea to provide the name of repository according to work which you are going to do. There are some cloud based source code tools which allows you to create your repositories such as bitbucket , GitLab, GitHub ,Subversion , SVN and Mercurial etc. where you can create your own repositories.

Create a new git repository using graphical interface on GitHub

  • Before we create the first repository make sure you have GitHub account created . If you don’t have please click here and Sign up.
  • If you have access to it then open your browser and go to GitHub website from here and click on Sign in.
  • Now click on New button to create a new repository
  • Now, provide a suitable name of the repository and you may keep it as public open to world if you wish people can see your code else keep it private. We are keeping as public as this is a demo tutorial and finally select create repository.
  • Now your repository is ready to be used. Please make sure you below steps which we will use later in this tutorial.
    • create a new repository on the command line and
    • push an existing repository from the command line

Creating a new git repository using command line on ubuntu machine

  • You must have ubuntu machine preferably 18.04 version + and if you don’t have any machine you can create a ec2 instance on AWS account
  • Recommended to have 4GB RAM
  • At least 5GB of drive space
  • SSH into your ubuntu machine
  • Now create a folder under /opt directory
cd /opt
mkdir git-demo
cd git-demo/
  • Initialize your new repository
git init
  • Create a file inside the same directory using the command
echo "My first change" > adding-new-file.txt
  • Now check the status of git repository using the command
git status
  • Add the file in git repository using the command
git add .
  • Again check the status of git repository using the command
git status
  • Commit your changes in git repository using the command
 git commit -m "MY FIRST COMMIT"
  • Add the remote repository which we created earlier as a origin.
git remote add origin https://github.com/Engineercloud/git-demo.git
  • Push the changes in the remote branch ( Enter your credentials when prompted)
git push -u origin master
  • Now Verify if the text file which we created is present in repository

Getting Started with Git Commands for beginners

Here we will work with most important commands of Git. Let us work with same directory which we created earlier.

cd /opt
mkdir git-demo
cd git-demo/
This image has an empty alt attribute; its file name is image-300.png
  • Check the status of the repository
git status
  • Add a file in the directory and run the command
echo "Adding new file again" > second_file.txt
  • Now check the status of repository again, it should show untracked files as they are added in repo yet
  • Add files in repository by using command
git add .
  • Check the status of git again
  • To check the status of git with short status
git status -s
  • Now edit the file which we created and check the status .
    • You will notice that there will be two things one is changes to be committed because we already added them using git add.
    • Also you will notice changes not staged for commit because we recently edited our file but didn’t add it.
echo "I am editing my second file " >> second_file.txt
  • Add the file in repository which we modified.
git add .

git status
  • Although we know that we modified a line and checked the status , but if you want to check the difference between the two you can use command.
git diff        # Git diff provides the information which is not committed
  • Also you can check the committed change so far by using the command.
git diff --cached      # Git diff --cached provides the information what is committed
  • Commit all your changes
git commit -m "Committing all my changes" .       # m here means message
  • To delete your staged file properly, that is how to delete those files which are already committed
git rm second_file.txt  # This command will also remove the file from the directory
  • If you wish to delete your staged file properly but keep the file in the directory for future use
git rm --cached second_file.txt  # This command will not remove the file from the directory
  • To check history of your commits in a git repos use a command
git log  # The command gives the list of commits in reversal order which is expected.
  • To check history of your commits with the difference between each commit we use -p flag
 git log -p -2    # -p stands for patch that is difference and 2 here is last 2 commits
  • There are some more commands to view the commits in more presentable way , lets checkout.
git log --pretty=format

git log --pretty=format:"%h %s" --graph

Summary

You should now have a very sound knowledge of what Git is , how to create Git repository using graphical mode as well as using command line tool . Also we discussed how to work with Git repositories using different commands. This tutorial consists of all the practical’s which were done on our lab server with lots of hard work and efforts.

Please share the word if you like it and hoping you get benefit out of this tutorial.

You can also visit : Introduction-to-git-a-version-control-getting-started-with-git-step-by-step

Introduction to Git Version control and Getting Started with Git step by step

Are you tired of taking backups of your multiple files and facing syncing errors of your files ? Do you have any central place where you can store your files with proper versions and history ? Most of us faced this issue of file sync issues or how to control you same files with some updates. Git is a version control which solves all these issues & has grown the popularity for the way it works .

In this tutorial we will discuss what is version control , what are types of version controls and how to install git a version controller on windows machine and then setup to working with it.

Table of Content

  1. What is Git ?
  2. How to Install git version 2.31.0 on Windows machine?
  3. Three types of control versions
  4. Setting up Git Bash on windows machine
  5. Summary

What is Git ?

Git is a version control which means if you have lots of file and each of these files are updated with few changes from the previous one then you need some way to handle it and this is taken care very well by Git. Sometimes you need to revert the changes back to previous state or you need to check few things of a month back , those all things are maintained by version control very well.

Three types of control versions

  • Local control version – In this case you take files from version control database to your local machine it’s just like a copy from one folder to other. In this case no team member knows what you are doing.
  • Centralized control version – In this case you can checkout files from centralized location which is better than local control as team is aware about the project and what team members are doing but in case if server goes down nobody will have full backup as in complete repository data and can cause significant loss. Example: SVN, subversion
  • Distributed control version: This is version control which manages everything in distributed way that means when you clone any repository it takes all data of one repository and copies on your local machine. Git is among the distributed control version

How to Install git version 2.31.0 on Windows machine

  • Open your browser and click on the link here so that installation starts automatically.
  • Now Click on the downloaded Git-2.31.0-64-bit.exe and select YES
  • Follow along with me and click the Next button and in most of the case select default values which are already selected
  • Select the location on your system where you like to place GIT installation files.
  • It is better to add it on Desktop
  • Please select the recommended
  • Here you will be asked to choose SSH executable and select OpenSSH. This will allow us to work with SSH connections such as logging into Linux machine.
  • Use MinTTY as default
  • Finally Git is launched on your machine and GIT bash will be available to use.

Setting up Git Bash on windows machine

  • Let us verify is Open SSH is working from Git Bash.
  • Now, you can check the config file where you can modify some configurations if required and to check that run the command below.
git config --list --show-origin
  • You can modify the global name while using Git by using below command
  • Check all your settings by using command
git config --list

Summary

You should have a basic knowledge of what Git is and how it’s different from any centralized version control systems you may have been using previously. You should also now have a working version of Git on your system that’s set up with your personal identity.