Basics of Git
Git is a tool that helps developers track changes in their code. It’s like a time machine for your project, allowing you to see what changed, when, and by whom.
what is Git is why it is used?
Git is a version control system that helps you manage changes to files in your project over time. Imagine writing a book, Git is like a notebook where you record every change you make to your draft. If you mess up, you can look back at your notes and restore an earlier version of the book.
It is used to:
- save versions of your work
- collaborate with others
- keep track of changes without losing anything
Now after you have understood what Git is Now lets install Git.
Installing and configuring Git
Install Git on your system by downloading it from: git-scm.com. follow the instructions provided on the official website to: Install Git
After Installing open Git Bash. as shown in below image
- Then configure your name using the command:
git config --global user.name "Your Name"
- Finally configure your email with:
git config --global user.email "your-email@example.com"
- Now you have saved you name and email.lets verify everything using below command:
git config --global --list
Before starting with git commands. let's first understand some terminology related to git
Git Terminology
- Repository (Repo) : A repository is where Git stores all your project files and their version history.Think of a repository as a folder where you keep your project. Git keeps a hidden "diary" in that folder, recording everything you do.
- Working Directory : The working directory is the folder where you’re actively working on your files.It’s like your desk where you spread out your papers to edit.
- Staging Area : The staging area is where you prepare files before saving them in Git’s history. Imagine a box(staging area) where you gather paper(files) to click a photo (commit).
- *commit * : A commit is a snapshot of your project at a specific point in time.Imagine taking a photo of your desk. A commit freezes the current state of your work so you can always look back at it later.
- Branch : A branch is a separate line of development in your project.It’s like working on a copy of your book while keeping the original safe. Once you’re done, you can merge the changes back into the main book.
- Merge : Merging combines changes from one branch into another.If two people write different chapters of a book, merging is putting those chapters together into one final draft.
- HEAD : HEAD is the pointer to your current commit or branch.HEAD is like a bookmark showing where you are in your project history.
Now you are done with Git basics let's dive into Git commands.
Essential Git Commands
These are the basic command that you will encounter daily. so Let's start:
- git init : This initializes a new Git repository in your project folder, allowing Git to track changes.
git init
This creates a hidden .git folder in your project directory, setting up Git for version control.
- git add : This command moves changes (modified or new files) from the working directory to the staging area.
git add file1.txt # Add file1.txt to staging area
git add . # Adds all changes in the current folder
This prepares files for committing.
- git commit : This saves the changes in the staging area as a snapshot in Git's history. Each commit has a unique ID and message to describe the changes.It’s like taking a photograph of your project at a specific moment.
git commit -m "Added a new feature to the homepage"
- git status : This command shows the current state of the working directory and staging area. It tells you what changes have been staged, what changes are not staged, and which files are untracked. Example:
git status
output:
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
file1.txt
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: file2.txt
- git log : This command displays the history of commits in your repository, showing the commit hash, author, date, and message.
git log
Working with Branches
A branch in Git is like a separate line of development. It allows you to work on new features, fix bugs, or experiment without affecting the main code.
- Create a Branch:
git branch feature-branch
Creates a new branch called feature-branch.
- Switch to a Branch:
git checkout feature-branch
Switches to the branch feature-branch.
OR
(modern way):git switch feature-branch
- Create and Switch to a Branch (Shortcut):
git checkout -b feature-branch
- List All Branches:
git branch
Shows all branches in the repository. The current branch is marked with *.
- Merge a Branch:
git merge feature-branch
Combines changes from feature-branch into the current branch.
- Delete a Branch:
git branch -d feature-branch
Deletes feature-branch after it’s merged.
Imagine you’re writing a book. The main branch (main) is your final version. When you want to write a new chapter, you make a photocopy (a branch). You work on this copy separately. Once done, you add the new chapter to the main book (merge the branch).
Remember: Depending on your PC environment, your local repository may have a default branch named either main or master. In this guide, we'll use main as the default branch name, aligning with GitHub's recent change from master to main.
Undoing Changes in Git
Mistakes happen, and Git makes it easy to undo changes at different stages. Let’s explore common scenarios with examples and outputs.
- Undo Changes in the Working Directory (git checkout or git restore)
If you’ve made changes to a file but haven’t staged it yet, you can discard those changes.
command:
git restore <file>
Example:
Suppose you edited index.html but don’t want the changes.
git restore index.html
Output:
The file index.html is restored to the last committed version. Changes are discarded.
- Unstage Changes (git restore --staged)
If you’ve staged a file using git add but want to remove it from the staging area without deleting changes:
command:
git restore --staged <file>
Example:
You staged style.css but realize it’s not ready to commit.
git restore --staged style.css
Output:
The file is moved back to the working directory, and git status will now show it as modified but not staged.
- Undo a Commit (git reset)
a. Undo the Last Commit (Keep Changes in Working Directory):
Command:
You committed too early and want to redo it.
git reset --soft HEAD~1
Output:
The last commit is undone, but changes remain in the staging area.
b. Undo the Last Commit (Unstage and Keep Changes):
Command:
git reset --mixed HEAD~1
Output:
The last commit is undone, changes are unstaged, and files are moved to the working directory.
c. Undo the Last Commit (Delete Changes):
Command:
git reset --hard HEAD~1
Example:
You committed unwanted changes and want to completely discard them.
git reset --hard HEAD~1
Output:
The last commit is undone, and changes are permanently deleted.
- Undo Changes in a Specific Commit (git revert)
Instead of deleting a commit, this creates a new commit that undoes the changes.
Command:
git revert <commit-hash>
Example:
To undo changes in commit abc123:
git revert abc123
- Discard Untracked Files (git clean)
Untracked file are the files you haven’t added to the staging area using git add. .
If you’ve added new files but don’t want to keep them:
command:
git clean -f
Example:
You created a file temp.txt but don’t need it.
git clean -f
Output:
The untracked file temp.txt is deleted.
Note: git clean -f command will delete all untracked files in your working directory. Here, -f flag is required to confirm you want to delete the files(Force Deletion).
Important Tip:
Dry Run Before Cleaning: To see which files will be deleted without actually deleting them, use git clean -n.git clean -n
Summary Table:
Action | Command |
---|---|
Undo changes in a file | git restore |
Unstage changes | git restore --staged |
Undo last commit (soft) | git reset --soft HEAD~1 |
Undo last commit (hard) | git reset --hard HEAD~1 |
Undo a specific commit | git revert |
Discard untracked files | git clean -f |
Git is an essential tool for managing code efficiently, and in this blog, we’ve covered the foundational concepts and commands you need to get started. From initializing repositories to branching and undoing changes, these skills form the backbone of version control and will help you work confidently in your projects.
Understanding these basics ensures you can track your progress, experiment without fear, and fix mistakes when things don’t go as planned. The remaining topics, such as working with remote repositories (GitHub), collaboration workflows, and advanced features, will be covered in my next blog. Stay tuned for more!
If you found this blog helpful or have any suggestions, feel free to reach out to me. Let’s connect TWITTER & Linkedin. Your feedback and support mean a lot to me as I continue sharing my journey in the tech world. 😊
Top comments (0)