Git important commands
"Passionate Cloud and Security Professional | AWS Cloud Architect | Proficient in Linux + Windows OS | Expert in Shell Scripting | Networking Enthusiast | Terraform Aficionado | DevOps Advocate | Actively Exploring Exciting Job Opportunities | Open to Collaborations and Networking | Bachelor's in Computer Engineering | Cloud Engineering Intern at LTIMindtree | Ready to Drive Digital Transformation with Innovative Solutions | Connect with me to Explore Potential Synergies! π»π #JobSeeker #CloudComputing #DevOps #AWS #Networking"
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:
π Git Official Documentation
Installing Git on Ubuntu
Open Terminal (
Ctrl + Alt + T).Run the following command to update the package list:
sudo apt updateInstall Git using:
sudo apt install git -yVerify the installation:
git --versionThis 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 --listYou 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.gitOpen Terminal on Ubuntu
PressCtrl + Alt + Tto open the terminal.Clone the Repository
Run thegit clonecommand 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.githas 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.gitsuffix indicates it is a Git repository.
Verify the Cloning
After cloning, navigate to the directory where the repository is cloned:cd git-tutorialYou can now list the files in the directory:
lsYou should see the
README.mdfile 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 calledmainormaster). 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 branchThis 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.txtUsingvi
Use thevieditor to create a new file calledhello-world.txtand add some content to it:vi hello-world.txtPress
ito enter insert mode, then type:Hello, World! This is my first file in Git.Press
Escto exit insert mode, and type:wqto 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 branchYou should see something like this:
* feature-hello-world mainThe asterisk next to
feature-hello-worldshows you're currently on that branch.
Add the New File
To addhello-world.txtto Git (or any other file), use the following command:To add only the specific file:
git add hello-world.txtTo 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 statusto 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 mainevery 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-worldwith the branch name.

Recap of the Key Commands:
Check Branch:
git branchCreate a Branch:
git branch <branch-name>Switch Branch:
git checkout <branch-name>Create and Edit File:
vi hello-world.txtAdd File:
git add hello-world.txtOr to add all files:
git add .Commit Changes:
git commit -m "Message describing changes"Push to GitHub:
git push -u origin main
