1. Initialize Git Repo
git init — Initializes a new, empty Git repository in your current folder. Creates a hidden .git directory to track versions of your files.
2. Clone a Repository
git clone <url> — Creates a copy of a remote repository (like from GitHub) on your local machine. The <url> can be HTTPS or SSH.
3. Check Repo Status
git status — Displays the state of your working directory and staging area. Shows which files are modified, staged, or untracked.
4. Stage Files
git add . — Stages all changes (new, modified, deleted) in the current directory for the next commit.
5. Commit Changes
git commit -m "message" — Takes a snapshot of the staged changes and saves it to the repository with a custom message.
6. View Commit History
git log — Lists all commits in the current branch along with author, date, and message. Useful for tracking changes over time.
7. Create Branch
git branch new-feature — Creates a new branch called new-feature but does not switch to it.
8. Switch Branch
git checkout main — Changes your working directory to the main branch (or any other specified branch).
9. Create & Switch
git checkout -b new-feature — Creates and immediately switches to the new-feature branch. Shortcut for branching and switching.
10. Merge Branches
git merge new-feature — Combines the changes from new-feature branch into the branch you're currently on.
11. Pull Changes
git pull — Fetches updates from the remote repository and merges them with your local copy in one step.
12. Push Changes
git push — Uploads your local commits to the remote repository (e.g., GitHub) for others to see and collaborate.
13. Delete Branch
git branch -d branch-name — Deletes a branch locally. Use -D to force delete even if it’s not merged.
14. Stash Changes
git stash — Temporarily saves your uncommitted changes, letting you work on something else without committing.
15. Apply Stash
git stash apply — Reapplies the most recent stashed changes to your working directory.
16. View Remote URLs
git remote -v — Lists all remotes for the repo with URLs for fetch and push operations.
17. Set Remote Origin
git remote add origin <url> — Links your local repo to a remote one, usually on GitHub. origin is just the default name.
18. View File Changes
git diff — Shows line-by-line differences between your working files and the staging area or previous commits.
19. Reset Commit
git reset --soft HEAD~1 — Undoes the most recent commit but keeps your files staged for a new commit.
20. Revert Commit
git revert <commit-id> — Creates a new commit that undoes the changes made in a previous one. Safer than reset for shared branches.