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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s