Useful Git commands for beginners
Git is a powerful tool for version control, enabling developers to track changes, collaborate efficiently, and manage project history. This guide covers how to use Git effectively along with the most commonly used Git commands.
1. Installing Git
macOS (via Homebrew)
brew install git
Linux (Debian-based)
sudo apt update && sudo apt install git
Windows
Download and install Git from git-scm.com.
2. Configuring Git
Set up your Git username and email:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
3. Initializing and Cloning Repositories
Initialize a new repository
cd /path/to/your/project
git init
Clone an existing repository
git clone https://github.com/username/repository.git
4. Working with Changes
Check status of changes
git status
Add changes to staging
git add .
Commit changes
git commit -m "Your commit message"
5. Working with Branches
Create and switch to a new branch
git checkout -b feature-branch
Merge a branch into main
git checkout main
git merge feature-branch
Delete a local branch
git branch -d branch-name
6. Pushing and Pulling Changes
Push changes to a remote repository
git push -u origin main
Pull latest changes from the remote repository
git pull origin main
7. Undoing Changes
Revert last commit (soft reset)
git reset --soft HEAD~1
Undo last commit and discard changes
git reset --hard HEAD~1