Git for absolute beginners: the handful of commands you actually need
Git has hundreds of commands and options, but beginners only need a small set to work comfortably. Start with these and learn the rest as needed.
Setting up:
git initcreates a new repository in the current folder.git clone <url>copies an existing repository to your computer.
The everyday loop:
git statusshows what has changed. Run it often.git add <file>(orgit add .) stages changes for the next commit.git commit -m "Describe the change"saves a snapshot.git pushsends commits to a remote like GitHub.git pullgets the latest changes from the remote.
Looking around:
git log --onelineshows commit history compactly.git diffshows changes not yet staged.
Working with branches:
git branchlists branches.git switch -c new-featurecreates and switches to a new branch.git switch maingoes back to main.git merge new-featurebrings a branch's changes into your current branch.
Undoing safely:
git restore <file>discards uncommitted changes in a file (careful, they're gone).git restore --staged <file>unstages a file.
Tips: commit small and often, write clear messages, and add a .gitignore file so you don't commit things like passwords, node_modules or build output.
Which Git concept took you longest to understand? Do you use the command line or a visual Git tool?
Comments · 4
git status is the one command to run constantly. It tells you where you are and what's next.
What's the safest way to undo a mistake as a beginner? I get nervous about losing work.
Commit often. Then git restore for unsaved changes and git revert for a bad commit, both of which leave history intact. Avoid reset --hard at first.
Git commit -m "fix" has been my entire career.