10 Git Commands Every Developer Should Master
Git is the backbone of modern version control, and mastering these 10 essential commands will help you work faster, collaborate better, and avoid common pitfalls. Whether you’re a beginner or an experienced developer, these commands—from initializing a repository to merging branches—are crucial for efficient coding workflows.
“Git is the canvas where developers paint their collaborative masterpieces.”
1. Initialize a Git Repository with git init
Every Git-tracked project starts with git init
. This command creates a hidden .git
directory, setting up version control for your files.
- Initialize a repository in your current folder:
git init
- Create a new repository in a specific directory:
git init project-namecd project-name
2. Clone an Existing Repository with git clone
Need to work on an existing project? git clone
downloads a remote repository (like GitHub or GitLab) to your local machine.
- Clone a repository:
git clone https://github.com/user/repo.git
- Clone into a custom folder:
git clone https://github.com/user/repo.git my-folder
3. Check Repository Status with git status
git status
shows untracked, modified, and staged files, helping you track changes before committing.
- View detailed status:
git status
- Get a shorter summary:
git status -s
4. Stage Changes with git add
Before committing, stage your changes with git add
to tell Git which files to include.
- Stage a single file:
git add file.txt
- Stage all changes:
git add .
5. Save Changes with git commit
git commit
creates a snapshot of your staged changes. Always write clear commit messages!
- Commit with a message:
git commit -m "Fixed login bug"
- Commit all tracked files (skips
git add
):git commit -am "Quick update"
6. Upload Changes with git push
After committing, git push
uploads your changes to a remote repository like GitHub.
- Push to the main branch:
git push origin main
- Push a new branch and set tracking:
git push -u origin new-feature
7. Update Your Local Repository with git pull
git pull
fetches remote changes and merges them into your local branch.
- Pull the latest changes:
git pull
- Pull from a specific branch:
git pull origin feature-branch
8. Manage Branches with git branch
Branches isolate new features or fixes. Use git branch
to create, list, or delete them.
- List all branches:
git branch
- Delete a branch:
git branch -d old-branch
9. Switch Branches with git checkout
git checkout
lets you jump between branches or restore files.
- Switch to a branch:
git checkout main
- Create and switch to a new branch:
git checkout -b new-feature
10. Merge Branches with git merge
Combine changes from one branch into another using git merge
.
- Merge a feature into
main
:git checkout maingit merge feature-branch - Resolve conflicts if they arise.
#Git #VersionControl #DeveloperTools #Coding #Workflow