π From Version Control to Automation: Git, GitHub, and GitHub Actions Explained π»βοΈ


Introduction:
Imagine writing hundreds of lines of code and accidentally deleting the working part. Oops. π Now, do you wish there was an Undo button? Well, meet Git β the real superhero of developers (minus the cape but with plenty of confusing commands).
And where does this superhero chill? At GitHub β the social media for your code, where projects live, bugs hide, and everyone pretends they know what theyβre doing. π
Together, Git and GitHub save developers from disasters, messy code, and fights like: "Who broke the code?"
Thanks to them, you can track every tiny change, show off your skills, and collaborate like pros (or at least look like one).
Buckle up! In this blog, weβll explore Git and GitHub β the dynamic duo every developer swears by (after coffee β). Letβs dive in before you lose your code again. π
What is Git? π οΈ
Git is a Version Control System that helps developer track changes in their code.
Think of it as a time machine for your project - you can go back to any version if something breaks.
π₯ Why Use Git?
β
Track every change
β
Collaborate with teammates
β
Roll back if needed
β
Work on new features without messing up the main code
Basic Git Commands
Command | Description |
git init | Initialize a new Git repository |
git clone | Copy a repo from GitHub to your machine |
git status | Check the current status of your repo |
git add . | Add changes to staging |
git commit -m "" | Save the changes with a message |
git push | Upload your changes to GitHub |
git pull | Fetch and merge changes from GitHub |
What is GitHub? π
GitHub is a cloud platform built around git where developers:
βοΈ Store code
βοΈ Share projects
βοΈ Collaborate with others
βοΈ Handle issues and bugs
βοΈ Show off their work to the world (and future employers π)
Main Features:
Repositories (Repos): Where your code lives
Forks & Pull Requests: Collaborate without messing up the original repo
Issues: Track bugs, tasks, and new features
Projects: Organize your work like a pro
How GitHub Actions Work:
GitHub Actions run based on Workflows written in YAML and stored in .github/workflows/
folder.
Sample GitHub Action Workflow
yamlCopyEditname: Deploy on Push π
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
- name: Deploy
run: echo "Deploying your app..."
Whenever you push to the main branch, this workflow:
βοΈ Checks out your code
βοΈ Installs dependencies
βοΈ Runs tests
βοΈ Deploys the app
Easy, right?
Benefits of Using Git, GitHub, and GitHub Actions Together πͺ
β
Full version control and project history
β
Smooth collaboration with teammates
β
Automate builds, tests, and deployments
β
Open-source contributions made easy
β
Show off your work to recruiters and the developer community
π Advanced Git Commands You Should Know
Command | Description |
git branch | List all branches in your repo |
git branch <branch-name> | Create a new branch |
git checkout <branch-name> | Switch to a specific branch |
git checkout -b <branch-name> | Create and switch to a new branch |
git merge <branch-name> | Merge the specified branch into your current branch |
git rebase <branch-name> | Reapply commits on top of another base branch (cleaner history) |
git stash | Save your uncommitted changes temporarily |
git stash apply | Reapply the stashed changes |
git reset --hard <commit-hash> | Reset your repo to a specific commit, discarding changes |
git revert <commit-hash> | Undo a commit by creating a new reverse commit |
git cherry-pick <commit-hash> | Apply a specific commit from another branch |
git log --oneline --graph | Visualize your branch history in a pretty graph |
git diff <branch1> <branch2> | Show the difference between two branches |
git remote -v | Show the remote repository URLs |
git fetch | Fetch changes from remote (without merging) |
git pull origin <branch> | Pull latest changes from a specific remote branch |
git push origin <branch> | Push your branch to the remote repository |
git tag <tag-name> | Create a new tag (release marker) |
git show <tag-name> | Show the details of the given tag |
πͺ Git Hooks (Automate tasks like a Pro)
Git hooks are scripts that run automatically on certain Git actions like commit, push, merge, etc.
They are stored in the .git/hooks/
directory.
Hook File | When It Runs | Example Use |
pre-commit | Before a commit is executed | Run linting or tests |
commit-msg | After commit message is entered | Enforce commit message style |
pre-push | Before pushing to remote | Run tests or check code quality |
post-merge | After merging a branch | Install dependencies |
Example: Add this to .git/hooks/pre-commit
to block commits if npm test
fails:
#!/bin/sh
npm test || exit 1
Donβt forget to make it executable: chmod +x .git/hooks/pre-commit
π£ Bonus Advanced Tips:
β
Use stash when switching branches with uncommitted work
β
Rebase for cleaner history but be careful on shared branches
β
Cherry-pick when you need just one commit from another branch
β
Reset --hard = β οΈ dangerous but powerful
π§ Now Let's See the Practical of These Advanced Commands
1. Creating and Switching Branches
bashCopyEditgit branch feature-login # Create a new branch
git checkout feature-login # Switch to that branch
OR directly:
bashCopyEditgit checkout -b feature-login
2. Stashing Changes (Temporary Save)
bashCopyEditgit stash # Stash your current uncommitted changes
git stash list # See all your stashed changes
git stash apply # Re-apply the latest stash
git stash drop # Remove the applied stash
3. Merging Branches
bashCopyEditgit checkout main # Switch to main branch
git merge feature-login # Merge feature-login branch into main
4. Rebasing (Cleaner History)
bashCopyEditgit checkout feature-login
git rebase main
Note: Be careful with rebase on shared branches!
5. Cherry-Pick a Specific Commit
bashCopyEditgit checkout main
git cherry-pick <commit-hash> # Apply a specific commit from another branch
6. Resetting to a Previous Commit
bashCopyEditgit reset --hard <commit-hash> # Danger zone: removes changes after the hash
7. Viewing a Beautiful Commit Graph
bashCopyEditgit log --oneline --graph --all --decorate
8. Setting Up a Simple Git Hook Example
π Go to .git/hooks/
directory
π Create or edit pre-commit
file:
bashCopyEdit#!/bin/sh
echo "Running tests before commit..."
npm test || exit 1
π Make it executable:
bashCopyEditchmod +x .git/hooks/pre-commit
Now, every time you run git commit
, it will automatically run your tests! β
Conclusion
Git, GitHub, and GitHub Actions together form a powerful combo every developer must master. π»βοΈ
From tracking your changes with Git, collaborating globally with GitHub π, to automating tasks using GitHub Actions π€ β these tools make development faster, smarter, and smoother.
Once you get the hang of basic commands, exploring advanced stuff like branches, stashing, cherry-picking, and hooks will turn you into a Git Ninja π₯·.
And trust me, automation with GitHub Actions is like having your personal assistant doing all the boring tasks while you relax. βπ
So, what are you waiting for?
Push your first repo, set up that CI pipeline, and level up your dev journey! πͺβ¨
π Waitβ¦ Before You Go!
Hey folks, Iβve been talking about Git and GitHub so muchβ¦ how can I forget to share my own GitHub profile? ππ»
π Follow me on GitHub: sachinxsharma
Letβs grow together, collaborate on cool projects, and level up our coding game! ππ₯
Donβt be shy β drop a β on my repos if you find them helpful π
Social Links
Instagram π»: instagram.com/sachinnn_72
Portfolio π¦βπ₯: sachin-sharma-portfolio.vercel.app
Hashnode: s72tech.hashnode.dev
Subscribe to my newsletter
Read articles from Sachin directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Sachin
Sachin
Hello Devs, I'm Sachin Sharma, a graduate student currently pursuing my master's degree. During my studies, I've decided to dive into DevOps and aim to land my first job in the field by January 2025. I'll be sharing my learning journey and experiences along the way. If you guys have any questions, feel free to ask!