This guide explains the essential Git commands you need to save (commit) your work and share it (push/pull) with others. You’ll learn what each command does, when to use it, and how to avoid common pitfalls as a beginner.
What Is Git and Why Use It?
Git is a version control system that tracks changes to files over time. It was created by Linus Torvalds in 2005 for the Linux kernel development. Unlike simple file copying, Git stores a complete history of every change, allowing you to revert to any previous state, work on multiple features simultaneously, and collaborate without overwriting each other’s work. A repository (or “repo”) is the folder that Git tracks, containing all files, history, and configuration.
For a beginner, the most important concept is the three-tree architecture: your working directory (where you edit files), the staging area (where you prepare changes), and the local repository (where commits are stored). Commands like add and commit move changes between these areas, while push and pull sync them with a remote server like GitHub, GitLab, or Bitbucket.
Essential Git Commands: Saving Your Work
These are the commands you’ll use daily to record your progress:
git init– Creates a new Git repository in your current folder. Run this once when starting a project.git status– Shows which files are modified, staged, or untracked. Always run this first to see where you are.git add <file>– Stages a file (orgit add .for all changes). This tells Git which changes you want to include in the next commit.git commit -m "message"– Creates a snapshot of the staged changes with a descriptive message. The message should explain why the change was made.git log– Shows the commit history with unique IDs (hashes), authors, dates, and messages.git diff– Shows the exact differences between your working directory and the last commit. Usegit diff --stagedto review what you’ve staged.
Example workflow: after editing index.html, you run git add index.html, then git commit -m "Fix header layout". This creates a permanent record you can always return to.
How to Share Your Work: Remote Repositories
To collaborate, you need a remote repository hosted on a service like GitHub, GitLab, or Bitbucket. The key commands are:
git remote add origin <URL>– Connects your local repo to a remote one.originis the conventional name for the primary remote.git push -u origin main– Uploads your local commits to the remote. The-uflag sets the upstream so future pushes can be justgit push.git pull– Downloads changes from the remote and merges them into your current branch. This is how you get your collaborator’s updates.git clone <URL>– Copies an existing remote repo to your local machine, including its full history.
For example, to contribute to an open-source project, you’d fork it on GitHub, clone your fork, make changes, commit, push, and then open a pull request.
Branching and Merging: Working Without Chaos
Branches let you work on features or fixes independently without affecting the main codebase. The default branch is often named main or master.
git branch– Lists all local branches. The current one is marked with an asterisk.git branch <name>– Creates a new branch.git checkout <name>orgit switch <name>– Switches to that branch.git merge <branch>– Integrates changes from the specified branch into your current branch.
Example: you’re on main and want to add a new feature. Create a branch: git checkout -b feature-login (this creates and switches in one step). After committing your changes, switch back to main and run git merge feature-login. This keeps the main branch stable while you experiment.
Undoing Changes: Fixing Mistakes Safely
Everyone makes mistakes. Git provides several ways to undo changes, each suited to a different situation:
git restore <file>– Discards uncommitted changes in your working directory, reverting to the last commit.git reset HEAD <file>– Unstages a file but keeps your changes. Useful if you accidentally added the wrong file.git commit --amend– Modifies the last commit, either to add more changes or update the message. Use with caution if you’ve already pushed.git revert <commit-hash>– Creates a new commit that undoes the changes of a specific commit. This is safe for shared branches because it doesn’t rewrite history.
Never use git reset on a branch that others have pulled from, as it rewrites history and can cause conflicts.
Common Problems and How to Solve Them
Beginners often hit the same issues. Here are the most frequent ones and their fixes:
- Merge conflicts – When two people change the same line, Git can’t auto-merge. You’ll see markers like
<<<<<<<in the file. Manually edit the file to keep the correct version, thengit addandgit commit. - Accidentally committing sensitive data – If you commit a password or API key, change the secret immediately. Then use
git revertor, if the commit hasn’t been pushed,git resetand recommit. - “Please tell me who you are” error – Git needs your name and email. Set them globally:
git config --global user.name "Your Name"andgit config --global user.email "you@example.com". - Pushing to the wrong branch – Check with
git branchbefore pushing. If you’ve pushed to the wrong branch, you can usegit revertor, if it’s your own fork, force-push (but avoid this on shared branches).
Git vs. Other Version Control Systems
Git is the most widely used version control system, but it’s not the only one. Subversion (SVN) and Mercurial are older alternatives. Git’s main advantages are its distributed nature (every clone has the full history), speed, and powerful branching. The downside is a steeper learning curve. For a solo beginner, the commands above are enough; you don’t need to understand every internal detail to benefit.
Frequently Asked Questions
What is the difference between git pull and git fetch?
git fetch downloads the latest changes from the remote but does not merge them into your working directory. git pull does both: it fetches and then automatically merges. Beginners usually want git pull, but if you want to review changes before merging, use git fetch followed by git merge origin/main.
How do I ignore files I don’t want to commit?
Create a file named .gitignore in your repository root. List files or patterns, one per line, like node_modules/ or *.log. Git will then ignore those files. This is essential for excluding build artifacts, local configuration, and sensitive files.
What is a good commit message?
A good commit message is concise but descriptive. Use the imperative mood (“Fix bug” not “Fixed bug”). Include a short summary in the first line (under 50 characters) and, if needed, a blank line followed by a longer explanation. For example: “Update login form validation” or “Refactor database connection to use environment variables”.
Can I use Git locally without a remote?
Yes. Git works entirely on your local machine. You can initialize a repo, commit changes, and create branches without ever connecting to a remote. This is a great way to track your own projects. However, you won’t have a backup or collaboration features unless you add a remote.
How do I undo the last commit?
If the commit hasn’t been pushed, use git reset --soft HEAD~1 to undo the commit but keep your changes staged. If you want to discard the changes entirely, use git reset --hard HEAD~1 (this deletes the changes permanently). If the commit has been pushed, use git revert HEAD to create a new commit that undoes it.
What is the difference between Git and GitHub?
Git is the version control software that runs on your computer. GitHub is a web-based hosting service for Git repositories, offering collaboration features like pull requests, issue tracking, and project management. You can use Git without GitHub, but GitHub makes it easier to share and collaborate.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Secure Boot Battlefield 6 ASUS: Fix & Enable Guide
- Best Tech Websites to Follow for Pros & Beginners
- Second Wind Quest Walkthrough: Where Winds Meet6.00 min/km
- Enshrouded Weapon Upgrade Guide: How to Improve Gear5.00 min/km
- Connect Phone to Controller for Cloud Gaming
- Python Search Highlighting: Snippets & Matches