Git important commands
What is Git?
Git is a distributed version control system (VCS) that helps developers track changes in their code, collaborate with others, and manage projects efficiently. It was created by Linus Torvalds in 2005 to handle the development of the Linux kernel.
With Git, multiple developers can work on a project simultaneously without overwriting each other’s work. It keeps a history of all changes, so you can easily revert to a previous version if something goes wrong.
Advantages of Git:
Version Control – Keeps track of every change in the project.
Collaboration – Multiple developers can work on the same project without conflicts.
Branching & Merging – Create separate branches for features and merge them when ready.
Speed & Performance – Git is fast, even for large projects.
Distributed System – Every developer has a full copy of the repository.
Security – Uses cryptographic hash functions to protect data.
Open Source – Free to use and widely supported.
What is GitHub?
GitHub is a cloud-based platform that hosts Git repositories and provides tools for collaboration. While Git is the technology for version control, GitHub acts as a remote service where developers store their projects, share code, and contribute to open-source projects.
Why Do We Need GitHub?
While Git is a great tool for version control, it has limitations when it comes to collaboration, remote access, and project management. GitHub solves these problems by:
Centralized Collaboration – Instead of manually sharing Git repositories, teams can work together in a single place online.
Remote Storage & Backup – Your code is stored securely in the cloud, preventing data loss.
Easy Contribution to Open Source – Developers worldwide can contribute to projects via pull requests.
Issue Tracking & Documentation – Helps manage bugs, feature requests, and discussions.
Automated Workflows (CI/CD) – Automates testing, deployment, and integrations with GitHub Actions.
Access Control & Security – Teams can control who can view or modify the code.
GitHub is widely used by both individuals and enterprises to collaborate on software development projects, making it one of the most popular platforms for open-source and professional coding.
Stages of Git
Working Directory – This is where you create and modify files. It contains your project files before they are added to version control.
Staging Area – A temporary storage where changes are added before committing. This helps you organize your updates before saving them permanently.
Git Repository (Local Repo) – The place where Git permanently tracks all changes. Once you commit changes, they are stored in the repository.
Remote Repository – A version of your project stored on a cloud platform like GitHub, GitLab, or Bitbucket. It allows collaboration with others.
Types of Version Control Systems
Centralized Version Control System (CVCS)
Uses a single central server where all changes are stored.
Developers pull files from the central server and push changes back.
Example: Subversion (SVN), Perforce.
Problem: If the central server crashes, all history may be lost.
Distributed Version Control System (DVCS)
Every developer has a full copy of the repository, including all history.
Works offline and allows multiple developers to collaborate without depending on a central server.
Example: Git, Mercurial.
Advantage: No risk of losing history if the central server crashes.
Difference Between CVCS and DVCS
Feature | CVCS (Centralized) | DVCS (Distributed) |
Storage Location | Central server | Each developer has a full copy |
Offline Work | Not possible | Possible |
Speed | Slower | Faster |
Risk of Data Loss | High (if server fails) | Low (backup copies exist) |
Examples | SVN, Perforce | Git, Mercurial |
How to Install Git
To install Git, follow the official installation guide:
Installing Git on Ubuntu
Open Terminal (
Ctrl + Alt + T
).Run the following command to update the package list:
sudo apt update
Install Git using:
sudo apt install git -y
Verify the installation:
git --version
This should display the installed Git version.
There is a small mistake in the command you provided. The correct syntax to set up your Git username and email globally is:
Set Git Username and Email on Ubuntu
Open Terminal and run the following commands:
git config --global user.name "YourUsername" git config --global user.email "yourgmailid@gmail.com"
Verify the configuration to check if your details are set correctly:
git config --global --list
You should see an output like
Steps to Create a GitHub Repository
Log in to GitHub
Go to GitHub and log in with your credentials. If you don't have an account, create one by clicking on "Sign Up" and following the steps.Create a New Repository
After logging in, follow these steps to create a new repository:Configure the Repository
Create the Repository
Once you've filled out the necessary information, click on "Create repository".
Clone the GitHub Repository to Your Local Machine (Ubuntu)
Now, let's clone the repository to your Ubuntu system:
Copy the Clone URL
On your GitHub repository page, you will see a green button called "Code". Click on it to reveal the repository's URL for cloning.For example, the URL might look like this:
https://github.com/gauravhalnawar1011/git-tutorial.git
Open Terminal on Ubuntu
PressCtrl + Alt + T
to open the terminal.Clone the Repository
Run thegit clone
command with the repository URL you copied from GitHub:git clone https://github.com/gauravhalnawar1011/git-tutorial.git
Explanation of the URL
The URLhttps://github.com/gauravhalnawar1011/git-tutorial.git
has the following parts:https://
: This indicates the protocol for communication.github.com
: This is the domain where the repository is hosted.gauravhalnawar1011
: This is the GitHub username or organization name.git-tutorial
: This is the name of the repository..git
: The.gi
t
suffix indicates it is a Git repository.
Verify the Cloning
After cloning, navigate to the directory where the repository is cloned:cd git-tutorial
You can now list the files in the directory:
ls
You should see the
README.md
file that was created when you initialized the repository.
Working with Branches in Git
What Are Branches in Git?
A branch in Git is like a separate version of your project where you can make changes without affecting the main version (usually calledmain
ormaster
). You can create new branches to work on different features or fixes and then merge them back into the main branch when ready.Check the Current Branch
To check which branch you're currently on, use the following command:git branch
This will list all branches. The current branch will have an asterisk (
*
) next to it.Create a New Branch
To create a new branch, use the following command:git branch <branch-name>
For example, to create a branch called
feature-hello-world
, run:git branch feature-hello-world
Switch to a New Branch
After creating a new branch, you need to switch to it to start working on it:git checkout <branch-name>
To switch to the newly created branch
feature-hello-world
:git checkout feature-hello-world
Alternatively, you can combine the branch creation and checkout commands with:
git checkout -b <branch-name>
This will both create and switch to the new branch in one step.
Create and Edit Your First File
Create
hello-world.txt
Usingvi
Use thevi
editor to create a new file calledhello-world.txt
and add some content to it:vi hello-world.txt
Press
i
to enter insert mode, then type:Hello, World! This is my first file in Git.
Press
Esc
to exit insert mode, and type:wq
to save and quit.
Basic Git Commands for Tracking Changes
Check the Current Branch
As mentioned earlier, use this command to see which branch you're on:git branch
You should see something like this:
* feature-hello-world main
The asterisk next to
feature-hello-world
shows you're currently on that branch.Add the New File
To addhello-world.txt
to Git (or any other file), use the following command:To add only the specific file:
git add hello-world.txt
To add all files (including new, modified, and deleted files):
git add .
Commit the Changes
After adding the files, commit the changes with a message describing what you did. This message helps others understand the purpose of the changes.git commit -m "Added hello-world.txt for Git tutorial"
It's good practice to write meaningful commit messages to make it clear what changes you made.
Check Git Status
Usegit status
to see the state of your repository. This will tell you if there are any uncommitted changes or files that need to be added.git status
Push Changes to GitHub
After committing your changes, it's time to push them to GitHub. Use the following command to push to the main branch:git push -u origin feature-hello-world
-u
: This sets the upstream branch, so you don't need to specifyorigin main
every time you push.origin
: This refers to your remote repository (GitHub).feature-hello-world
: This is the branch you're pushing to. If you're pushing to a differentfeature-hello-world
, replacefeature-hello-world
with the branch name.
Recap of the Key Commands:
Check Branch:
git branch
Create a Branch:
git branch <branch-name>
Switch Branch:
git checkout <branch-name>
Create and Edit File:
vi hello-world.txt
Add File:
git add hello-world.txt
Or to add all files:
git add .
Commit Changes:
git commit -m "Message describing changes"
Push to GitHub:
git push -u origin main