Core Git Operations
Managing your development enviroment and repositories effectively is critical for version control workflows.
Repository Setup and File Management
# Initialize a new local repository
git init
# Configure remote origin
git remote add origin <repository_url>
# Clone a specific branch
git clone -b <branch_name> <repository_url>
# Stage files for commit
git add <file_path>
# Commit changes with a descriptive message
git commit -m "Description of changes"
Branching and Repository Inspection
# Check status and diff
git status
git diff <file>
# View commit history
git log --oneline
git reflog
# Manage branches
git branch -a # List all branches
git branch <name> # Create a new branch
git checkout -b <name> # Create and switch to new branch
git branch -d <name> # Delete a local branch
Version Reversion and Modification Undo
Use git reset to move the branch pointer, or git revert to generate a new commit that neutralizes previous changes.
# Hard reset to a previous state
git reset --hard HEAD~1
# Revert a specific commit by creating an inverse change
git revert <commit_id>
# Discard local file modifications
git checkout -- <file>
Stashing Work-in-Progress
When you need to switch tasks without committing, use the stash functionality to temporarily save your changes.
git stash # Hide current work
git stash list # View stashed items
git stash pop # Restore and remove from stash
Advanced Configurations
Case Sensitivity and File Modes
On systems where Git behaves incorrectly regarding file naming conventions or permissions, adjust your config:
git config core.ignorecase false
git config core.filemode false
Cleaning Untracked Files
Remove extraneous files that are not under version control:
git clean -nfd # Preview files to be deleted
git clean -fd # Remove untracked files and directories
Multi-Account SSH Management
To manage multiple GitHub/GitLab accounts on a single machine, use an SSH configuration file (~/.ssh/config):
# ~/.ssh/config structure
Host personal-github
HostName github.com
IdentityFile ~/.ssh/id_rsa_personal
Host work-github
HostName github.com
IdentityFile ~/.ssh/id_rsa_work
Once configured, point your remote URLs to the alias defined in the Host block: git remote add origin git@work-github:user/repo.git.