Basic Git Commands – Essential Git Guide for Beginners
Why Learn Git Commands?
Git is a powerful version control system, but to use it effectively, you need to understand its basic commands. These commands will help you create repositories, track changes, collaborate with teams, and manage your code efficiently.
This guide covers the most important Git commands every beginner should know.
Setting Up Git
Before using Git, ensure it's installed and configured:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Verify your settings:
git config --list
️ Essential Git Commands
Initialize a New Repository
Create a new Git epository in an existing folder:
git init
This creates a .git
directory, which stores all version control data.
Clone an Existing Repository
Copy a remote repository to your local system:
git clone <repository-url>
Example:
git clone https://github.com/user/repo.git
Check the Status of Your Repository
See which files are modified, staged, or untracked:
git status
Track New Files (Staging Area)
Add a file to the staging area to be included in the next commit:
git add <file-name>
To stage all files at once:
git add .
Commit Changes (Save Snapshot)
Record staged changes in the repository:
git commit -m "Describe your changes here"
Example:
git commit -m "Fixed login bug"
View Commit History
See a list of all previous commits:
git log
For a compact, one-line summary:
git log --oneline
Undo Changes
Unstage a file (remove from staging area but keep changes)
git reset <file-name>
Undo last commit (but keep changes unstaged)
git reset --soft HEAD~1
Undo last commit (discard changes permanently)
git reset --hard HEAD~1
Fetch Updates from a Remote Repository
Download changes from the remote repository but do not merge them:
git fetch
This allows you to review incoming changes before applying them.
Pull Updates from a Remote Repository
Fetch and merge the latest changes from a remote repository:
git pull
Push Changes to a Remote Repository
Send local commits to a remote repository:
git push origin main
(Replace main
with your branch name if needed.)
Create and Switch Branches
Create a new branch:
git branch new-feature
Switch to the new branch:
git checkout new-feature
Or create and switch in one command:
git checkout -b new-feature
List Git Tags
Show all tags in the repository:
git tag
Search for a specific tag:
git tag -l "v1.*"
View details of a specific tag:
git show <tag-name>
Next Steps
Now that you know the basic Git commands, try using them in a real project! Explore more advanced Git topics:
Using Git Branches – Learn how to manage branches.
Merging & Handling Merge Conflicts – Combine changes efficiently.
Pushing and Pulling from GitHub & GitLab – Work with remote repositories.
Need help? Ask in the comments or check the official Git documentation.