What is git?
Source: Dev.to
Why You Need Git
For many developers, a pendrive is just a place to store and retrieve old projects or files.
But when you have too many folders, redundant files, and a nearly‑full pendrive, you start deleting the oldest folders and keeping only the last 1‑2 versions of a project.
Later, after adding new features and removing old ones, you may realize that a piece of basic functionality you need is missing and you have no record of the older versions.
That’s where Git comes in as a saviour.
What Git Gives You
- Every time you stage or push (later) a tested change, Git saves a snapshot inside the hidden
.gitdirectory. - If you mess up, you can backtrack to a previous version and keep your app running.
- Git compresses only the changes (deltas) rather than storing the full code each time, so the repository stays small compared with simply copying whole folders onto a pendrive.
- Snapshots are stored in a compressed format and include a full history of the project’s file structure.
Inside a Git Commit
When you commit, Git creates objects in .git/objects/:
| Folder | What it Holds |
|---|---|
blob | The actual file contents (the changes). |
tree | The directory structure for that commit. |
commit | Metadata (author, date, message) and the SHA‑1 hash that identifies the commit. |
Each commit gets a unique SHA‑1 hash.
When you ask for a previous version, Git:
- Looks up the commit object (metadata).
- Reads the associated tree to reconstruct the directory layout.
- Retrieves the required blobs (file contents).
- Writes the reconstructed files into your working directory.
Simple Developer Workflow
1. Initialise a Repository
git init
Creates the .git folder and tells Git to start tracking the project.
2. Stage Files
git add # add specific files
# or
git add .
Marks the files as ready to be committed.
3. Commit
git commit -m "Your commit message here"
Creates a snapshot with a unique hash. This is essential for later retrieving a specific feature or bug‑fix.
4. Push (optional)
git push origin <branch-name>
Uploads your commits to a remote repository (e.g., GitHub, GitLab) so the project is tracked off‑site.
Branches – Working on Multiple Lines of Development
A branch is simply a pointer to a commit.
Git creates a default master (or main) branch, but you should create additional branches for:
- Feature development – work on new functionality without disturbing the stable code.
- Testing – verify changes before merging them into the main line.
- Collaboration – each developer can have their own branch, reducing the chance of conflicts.
Example:
If you and a friend are building a Todo app, you might have three branches:
dev‑alice– your workdev‑bob– your friend’s worktest– integration testing before merging intomain
For more details, see the official documentation.
Viewing History
-
Full log – shows all metadata and hashes
git log -
One‑line summary – shows only the short hash and commit message
git log --oneline
Copy the hash of the commit you want to revisit (e.g., 01xyz2).
Checking Out an Older Version
Detached HEAD (temporary view)
git checkout 01xyz2
Shows the project as it was at that commit, but you are in a detached HEAD state (not on any branch).
Create a New Branch from that Commit
git checkout -b 01xyz2
- Checks out the old version and creates a new branch pointing to it, so you stay on a proper branch instead of a detached HEAD.
Going Back Permanently
After inspecting the old code you have two options:
-
Leave the repository as‑is – just use the old code for reference.
-
Reset the current branch to that commit (dangerous – rewrites history).
# Move the current branch pointer to the old commit git reset --hard 01xyz2Use
--hardonly if you are sure you want to discard all newer commits.
TL;DR
- Git stores only the changes in a compressed format, keeping history lightweight.
- Every commit is a snapshot identified by a SHA‑1 hash.
- Use
git init,git add,git commit, andgit pushto start tracking. - Branches let multiple developers work in parallel without stepping on each other’s toes.
git logandgit checkoutlet you explore and revert to any previous version.
Happy coding! 🚀
Git reset options – a quick overview
| Command | What it does | Effect on your work‑tree & index |
|---|---|---|
git reset --soft <commit> | Moves HEAD to the specified commit but leaves all changes you made after that commit staged (in the index). | If you commit now, the new commit will contain all the changes that were present in the latest commit plus the ones you staged. |
git reset --mixed <commit> (or simply git reset <commit>) | Moves HEAD to the specified commit and unstages any changes that were staged after that point. | Your working directory stays unchanged, but the changes are no longer staged. You can edit, stage, and commit them again as you wish. |
git reset --hard <commit> | Moves HEAD to the specified commit and discards all changes made after that commit, both staged and unstaged. | All modifications that were not part of the commit are permanently removed from the working tree. Use with caution! |
How to backtrack safely
- Identify the commit you want to return to (e.g., via
git logor a visual tool). - Choose the appropriate reset mode:
- Use
--softif you want to keep everything staged for a new commit. - Use
--mixedif you want to keep the changes but start with a clean index. - Use
--hardonly when you are sure you can discard the later work.
- Use
- Run the chosen command.
Collaboration tip
When working with others (e.g., on a shared “Todo” repository), avoid rewriting public history:
- Never
git reset --hardon commits that have already been pushed to a shared branch. - If you need to undo a public commit, consider using
git revertinstead; it creates a new commit that undoes the changes without rewriting history.
By understanding these three reset modes, you can confidently navigate between versions of your code while minimizing the risk of accidental data loss or merge conflicts.