Git is a version control system that records snapshots of your project so you can see what changed, undo mistakes, and collaborate without overwriting each other's work. For everyday work, beginners only need a small set of commands: git status to see what is going on, git add and git commit to save a snapshot, git pull and git push to sync with a remote, and git branch, git switch, git merge, git log and git restore to move around and fix things. This guide walks through those commands in the order you will actually use them, plus the mistakes that trip up most beginners.
What You'll Need
- Git installed on your computer. On Windows, install Git for Windows, which also gives you Git Bash. On macOS, running
git --versionin Terminal usually prompts you to install the developer command line tools if Git is missing. On Linux, use your distribution's package manager. - A terminal or command prompt. Git Bash on Windows, Terminal on macOS and Linux, or the built-in terminal in your code editor all work.
- A Git repository. This can be a folder you turn into a repository with
git init, or an existing project you clone from a hosting service such as GitHub, GitLab or Bitbucket. - A configured name and email, set once with
git config --global user.name "Your Name"andgit config --global user.email "you@example.com". Commits record this information.
Step-by-Step
- Check where you are. Run
git status. It tells you which branch you are on, which files have changed, and which changes are staged for the next commit. Make this the first command you run whenever you sit down to work; it prevents most confusion. - Create or get a repository. To start fresh, open a terminal in your project folder and run
git init. To work on an existing project, rungit clone <repository-url>, which downloads the project and its full history into a new folder. - See what changed.
git statuslists modified and untracked files.git diffshows the exact line-by-line changes in files that are not yet staged, andgit diff --stagedshows what is already staged. Reading the diff before committing is the single best habit for catching accidental edits. - Stage the changes you want to save. Use
git add <file>for a specific file orgit add .to stage everything in the current folder and below. Staging is how you choose what goes into the next commit, so you can commit related changes together and leave unrelated work for later. - Commit the snapshot. Run
git commit -m "Short description of what changed". Write the message in the imperative mood, for example "Fix login button alignment" rather than "Fixed stuff". A commit is a permanent point you can return to, so a clear message matters more than a long one. - Work on a branch. Create one with
git switch -c <branch-name>and move between branches withgit switch <branch-name>. Branches let you try changes without disturbing the main line. List them withgit branch. - Sync with the remote. Before you start work, run
git pullto fetch and merge the latest changes from the remote repository. When your commits are ready to share, rungit push. On a new branch the first push usually needsgit push -u origin <branch-name>to set the upstream. - Merge your branch back. Switch to the target branch with
git switch main(ormasteron older setups), pull the latest changes, then rungit merge <branch-name>. If there are no conflicting edits, the merge completes automatically. - Review history.
git log --onelinegives a compact list of commits.git log --oneline --graph --allshows branches and merges visually. This is how you find a commit to return to or to inspect. - Undo safely. To discard uncommitted changes in a file, use
git restore <file>. To unstage a file but keep its changes, usegit restore --staged <file>. To undo the most recent commit while keeping its changes in your working files, usegit reset --soft HEAD~1. Avoidgit reset --hardunless you are certain, because it permanently discards uncommitted work.
Common Mistakes
Beginners most often commit too much at once, so a single commit mixes unrelated changes and becomes hard to review or revert. Stage and commit in small, logical chunks. Another frequent mistake is running git add . without checking git status first, which can sweep in build output, temporary files or secrets; a .gitignore file prevents that. Many people also forget to pull before pushing, which causes rejected pushes, and some run git reset --hard to "clean up" and lose work they meant to keep. Finally, commit messages like "update" or "fix" make history useless later; say what changed and why.
Tips
Run git status constantly; it is cheap and prevents surprises. Commit early and often on a branch rather than saving one giant commit at the end. Pull before you start work each day and before you push. Keep one branch per task and merge it back when done. Use git diff before every commit to review exactly what you are saving. If a command looks destructive, especially anything with --hard or --force, stop and check what it will remove before pressing Enter. Learn to read the hints Git prints after each command; they usually tell you the next step.
Troubleshooting
If git push is rejected because the remote has changes you do not have, run git pull first, resolve any conflicts, commit the resolution, then push again. If a merge reports conflicts, open the listed files, look for the conflict markers, keep the correct lines, remove the markers, then git add the files and commit. If you committed to the wrong branch, you can move the commit with git reset --soft HEAD~1 on that branch, switch to the correct branch, and commit again. If you accidentally staged a large or sensitive file, unstage it with git restore --staged <file> before committing; if it was already committed and pushed, treat it as exposed and rotate any credentials. If Git says it does not know who you are, set your name and email with the config commands above. If you are completely lost, git status and git log --oneline will usually show where you are and what happened.
Frequently Asked Questions
What is the difference between git add and git commit?
git add stages changes, marking which ones will go into the next snapshot. git commit actually records that snapshot in the repository history.
Do I need GitHub to use Git?
No. Git works entirely on your own computer. A hosting service such as GitHub, GitLab or Bitbucket is only needed when you want to back up, share or collaborate on a remote copy.
What does git pull actually do?
It fetches changes from the remote repository and merges them into your current branch. If your local changes conflict with incoming ones, Git asks you to resolve the conflicts before continuing.
How do I undo my last commit?
Use git reset --soft HEAD~1 to remove the commit while keeping its changes staged, or git reset --mixed HEAD~1 to keep them unstaged. Both preserve your work.
What is a merge conflict?
It happens when two branches changed the same lines and Git cannot decide which version to keep. You resolve it by editing the file, removing the conflict markers, and committing the result.
Is it safe to delete a branch?
Deleting a merged branch with git branch -d is safe because its commits are already in the target branch. Use -D only if you are sure you do not need the unmerged commits.