DEV Community

M.T.Ramkrushna
M.T.Ramkrushna

Posted on

πŸš€ The Only Git Cheat Sheet Every Developer Should Know (With One-Line Use Cases)

πŸ”§ Getting Started

git init β€” Create a new Git repository in the current folder.
git clone <url> β€” Download a repository from GitHub/GitLab to your machine.
git config --global user.name "Your Name" β€” Set your Git username.
git config --global user.email "example@mail.com" β€” Set your Git email.


πŸ“Œ Daily Workflow Commands

git status β€” Show which files are modified or staged.
git add <file> β€” Stage a specific file for commit.
git add . β€” Stage all modified files.
git commit -m "message" β€” Save staged changes with a message.
git push β€” Upload your commits to the remote repository.
git pull β€” Fetch & merge the latest changes from remote.


🌿 Branching Essentials

git branch β€” List all local branches.
git branch <name> β€” Create a new branch.
git checkout <branch> β€” Switch to a specific branch.
git checkout -b <branch> β€” Create a branch and switch to it.
git merge <branch> β€” Merge the given branch into your current branch.

βœ‚οΈ Deleting Branches

git branch -d <branch> β€” Delete a branch that’s fully merged.
git branch -D <branch> β€” Force delete an unmerged branch.


♻️ Fixing Mistakes & Undoing Changes

πŸ”„ Undo Changes

git restore <file> β€” Discard local modifications in a file.
git restore --staged <file> β€” Unstage a file while keeping changes.

πŸ›‘ Abort a Merge

git merge --abort β€” Cancel an ongoing merge and revert to previous state.

✏️ Edit Last Commit Message

git commit --amend β€” Modify the message of the latest commit.


βͺ Undoing Commits (Reset, Revert)

πŸ”™ Reset (Go Back in History)

git reset --soft HEAD~1 β€” Undo last commit but keep changes staged.
git reset --mixed HEAD~1 β€” Undo last commit, keep changes unstaged.
git reset --hard HEAD~1 β€” Undo last commit and delete changes forever.
git reset --hard origin/<branch> β€” Reset your local branch to match remote.

πŸ”„ Revert (Safe Undo)

git revert <commit> β€” Create a new commit that reverses a previous one.


🧽 Squashing & Cleaning Commit History

Squash Multiple Commits

git rebase -i HEAD~<n>
Enter fullscreen mode Exit fullscreen mode

β†’ Then change pick β†’ squash for the commits you want to merge into one.

Quick Squash Last 2 Commits

git reset --soft HEAD~2
git commit -m "single squashed commit"
Enter fullscreen mode Exit fullscreen mode

This is perfect when you want to clean your history before pushing a feature branch.


πŸ“œ Viewing History & Differences

git log β€” Show full commit history.
git log --oneline β€” Display a compact commit list.
git diff β€” View uncommitted changes.
git diff --staged β€” See diff for staged changes.


🌐 Working with Remote Repositories

git remote -v β€” View linked remotes.
git remote add origin <url> β€” Connect your local repo to a remote.
git push -u origin <branch> β€” Push a new branch and set tracking.


πŸ’‘ Bonus: Life-Saving Commands You’ll Use Often

git stash β€” Save your changes temporarily without committing.
git stash pop β€” Restore the stashed changes.
git cherry-pick <commit> β€” Copy a specific commit onto current branch.
git pull --rebase β€” Pull updates without creating merge commits.
git fetch β€” Get updates from remote without merging.

GIT commands

Top comments (0)