Version control with Git for tracking code changes and collaboration.
Initial Configuration
1
2
3
4
5
6
7
8
9
|
# Configure your identity
git config --global user.name "N/A"
git config --global user.email "[email protected]"
# Set default branch name
git config --global init.defaultBranch main
# Improve output readability
git config --global color.ui auto
|
Local Repository
Initialize and work with a local repository:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
# Create new repository
git init
# Check current status
git status
# Stage all files
git add .
# Commit changes
git commit -m "Initial commit"
# View commit history
git log
git log --oneline --graph
|
Remote Repository
Connect local repository to remote server (GitHub, GitLab, etc.):
1
2
3
4
5
6
7
8
|
# Add remote
git remote add origin [email protected]:username/repo.git
# Push to remote (first time)
git push -u origin main
# View remotes
git remote -v
|
Basic Workflow
Standard workflow for daily development:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
# Get latest changes from remote
git pull
# Make your changes...
# Stage modified files
git add file1.txt file2.txt
# Or stage all changes
git add .
# Commit with descriptive message
git commit -m "Add feature X"
# Push to remote
git push
|
Branching
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# Create new branch
git branch feature-x
# Switch to branch
git checkout feature-x
# Create and switch in one command
git checkout -b feature-x
# List branches
git branch
# Merge branch into current branch
git merge feature-x
# Delete branch
git branch -d feature-x
|
Ignoring Files
Create .gitignore in repository root:
# Ignore build artifacts
*.o
*.so
build/
dist/
# Ignore environment files
.env
.env.local
# Ignore IDE settings
.vscode/
.idea/
# Ignore OS files
.DS_Store
Thumbs.db
Viewing Changes
1
2
3
4
5
6
7
8
9
10
11
|
# Show unstaged changes
git diff
# Show staged changes
git diff --staged
# Show changes in specific file
git diff file.txt
# Show commit details
git show abc123
|
Undoing Changes
1
2
3
4
5
6
7
8
9
10
11
|
# Discard changes in working directory
git checkout -- file.txt
# Unstage file (keep changes)
git reset HEAD file.txt
# Amend last commit (before push)
git commit --amend
# Revert committed changes (creates new commit)
git revert abc123
|
Stashing
Temporarily save work in progress:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# Stash current changes
git stash
# List stashes
git stash list
# Apply most recent stash
git stash apply
# Apply and remove stash
git stash pop
# Stash with message
git stash save "Work in progress on feature X"
|
Useful Aliases
Add to ~/.gitconfig:
1
2
3
4
5
6
7
8
|
[alias]
st = status
co = checkout
br = branch
ci = commit
unstage = reset HEAD --
last = log -1 HEAD
lg = log --oneline --graph --decorate
|
Usage:
1
2
|
git st # Same as git status
git lg # Pretty log
|
Tips
- Commit often with clear messages
- Pull before starting new work
- Keep commits focused (one logical change per commit)
- Use branches for new features
- Review changes before committing (
git diff)
- Don’t commit sensitive data (passwords, API keys)