π§ 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>
β 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"
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.

Top comments (0)