git is a distributed version control system for tracking changes in source code during software development.
- Download the installer from the official Git website.
- Run the installer and follow the setup instructions.
- Open a new terminal and verify the installation:
git --version
- Install via Homebrew:
brew install git
- Or download from the official Git website.
- Verify the installation:
git --version
- Install via package manager:
# Ubuntu/Debian sudo apt update && sudo apt install git # CentOS/RHEL/Fedora sudo yum install git # or sudo dnf install git
- Verify the installation:
git --version
- Set your name:
git config --global user.name "Your Name" - Set your email:
git config --global user.email "your.email@example.com" - Check current configuration:
git config --list
- Initialize a new repository:
git init
- Clone an existing repository:
git clone <repository-url>
- Clone to a specific folder:
git clone <repository-url> <folder-name>
- Check repository status:
git status
- View commit history:
git log
- View compact log:
git log --oneline
- Add files to staging area:
git add <file-name>
- Add all files:
git add . - Commit staged changes:
git commit -m "Your commit message" - Add and commit in one step:
git commit -am "Your commit message"
- See differences in working directory:
git diff
- See differences in staged files:
git diff --staged
- Show changes in specific commit:
git show <commit-hash>
- List all branches:
git branch
- Create a new branch:
git branch <branch-name>
- Switch to a branch:
git checkout <branch-name>
- Create and switch to new branch:
git checkout -b <branch-name>
- Delete a branch:
git branch -d <branch-name>
- Switch to a branch:
git switch <branch-name>
- Create and switch to new branch:
git switch -c <branch-name>
- List remote repositories:
git remote -v
- Add a remote:
git remote add <name> <url>
- Remove a remote:
git remote remove <name>
- Fetch changes from remote:
git fetch
- Pull changes from remote:
git pull
- Push changes to remote:
git push
- Push new branch to remote:
git push -u origin <branch-name>
- Merge a branch:
git merge <branch-name>
- Merge with no fast-forward:
git merge --no-ff <branch-name>
- Rebase current branch:
git rebase <branch-name>
- Interactive rebase:
git rebase -i <commit-hash>
- Unstage a file:
git reset <file-name>
- Discard changes in working directory:
git checkout -- <file-name>
- Reset to previous commit (keep changes):
git reset --soft HEAD~1
- Reset to previous commit (discard changes):
git reset --hard HEAD~1
- Revert a commit:
git revert <commit-hash>
- Revert multiple commits:
git revert <commit-hash1>..<commit-hash2>
- Create useful aliases:
git config --global alias.st status git config --global alias.co checkout git config --global alias.br branch git config --global alias.cm commit git config --global alias.lg "log --oneline --graph --all"
- Stash current changes:
git stash
- Apply stashed changes:
git stash pop
- List stashes:
git stash list
# Clone a repository
git clone https://github.com/username/repo.git
cd repo
# Create and switch to feature branch
git checkout -b feature/new-feature
# Make changes and commit
git add .
git commit -m "Add new feature"
# Push to remote
git push -u origin feature/new-feature
# Switch back to main and merge
git checkout main
git pull
git merge feature/new-feature
git pushFor more details, visit the official Git documentation: