Git Cheat Sheet: Commands Every Developer Uses Daily

Git Cheat Sheet: Commands Every Developer Uses Daily




Git Cheat Sheet: Commands Every Developer Uses Daily

Git Cheat Sheet: Commands Every Developer Uses Daily

Git is the backbone of modern development workflows, and mastering essential commands will dramatically improve your productivity. Whether you’re pushing code, managing branches, or fixing mistakes, these daily-use commands are what separate efficient developers from those constantly searching documentation.

Essential Git Configuration and Setup Commands

Before diving into complex workflows, you need to configure Git properly on your system. These foundational commands ensure your commits are attributed correctly and your Git environment works seamlessly.

Start with user configuration:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global core.editor "vim"

These commands set your identity for all repositories on your machine. The --global flag applies settings system-wide. If you need repository-specific settings, omit the flag and run the command inside your project directory.

To verify your configuration:

git config --list
git config user.name

You can also generate SSH keys for authentication, which is essential for secure GitHub and GitLab operations:

ssh-keygen -t ed25519 -C "[email protected]"
cat ~/.ssh/id_ed25519.pub

Copy the public key output to your Git hosting platform’s SSH settings. This eliminates the need to enter credentials repeatedly.

Daily Workflow Commands for Commits and Branches

Most of your day revolves around creating, switching, and committing to branches. These commands form the core of your development routine.

Cloning and initializing repositories:

git clone https://github.com/username/repo.git
git init

Creating and switching branches:

git branch feature/login-system
git checkout feature/login-system
git checkout -b feature/login-system

The last command combines branch creation and switching in one operation—a real time-saver. List all branches with:

git branch
git branch -a

The -a flag shows both local and remote branches, useful when tracking what teammates have pushed.

Staging and committing your changes:

git add file.js
git add .
git add *.js
git commit -m "Add login validation"
git commit -am "Fix authentication bug"

The -a flag stages all modified files before committing, skipping the separate add step for tracked files. Use descriptive commit messages—your future self will thank you.

Viewing your work:

git status
git log
git log --oneline
git diff
git diff HEAD~1

git log --oneline displays recent commits in compact format, perfect for quick history reviews. git diff shows exactly what changed in unstaged files, while git diff HEAD~1 compares your current state to the previous commit.

Advanced Commands for Fixing Mistakes and Merging

Mistakes happen in development. Git provides powerful commands to undo changes, rewrite history safely, and merge work from multiple developers.

Undoing changes:

git restore file.js
git checkout -- file.js
git reset HEAD~1
git reset --soft HEAD~1
git reset --hard HEAD~1
git revert HEAD

git restore is the modern approach to discard changes in working files. git reset moves your branch pointer—use --soft to keep staged changes, --hard to discard everything. git revert creates a new commit that undoes a previous one, which is safer for shared branches.

Merging and pulling changes:

git merge main
git pull origin main
git pull --rebase origin main
git fetch

git fetch downloads remote changes without merging, letting you review before integrating. git pull combines fetch and merge. Using --rebase replays your commits on top of remote changes, creating a cleaner history than merge commits.

Pushing your work:

git push origin feature/login-system
git push -u origin feature/login-system
git push --force-with-lease
git push origin --delete feature/login-system

The -u flag tracks the remote branch, so future pushes require just git push. --force-with-lease safely rewrites history without overwriting teammates’ work—prefer this over --force.

Stashing work in progress:

git stash
git stash list
git stash pop
git stash drop

Stashing temporarily saves changes without committing, essential when you need to switch context quickly. Pop restores the latest stash, and drop removes it permanently.

How to Use the Code Snippet Manager

Developers often need to quickly store, organize, and retrieve Git commands and scripts they use frequently. The Code Snippet Manager on DevUtilityPro helps you build a personal repository of Git commands, shell aliases, and workflow scripts. Save your most-used Git commands with explanations, organize them by category, and access them instantly when you need them. This transforms scattered notes and browser bookmarks into a centralized knowledge base that boosts your development speed.

FAQ

What’s the difference between git reset and git revert?

git reset moves your branch pointer backward, rewriting history. It’s dangerous on shared branches because teammates’ work might conflict. git revert creates a new commit that undoes changes, preserving history. For public branches, always use git revert. Use git reset only on local branches before pushing.

How do I fix a commit message before pushing?

Use git commit --amend to edit the most recent unpushed commit:

git commit --amend -m "Corrected commit message"

If you’ve already pushed, use git revert or carefully use git push --force-with-lease only if no one else has based work on that commit.

When should I use git rebase versus git merge?

Use git merge on shared branches to preserve history and avoid surprises. Use git rebase on feature branches before pushing to keep history linear and clean. Never rebase commits that others have pulled—it creates duplicate commits and confusion. The golden rule: rebase local work, merge shared work.


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top