Git for beginners: the commands you'll actually use
Git is the version control system used by almost every software project. It records the history of your files, lets you undo mistakes, try ideas safely on branches, and collaborate without overwriting each other's work. It looks intimidating at first, but day-to-day use needs only a handful of commands. This guide covers them.
Three ideas to understand first
- Repository (repo): a project folder that Git is tracking, including its full history.
- Commit: a saved snapshot of your project at a moment in time, with a message describing what changed.
- Branch: an independent line of work. You can experiment on a branch without affecting the main version.
Git also has a "staging area": you choose which changes go into the next commit by adding them first. This lets you commit related changes together even if you edited many files.
Setup
Install Git, then tell it who you are. This name and email are recorded in your commits.
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global init.defaultBranch main
Starting a repository
cd my-project
git init
Or copy an existing project from GitHub, GitLab or another server:
git clone https://github.com/user/project.git
The everyday loop
Most of your Git usage is these four commands, repeated:
git status # what has changed?
git add index.html style.css # stage specific files
git add . # or stage everything
git commit -m "Add contact section to homepage"
Run git status often. It tells you which files are modified, which are staged, and what Git expects you to do next.
Writing good commit messages
Write messages that explain the change in one short line, in the imperative: "Fix broken link in footer", "Add dark mode toggle". A month from now, "update" or "stuff" will tell you nothing. Commit small, related changes together rather than a whole day's work in one commit.
Looking at history
git log --oneline # compact list of commits
git diff # unstaged changes
git diff --staged # staged changes, about to be committed
Working with branches
Branches let you build a feature or try an idea without risking the working version.
git switch -c new-header # create a branch and switch to it
# ...edit files, add, commit...
git switch main # go back to main
git merge new-header # bring the changes into main
git branch -d new-header # delete the finished branch
If the same lines were changed differently on both branches, Git reports a merge conflict and marks the conflicting section in the file with <<<<<<<, ======= and >>>>>>>. Edit the file to keep the correct version, remove the markers, then git add the file and commit.
Remote repositories
A remote is a copy of the repository on a server, such as GitHub. It serves as a backup and is how teams share work.
git remote add origin https://github.com/user/project.git
git push -u origin main # first push
git push # later pushes
git pull # get others' changes
Pull before you start working each day, and push when you finish something, to keep conflicts small.
Undoing things
git restore file.txt # discard unstaged changes to a file
git restore --staged file.txt # unstage, but keep the changes
git commit --amend -m "Better message" # fix the last commit (before pushing)
git revert abc1234 # undo a commit by creating a new one
git revert is the safe way to undo something that has already been pushed, because it doesn't rewrite shared history.
Be careful with git reset --hard and git push --force. They permanently discard work or overwrite history that others may depend on. Make sure you understand exactly what they'll do first.
The .gitignore file
Some files should never be committed: passwords and API keys, dependency folders like node_modules, build output, and system files like .DS_Store. List them in a .gitignore file in the project root:
node_modules/
dist/
.env
.DS_Store
If you ever commit a password or key by accident, assume it's compromised: change it immediately, even after removing it from the repository.
Summary
Day to day, Git is just status, add, commit, push and pull, plus branches for new work. Commit small changes with clear messages, keep secrets out with .gitignore, and you'll always be able to go back to any point in your project's history.