Read About my Experience on GDG Cloud Event : - Click here to Read !

Boost Your Git Skills for Any Opensource project - git commands cheat sheet

post is all about the git cheat sheet , git workflow, git commit github workflows and some advance tips for developer
git cheat sheet thumbnail

Greetings, aspiring code maestro! Are you ready to embark on a magical Hacktoberfest adventure that will transform your GitHub skills from novice to virtuoso? If you've been yearning for a mystical guide to navigate the enchanted realm of open-source contributions, you've just stumbled upon the holy grail of GitHub wisdom.

It's time to dust off your code editor and get ready to metamorphose your GitHub prowess from merely adequate to absolutely extraordinary.

I know long time no see but from now onwards i will be posting some cool stuffs which i have been preparing for long time.

Buckle up, because we're about to embark on a thrilling journey that will elevate you from a humble code apprentice to a legendary Hacktoberfest champion. Are you prepared to unlock the hidden potential within you and emerge as the open-source hero you've always aspired to be? Let's dive into this magical world of Git commands.

Configure name and email with Git

if you are a beginner into this git and github things and if you just install the git for first time or say on a new machine then you need to configure some data to use the git into your local system. Here are the few steps which you can follow copy paste the below command into your any terminal and make sure you change the user.name and user.email with your actual details

# set name and email
git config --global user.name "paruidev"
git config --global user.email "pratap@paruidev.com"

# set default branch name (optional)
git config --global init.defaultBranch main

Simple Git Workflow

Cloning a Repository

Clone a remote repository to your local machine, bringing the entire project history and all its branches to your computer. This allows you to work on the project offline and contribute changes back to the original repository.

git clone <repository-url>

You can copy the repository URL from the repo's GitHub page.

Check Repository Status

Check the status of your working directory to see which files have been modified, added, or deleted since your last commit. This command provides a snapshot of your current project state, helping you track changes and prepare for staging.

git status

This command shows which files are changes, which ones are added for committing, etc.

Adding Changes

Stage changes by adding files to the staging area in preparation for committing. This crucial step allows you to selectively choose which modifications you want to include in your next commit, giving you fine-grained control over your version history. By staging changes, you can review and organize your work before permanently recording it in the repository.

git add <file-name>

Commit Changes

After staging, commit changes with a meaningful message.

git commit -m "Commit message"

Viewing Commit History

Check your commit history.

git log

For a simpler, one-line format:

git log --oneline

Push Changes to Remote

Push committed changes to the remote repository.

git push

Create Branches and Merge it

Branching allows you to work on different parts of your project independently. Here's how to manage branches:

Create a New Branch

Create a new branch for your feature or bug fix.

git switch -c <branch-name>

If you just want to create the new branch and don't want to switch to it:

git branch -c <branch-name>

Switch to Another Branch

if you want to switch to another branch

git switch <branch-name>

List All Branches

See all branches in your repository.

git branch

Merge a Branch

Merge changes from one branch into your current branch.

git merge <branch-name>

Delete a Branch

Once a branch is no longer needed, you can delete it. but make sure once it done it can’t be revert back.

# Locally
git branch -d <branch-name>

# Remotely
git push origin --delete <branch-name>

Save Code in Stashing.

Stashing temporarily stores your changes without committing them.

Save Changes to a Stash

If you're working on something and want to switch branches without committing:

git stash

Apply Stash

To retrieve your changes later:

git stash apply

List Stashed Changes

If you have multiple stashes, you can see them with:

git stash list

Advanced Command: Git Rebase

What is Git Rebase? 🤔

In simple terms, Git Rebase helps you move or apply your changes from one branch onto another branch in a linear fashion. The main advantage of using rebase over merge is that it avoids unnecessary merge commits, giving your project history a much cleaner look. Think of it as rearranging your project timeline so that it appears as if you’ve built everything in sequence!

Why Use Git Rebase?

Here’s why Git Rebase is so loved by developers:

  • Keeps your commit history clean and linear.
  • Avoids merge commits, which can make your history messy.
  • Gives you full control over your project’s commit history.

That sounds great, right? Let’s look at the essential Git Rebase commands now.

1. How to Start a Basic Rebase

Let’s say you have a feature branch, and you want to bring in the latest changes from main (or master). Here’s how to do it:

git checkout <branch-to-rebase>
git rebase <target-branch>
  • First, you checkout your feature branch.
  • Then, you rebase it onto the target branch, which is usually main.

💡 Pro Tip 1: Before rebasing, make sure to pull the latest changes from the main branch to avoid conflicts!

💡 Git Tip: The git checkout -- <filename> command discards changes made to a specific file, restoring it to its state from the last commit. This is handy for reverting one file without affecting others. Just replace <filename>with the file name you want to reset.

2. Interactive Rebase – Your History Rewriter

Want to go back in time and rewrite some history? That's where interactive rebase comes in handy.

git rebase -i <commit>

This command allows you to:

  • Pick the commits you want.
  • Squash multiple commits into one.
  • Reword commit messages.

When you run this command, Git opens an editor where you can decide how to handle each commit. It’s a powerful way to clean up your project!

3. Oops, Something Went Wrong? Abort the Rebase

We’ve all been there—sometimes things don’t go as planned. If you find yourself in a tricky situation while rebasing, you can simply abort the process:

git rebase --abort

This will take you back to the state before you started the rebase, and you can start fresh.

4. Skip a Commit During Rebase

Let’s say there’s a commit causing issues, and you can’t resolve the conflict right now. You can skip it and continue with the rebase:

git rebase --skip

Use this when you hit a snag with one specific commit but want to keep moving forward.

5. Continue After Resolving Conflicts

If conflicts arise during a rebase (and they will, trust me 😅), you can resolve the conflicts manually. Once done, continue the rebase process with:

git rebase --continue

This tells Git that you’ve resolved the issues and are ready to proceed with the rest of the rebase.

6. Rebase a Feature Branch Onto Main

This is one of the most common rebasing scenarios. When your feature branch has fallen behind main, you can rebase your changes onto the latest version of main.

git checkout feature-branch
git rebase main

This brings in all the new changes from main without creating unnecessary merge commits.

7. Use Auto squash to Clean Up Your Commits

If you have commits that are meant to fix or clean up earlier commits, you can tell Git to handle those automatically. This is super helpful when you want to tidy things up before merging:

git rebase -i --autosquash <base-branch>

Git will automatically squash those "fixup" commits into the ones they’re related to. Nice and neat!

Few Tips for Intermediate Developers 🚀

So, you’ve got the basics of Git and GitHub down, and you’re starting to feel more comfortable with your development workflow. That’s awesome! But what if I told you there are ways to level up even further? Whether you’re trying to automate tasks, create professional project documentation, or dive into more complex integrations, these advanced GitHub tips are here to help you stand out.

Let’s dig into some cool features that can take your skills to the next level!

1. GitHub Actions: Automate Workflows to Boost Efficiency

Do you ever wish that some of the repetitive tasks in your workflow could just do themselves? Well, with GitHub Actions, they totally can!

GitHub Actions let you automate all sorts of tasks directly from your repository. You can set up actions to run tests, deploy code, build projects, or even notify your team when something changes. The best part? You can trigger these actions based on specific events, like pushing code to a branch or opening a pull request.

Here’s a quick look at what you can automate with GitHub Actions:

  • Continuous Integration (CI): Automatically run tests on your code every time you push a new commit.
  • Continuous Deployment (CD): Deploy your app whenever changes are merged into the main branch.
  • Automated issue triage: Automatically label or assign issues based on keywords.

Setting up GitHub Actions is super straightforward:

name: CI Pipeline

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '18'

      - name: Install dependencies
        run: npm install

      - name: Run tests
        run: npm test

💡 Pro Tip 2: You can explore and use pre-built actions from GitHub’s marketplace to save time and effort.

2. GitHub Pages: Create Project Documentation to Enhance User Experience

Let’s be real—great documentation can make or break a project. Whether it’s for open-source users or your team, you want to make sure people can easily understand and use your work.

Enter GitHub Pages! This feature allows you to host static websites directly from your repository. You can use it to create:

  • Project documentation: Think README.md on steroids.
  • Portfolios: Perfect if you want to showcase your personal projects.
  • Demo sites: Want to show a working example of your project? This is the way to go.

Setting up GitHub Pages is a breeze. You can even choose from a variety of Jekyll themes to make your documentation look polished and professional.

Here’s how to get started with GitHub Pages:

  1. Go to your repository’s settings.
  2. Scroll down to the GitHub Pages section.
  3. Select the branch you want to publish from (often main or gh-pages).
  4. Save and boom—you’ve got a live site!

You can even customize the domain and URL for a more professional feel.

💡 Pro Tip 3: Use GitHub Pages in conjunction with a custom domain to give your documentation a sleek, branded touch.

3. GitHub API: Explore Integrations for More Complex Projects

If you’re looking to take on more complex, technical challenges, the GitHub API is where you want to be. The API allows you to programmatically interact with your GitHub repositories, issues, pull requests, and much more.

The possibilities with the GitHub API are endless. Here’s a few things you can do:

  • Automate your workflows: Fetch repository data, monitor pull requests, and even trigger builds all through the API.
  • Create custom dashboards: Build tools that visualize the status of your repositories or monitor project progress.
  • Integrate GitHub with other tools: Seamlessly connect your GitHub repositories to external services like Slack, Jira, or CI/CD pipelines.

Let’s look at a simple example of using the GitHub API to fetch the latest issues in a repository:

curl -H "Authorization: token YOUR_ACCESS_TOKEN" \\
  <https://api.github.com/repos/:owner/:repo/issues>

This call returns a list of issues in a specific repository, including their titles, statuses, and descriptions. Pretty cool, right?

💡 Pro Tip 4: Use the Octokit library to simplify your API calls. It’s GitHub’s official SDK, and it makes integrating the GitHub API into your applications a breeze.

Conclusion

By mastering these advanced GitHub tips—GitHub Actions, GitHub Pages, and the GitHub API—you can elevate your development workflow and take on more complex, impactful projects. Whether you’re automating repetitive tasks, making your documentation more accessible, or creating custom integrations, these tools are a must-have in your skill set.

So, what’s next for you? Are you ready to automate your workflows with GitHub Actions, or maybe set up your own GitHub Pages site? Whatever it is, remember that these tools are here to make your life easier and your projects even more awesome.

Let me know how you’re planning to use these tips in your projects.

Hey! 👋 I’m Pratap, a Frontend Developer with 2+ years of experience crafting digital experiences through code and design. I specialize in frontend web development and UI/UX design, Through this blog…

Post a Comment