Mastering the most frequently used Git commands is one of the best investments a developer can make. You don't need to memorize every Git command at once — just master the most essential ones first, and you'll already be able to work professionally on almost any project. This article covers the basic Git commands along with correct and relevant usage examples.
1. git init — Start a New Repository
This command is used to initialize a new Git repository inside a folder. Run it once at the start of a project:
git init
The result is a hidden .git folder that becomes the core of your project's version control system.
2. git clone — Copy a Repository
To copy a repository from a remote (such as GitHub) to your local machine:
git clone https://github.com/username/repo-name.git
You can also specify the destination folder name:
git clone https://github.com/username/repo-name.git local-folder
3. git status — Check File Status
One of the most frequently used commands. It shows which files have been changed, staged, or are not yet tracked by Git:
git status
4. git add — Add Files to Staging
Before committing, files must first be added to the staging area:
git add file-name.html
git add src/
git add .
Use git add . to add all changes at once. Use git add -p if you want to select specific parts of a file.
5. git commit — Save a Snapshot of Your Code
A commit saves the staged changes into the repository's history:
git commit -m "Add login feature with validation"
Use a descriptive message. You can also add and commit at once for files that are already tracked:
git commit -am "Fix bug on the checkout page"
6. git log — View Commit History
Displays the entire commit history along with author, time, and message details:
git log
git log --oneline
git log --oneline --graph --all
The --oneline flag makes the output more compact. The --graph flag shows a visualization of the branch structure.
7. git diff — Compare Changes
View the difference between changed files and the last committed version:
git diff
git diff --staged
git diff shows changes that haven't been staged. git diff --staged shows changes that have already been staged.
8. git push and git pull — Sync with the Remote
Send your local commits to the remote repository:
git push origin main
Download and merge changes from the remote to your local repository:
git pull origin main
9. git branch and git checkout — Branch Management
Create, switch to, and delete branches:
git branch
git branch fitur-login
git checkout fitur-login
git checkout -b fitur-register
The git checkout -b command creates a new branch and switches to it at the same time. In modern Git versions, you can also use git switch:
git switch -c fitur-baru
10. git merge — Merge Branches
Merge changes from another branch into the active branch:
git checkout main
git merge fitur-login
11. git stash — Temporarily Save Changes
If you need to switch branches but aren't ready to commit, temporarily save your changes with stash:
git stash
git stash list
git stash pop
git stash pop restores the most recently saved changes.
12. git reset and git revert — Undo Changes
To unstage before committing:
git reset HEAD file-name.html
To undo the last commit while keeping the changes in the working directory:
git reset --soft HEAD~1
To create a new commit that reverses the changes of a specific commit (safer for repositories that have already been pushed):
git revert abc1234
Conclusion
The twelve commands above cover almost all of a developer's day-to-day Git needs. Start by mastering init, add, commit, push, and pull. Once you're comfortable, move on to branch, merge, and stash. The more often you practice using them on real projects, the more automatic these commands will feel. Don't hesitate to check the documentation with git --help or git <command> --help whenever you need a quick reference.