πŸš€ From Version Control to Automation: Git, GitHub, and GitHub Actions Explained πŸ’»βš™οΈ

SachinSachin
7 min read

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

CommandDescription
git initInitialize a new Git repository
git cloneCopy a repo from GitHub to your machine
git statusCheck the current status of your repo
git add .Add changes to staging
git commit -m ""Save the changes with a message
git pushUpload your changes to GitHub
git pullFetch 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

CommandDescription
git branchList 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 stashSave your uncommitted changes temporarily
git stash applyReapply 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 --graphVisualize your branch history in a pretty graph
git diff <branch1> <branch2>Show the difference between two branches
git remote -vShow the remote repository URLs
git fetchFetch 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 FileWhen It RunsExample Use
pre-commitBefore a commit is executedRun linting or tests
commit-msgAfter commit message is enteredEnforce commit message style
pre-pushBefore pushing to remoteRun tests or check code quality
post-mergeAfter merging a branchInstall 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 πŸ˜‰

42
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!