Initializes a new Git repository.
.git folder (history, branches, config)After this, Git starts tracking file changes.
Sets your identity for commits.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"--global → applies to all projects--global → applies to current project onlyCopies an existing remote repository to your system.
git clone https://github.com/user/project.gitManages remote repositories (GitHub, GitLab, etc.).
Check remotes:
git remote -vAdd a remote:
git remote add origin https://github.com/user/project.gitorigin is just a common name for the main remote.
Shows the current state of your project.
git statusTells you:
Run this before every commit.
Stages changes for commit.
git add filename
git add .Saves a snapshot of staged changes.
git commit -m "Add login feature"Quick commit for tracked files:
git commit -a -m "Fix bug"Edit last commit:
git commit --amend⚠️ Commits are local only until pushed.
Uploads local commits to remote repository.
git push origin mainmain)Updates your local branch with remote changes.
git pull origin mainDownloads remote changes without merging.
git fetch originView new commits:
git log HEAD..origin/mainBest when you want to review first, merge later.
Creates and lists branches.
Create branch:
git branch new-featureList branches:
git branch* shows current branch.
Switches branches or restores files.
Switch branch:
git checkout new-featureCreate & switch:
git checkout -b new-featureRestore file:
git checkout -- style.cssModern alternative:
git switch new-featureCombines one branch into another.
git merge new-featureAfter fixing conflicts:
git add .
git commitRewrites commit history for a clean timeline.
git rebase mainShows commit history.
git log
git log --oneline
git log --graphUseful for:
Shows changes line-by-line.
git diff
git diff branch1..branch2Use before committing to see exact changes.
Temporarily saves uncommitted work.
git stash
git stash apply
git stash popUseful when switching branches mid-work.
Moves HEAD to a previous commit.
Soft (keep staged):
git reset --soft HEAD~1Mixed (unstage changes):
git reset HEAD~1Hard (delete everything):
git reset --hard HEAD~1⚠️ Hard reset permanently deletes work.
Safely undo a pushed commit.
git revert <commit-hash>Apply a specific commit from another branch.
git cherry-pick <commit-hash>