Guide · Technology
Git for Beginners: The Handful of Commands You Actually Need
Git looks scary because of its long command list. In daily work you use about ten of them. This guide explains the idea behind Git, then walks through the everyday workflow, branches, and how to undo mistakes.
For
Students and new developers who have heard of Git and GitHub but feel lost in the terminal.
Level
Before you start
Comfort opening a terminal and running basic commands. Git installed from https://git-scm.com.
On this page
Key takeaways
- Git records snapshots of your project called commits. You can go back to any of them.
- The daily loop is: change files, git add, git commit, git push.
- Branches let you try work without touching the main line of code.
- git status is your friend. Run it often.
- Most mistakes can be undone, if you commit often and understand what each undo command does.
Imagine writing a long document and saving copies as final.docx, final_v2.docx and final_REAL_final.docx. Now imagine ten people doing that to the same document at once.
Git solves exactly that problem for code. It records the history of your project, lets many people work in parallel, and makes it safe to experiment. Once you understand the model, the commands stop being magic.
The idea in a few lines
#- A repository (repo) is your project folder plus its full history.
- A commit is a snapshot of your files at a moment, with a message describing the change.
- A branch is a separate line of work. The main one is usually called
main. - A remote is a copy of the repo hosted elsewhere, such as GitHub. Git and GitHub are not the same: Git is the tool, GitHub is a website that hosts Git repos.
Think of commits as save points in a game. You can always return to one.
One-time setup
#Tell Git who you are. These details appear in every commit.
git config --global user.name "Your Name"git config --global user.email "you@example.com"Starting a project
#Either start a new repo in a folder:
git initor copy an existing one:
git clone https://github.com/owner/project.gitThe everyday loop
#This is 90% of Git.
git status # what changed?git add file.js # stage a file for the next commitgit add . # or stage everything that changedgit commit -m "Add login form validation"git push # send commits to the remoteStaging, explained
#Git has a middle step called the staging area. git add puts changes into it, and git commit saves only what is staged. This lets you commit related changes together, even if you've edited several things.
To see what you're about to commit:
git diff # unstaged changesgit diff --staged # staged changesWriting good commit messages
#A commit message is a note for your future self and your team.
- Write in the imperative: "Fix crash when cart is empty", not "fixed stuff".
- Keep the first line under about 70 characters.
- Make one logical change per commit.
Small, frequent commits make problems easier to find. This pairs well with the process in how to debug any bug, where git bisect relies on a clean history.
Looking at history
#git log --oneline # a short list of commitsgit log --oneline --graph # with branch structuregit show a1b2c3d # what one commit changedBranches: working without fear
#A branch lets you build a feature without affecting main.
git switch -c add-search # create and move to a new branch# ...edit, add, commit...git switch main # go back to maingit merge add-search # bring the work into maingit branch -d add-search # delete the finished branchWhy this matters: main stays working, while the feature grows on its own branch. If the feature goes badly, you throw the branch away.
A normal team workflow
#- Pull the latest
main. - Create a branch for your task.
- Commit as you go.
- Push the branch.
- Open a pull request (PR) on GitHub so teammates can review.
- After approval, merge it.
git switch maingit pullgit switch -c fix-checkout-bug# work and commitgit push -u origin fix-checkout-bugMerge conflicts: not a disaster
#A conflict happens when two branches changed the same lines. Git can't decide which is right, so it asks you.
Open the file and you'll see markers:
<<<<<<< HEADconst price = base + tax;=======const price = base + tax + shipping;>>>>>>> add-shipping- Decide what the final code should be.
- Delete the markers and keep the correct code.
- Run
git add file.js, thengit commit.
Conflicts are normal in team work. Smaller, more frequent merges reduce them.
Ignoring files
#Some files should never go into Git: dependencies, build output, local settings and, above all, secrets.
Create a .gitignore file:
node_modules/.envdist/.DS_StoreIf you commit a password or API key by mistake, deleting it in the next commit does not remove it from history. Treat it as compromised and rotate it.
Undoing things
#Most Git panic comes from not knowing which undo to use.
Situation | Command |
|---|---|
Discard unstaged changes in a file |
|
Unstage a file but keep the changes |
|
Fix the last commit message |
|
Undo a pushed commit safely |
|
Temporarily set work aside |
|
Be careful with git reset --hard and git push --force. They can destroy work. If you aren't sure, make a backup branch first:
git branch backup-before-resetUseful habits
#- Run
git statusbefore and after anything unusual. - Pull before you start work each day.
- Don't commit large files or generated files.
- Keep branches short-lived.
- Review your own diff before pushing.
Learning with AI help
#AI assistants can explain Git errors well, but never run a destructive command you don't understand. Paste the error text and ask what it means, then check against the Git docs. Guidance on using assistants well is in using AI at work.
Frequently asked questions
#What's the difference between Git and GitHub?
#Git is the version control tool on your computer. GitHub is a hosting service and collaboration platform for Git repositories. GitLab and Bitbucket are alternatives.
merge or rebase?
#Merge keeps history as it happened. Rebase rewrites your branch to sit on top of the latest main, giving a straighter history. As a beginner, use merge. Learn rebase later, and never rebase commits others have pulled.
What does "detached HEAD" mean?
#You've checked out a specific commit instead of a branch. You can look around, but new commits might be lost. Run git switch main to return, or create a branch to keep your work.
Do I need the command line, or can I use a GUI?
#GUIs, including those in VS Code, are fine. But learning the basic commands helps you understand what the GUI does and fix problems.
How do I practise?
#Create a small repo, make commits, branch, deliberately create a conflict and resolve it. An hour of this beats a day of reading.
Commands shown work with current versions of Git. Check git help <command> for details.
Sources & further reading
Connected reading
Linked from 7 guides
Other pages in the library that point here.
Share this guide
Keep reading
More in Technology →Guide · Technology
Git for Java Projects: Commits, Branches, Merging and Pull Requests
Guide · Technology
How to Learn Programming When AI Can Already Write Code
Guide · Career
A LinkedIn Profile That Gets Recruiter Messages

