git config --global user.name "Ada Lovelace"
git config --global user.email ada@example.comIdentity recorded in every commit.
The git commands you use every week, grouped by task, plus the safe way to undo things.
26 entries
git config --global user.name "Ada Lovelace"
git config --global user.email ada@example.comIdentity recorded in every commit.
git config --global init.defaultBranch mainDefault branch name for new repos.
git init
git clone https://github.com/user/repo.gitStart a repo or copy an existing one.
git status
git diff
git diff --stagedWhat changed; unstaged vs staged diff.
git add file.py
git add -pStage a file; -p stages hunk by hunk.
git commit -m "Add login form"Commit staged changes.
git commit --amend --no-editAdd staged changes to the last commit (before pushing).
git log --oneline --graph --allCompact history graph.
git switch -c feature/login
git switch mainCreate and switch; switch back.
git branch
git branch -d feature/loginList; delete a merged branch.
git merge feature/loginMerge a branch into the current one.
git rebase mainReplay your commits on top of main (don’t rebase shared branches).
git rebase -i HEAD~3Interactive rebase: squash, reword, reorder the last 3 commits.
git cherry-pick a1b2c3dCopy one commit onto the current branch.
git remote -v
git remote add origin URLList / add remotes.
git fetch
git pull --rebaseDownload; update current branch by rebasing.
git push -u origin feature/loginPush and set upstream.
git push --force-with-leaseSafer force-push: refuses if the remote moved.
git restore file.pyDiscard unstaged changes in a file.
git restore --staged file.pyUnstage, keep the changes.
git reset --soft HEAD~1Undo the last commit, keep changes staged.
git revert a1b2c3dNew commit that reverses an old one (safe on shared branches).
git reflogFind “lost” commits after a bad reset or rebase.
git switch main && git pull
git switch -c fix/typo
# edit…
git add -A && git commit -m "Fix typo"
git push -u origin fix/typo
# open a pull request, then after merge:
git switch main && git pull && git branch -d fix/typoThe everyday loop used on GitHub and GitLab.
fetch downloads new commits from the remote without changing your branch. pull is fetch followed by a merge (or a rebase with --rebase) into your current branch.
Merge keeps history exactly as it happened and is safe on shared branches. Rebase produces a linear history by rewriting your commits; use it on your own local or feature branches before sharing.
Use git revert <sha>. It creates a new commit that undoes the change without rewriting history that others may have pulled.