DEV Community

Cover image for Git Commands: The Developer's Power Tools 🚀
Sanchit Bajaj
Sanchit Bajaj

Posted on

4 1 1 1 1

Git Commands: The Developer's Power Tools 🚀

Hey Developers 👋 ever lost hours of work because you accidentally deleted code? Or struggled to merge changes from multiple team members without creating chaos? Welcome to the world before Git.

Git changed everything. This brilliant version control system has become the backbone of modern software development, tracking every change with precision and enabling seamless collaboration across time zones and teams. What once required elaborate backup systems and painful manual merges now happens with a few simple commands.

In this guide, we'll explore 70 essential Git commands that will supercharge your development workflow. From beginner to advanced, these commands are organized as levels in your Git mastery journey. Let's power up your development skills! ⚡

🌱 Level 1: Git Basics - First Steps

These commands acts as a stepping stone to your git setup and initialization.

  • git init: Creates a new Git repository in your current directory. This command gives birth to the magical .git folder that makes version control possible.

  • git clone [url]: Downloads a complete copy of a remote repository to your local machine. Not just the latest version—you get the entire history of the project!

  • git add [file/directory]: Stages changes for commit, adding them to Git's "staging area." Think of it as preparing your changes for their official snapshot.

  • git commit -m "[message]": Records the staged snapshot permanently in version history. Each commit creates a unique ID that lets you revisit this exact state later.

  • git status: Displays the state of your working directory and staging area. Your project's health check—shows modified files, staged changes, and more.

  • git diff: Shows changes between commits, commit and working tree, etc. The detective tool that reveals exactly what changed, line by line.

  • git checkout [branch/commit]: Switches branches or restores working tree files. Your time machine for moving between different versions of your project.

  • git reset [file/commit]: Undoes changes to files in the working directory. Your "undo" button for when things go wrong.

  • git log: Displays the commit history for the current branch. Your project's journal—see who did what and when.

  • git show [commit]: Shows various types of objects (commits, tags, etc.). Reveals the details behind any commit, tag, or reference.

  • git tag [name]: Creates, lists, deletes, or verifies tags. Perfect for marking release points like "v1.0" or "beta-release".

  • git push [remote] [branch]: Updates remote refs along with associated objects. Shares your local commits with the world!

  • git pull: Fetches from and integrates with another repository or branch. Gets you up to date with the latest changes from your team.

🌿 Level 2: Branching - Parallel Development

Developing things parallelly will boost your work and changes made in isolation does not impact the entire application if issue occurs.

  • git branch: Lists, creates, or deletes branches. Your map of the different development paths in your project.

  • git checkout -b [branch-name]: Creates a new branch and switches to it immediately. A fast track to start working on a new feature or fix.

  • git merge [branch]: Joins two or more development histories together. Brings the changes from another branch into your current branch.

  • git rebase [branch]: Reapplies commits on top of another base tip. Creates a cleaner history by moving your branch to the tip of another branch.

  • git branch --set-upstream-to [remote/branch]: Sets the default remote branch for the current local branch. Establishes a tracking relationship for easier pushing and pulling.

  • git branch --unset-upstream: Removes the upstream information for the current branch. Disconnects your branch from its remote counterpart.

  • git cherry-pick [commit]: Applies the changes from specific commits to the current branch. Selectively brings changes from one branch to another.

🔄 Level 3: Merging - Combining Work

After parallel development, merging is a key concept to gather and merge all changes on single main branch.

  • git merge [branch]: Incorporates changes from the named branch into the current branch. The collaborative bridge that brings different lines of development together.

  • git rebase [branch]: Reapplies your changes on top of another branch's changes. Creates a cleaner, more linear project history.

📦 Level 4: Stashing - Save Works in Progress

When something occurs and you’re not sure whether to commit this or not, stash will save your progress.

  • git stash: Temporarily stores all modified tracked files. Like pressing pause on your work without committing half-finished code.

  • git stash pop: Applies the stashed changes and removes them from the stash. Returns to your paused work right where you left off.

  • git stash list: Lists all stashed change sets. Shows all your saved work-in-progress snapshots.

  • git stash apply: Applies the stashed changes without removing them. Useful when you want to use the same stashed changes multiple times.

  • git stash drop: Discards the specified stash. Cleans up your stash list when you no longer need saved changes.

🌐 Level 5: Remotes - Connecting with Others

Making remote connections give access to centralized project’s hub to build, enhance and collaborate into others’ work.

  • git remote: Manages the set of tracked repositories. Your directory of project connections.

  • git remote add [name] [url]: Adds a new remote repository. Establishes a new connection with another repository.

  • git remote remove [name]: Removes a remote from your configuration. Cuts ties with a remote repository when you no longer need it.

  • git fetch [remote]: Downloads objects and refs from another repository. Gets remote updates without merging them into your work.

  • git pull [remote] [branch]: Fetches and merges changes from a remote repository. Brings remote changes directly into your working branch.

  • git push [remote] [branch]: Updates remote refs and associated objects. Publishes your local commits to a remote repository.

  • git clone --mirror [url]: Creates a mirror clone of a repository. Makes an exact copy including all references and objects.

⚙️ Level 6: Configuration - Customizing Git

Configuring git according to your project make defines simplicity in the process.

  • git config [key] [value]: Sets a configuration variable for your repository. Customizes how Git behaves for this specific project.

  • git config --global [key] [value]: Sets global Git configuration. Changes settings for all your Git repositories at once.

  • git reset config [key]: Resets a configuration variable to its default value. Returns a setting to its original state.

🔧 Level 7: Plumbing - Low-Level Operations

Plumbing out the low level operations with a few commands is where git shines.

  • git cat-file [type] [object]: Provides content or type and size information for repository objects. Peek inside Git's internal object structure.

  • git checkout-index [options] [file]: Copies files from the index to the working tree. Useful for scripts that need to extract specific files.

  • git commit-tree [tree] -p [parent] -m [message]: Creates a new commit object from a tree object. A low-level way to create commits with precise control.

  • git diff-tree [options] [commit] [commit]: Compares the content and mode of blobs between tree objects. Examines differences between specific commits at a detailed level.

  • git for-each-ref [options] [pattern]: Outputs information on each ref that matches a pattern. Powerful for scripting operations across multiple references.

  • git hash-object [file]: Computes the object ID value for a file. Shows what Git would store for a given file.

  • git ls-files [options]: Shows information about files in the index and working tree. Lists files Git is tracking and their current status.

  • git ls-remote [repository]: Lists references in a remote repository. See what branches and tags exist on a remote without cloning.

  • git merge-tree [base-tree] [branch1] [branch2]: Shows three-way merge without touching the index. Previews merge results without actually performing the merge.

  • git read-tree [options] [tree-ish]: Reads tree information into the index. Updates the staging area with content from a specific tree.

  • git rev-parse [options] [args]: Picks out and massages parameters for other Git commands. The Swiss Army knife for Git scripting.

  • git show-branch [options]: Shows branches and their commits. Visualizes branch history and relationships.

  • git show-ref [options]: Lists references in a local repository. Displays the commit that each reference points to.

  • git symbolic-ref [name] [ref]: Reads, modifies, and deletes symbolic refs. Manages references like HEAD in more flexible ways.

  • git tag --list [pattern]: Lists existing tags that match a pattern. Find tags that mark specific versions or features.

  • git update-ref [options] [ref] [value]: Updates the object name stored in a ref safely. Low-level command for updating references.

🔬 Level 8: Porcelain - User-Friendly Commands

Some user friendly commands to simplify your day-to-day tasks.

  • git blame [file]: Shows what revision and author last modified each line of a file. Find out who to thank (or ask) about specific code changes.

  • git bisect start [bad] [good]: Uses binary search to find the commit that introduced a bug. Like a detective efficiently tracking down when something broke.

  • git checkout [branch/commit] [path]: Switches branches or restores working tree files. Navigate through your project's history with precision.

  • git commit [options]: Records changes to the repository. Captures a snapshot of your project's currently staged changes.

  • git diff [options] [commit] [commit]: Shows changes between commits, working tree, etc. The detective tool that reveals exactly what changed, line by line.

  • git fetch [remote] [branch]: Downloads objects and refs from another repository. Retrieves updates without affecting your working directory.

  • git grep [pattern]: Prints lines matching a pattern in your codebase. Find that needle in the haystack of your entire project.

  • git log [options]: Shows the commit logs in various formats. Explore your project's history with flexible filtering and formatting.

  • git merge [options] [branch]: Joins two or more development histories together. Combines separate lines of development.

  • git push [options] [remote] [branch]: Updates remote refs along with associated objects. Sends your local commits to a remote repository.

  • git rebase [options] [branch]: Reapplies commits on top of another base tip. Rewrite history to create a cleaner, more linear project history.

  • git reset [options] [commit]: Resets current HEAD to the specified state. Your time machine for undoing changes at different levels.

  • git show [object]: Shows various types of objects (commits, tags, etc.). Reveals the details of any Git object in readable format.

  • git tag [options] [name] [commit]: Creates, lists, deletes, or verifies a tag object. Mark important points in your project history.

🧙‍♂️ Level 9: Shortcuts - Aliases

Make customize shortcuts commands for quick access.

  • git config --global alias.<alias> <command>: Creates a shortcut for a Git command. Transform lengthy commands into personal shortcuts (e.g., git co for git checkout).

⚙ Level 10: Automation - Hooks

Trigger your custom actions by creating your automation hooks.

  • git config --local core.hooksPath <path>: Sets the path to your project's hooks. Customizes where Git looks for hook scripts that trigger on specific actions.

🚀 Going Beyond: Practical Git Workflows

Now that you've leveled up your Git command knowledge, consider incorporating these commands into common workflows:

Feature Branch Workflow

  1. Create a branch: git checkout -b feature-name

  2. Make changes and commit: git add . and git commit -m "Add feature"

  3. Push to remote: git push origin feature-name

  4. Create pull request (via web interface)

  5. Merge after review: git checkout main and git merge feature-name

Hotfix Workflow

  1. Create hotfix branch: git checkout -b hotfix-issue

  2. Fix bug and commit: git commit -am "Fix critical bug"

  3. Push to remote: git push origin hotfix-issue

  4. Merge to main: git checkout main and git merge hotfix-issue

  5. Tag the release: git tag -a v1.0.1 -m "Hotfix release"

🏆 Conclusion: Your Git Journey Continues

Congratulations! You've just explored 70 powerful Git commands that will serve you throughout your development career. From basic operations to advanced techniques, you now have a comprehensive toolkit for managing code with confidence.

Remember, mastering Git is a journey. Start with the basic commands and gradually incorporate more advanced ones as your confidence grows. Each command you learn unlocks new potential in your development workflow.

Now go forth and commit with confidence! Your code—and your future self—will thank you. 💪

What's your favorite Git command? Share your Git tips in the comments below! and don’t forget to bookmark this for future reference

Connect with the author:

Top comments (0)