Essential Git Commands for Local and Remote Repository Management
Git Core Concepts
Git opreates through three primary areas: the working directory, staging area, and repository. The working directory contains visible files, the staging area (or index) holds changes before commit, and the repository stores all committed versions in the .git directory.
Basic Local Repository Commands
Configuration and Initialization
Set global user details:
git config --global user.name "Your Name"
git config --global user.email "email@example.com"
Verify configuration:
git config --list --global
git config user.name
git config user.email
Initialize a new repository:
git init
File Tracking and Status
Check file status:
git status
git status -s # Compact view
Stage files for commit:
git add filename.txt # Single file
git add . # All files
Commit staged changes:
git commit -m "Commit description"
History and Modifications
View commit history:
git log
git log -2 # Last two commits
git log -2 --pretty=oneline # One-line format
git log -2 --pretty=format:"%h | %an | %ar | %s" # Custom format
Undo changes:
git checkout -- filename.txt # Discard working directory changes
git reset HEAD filename.txt # Unstage file
Remove files:
git rm -f filename.txt # Remove from both repository and working directory
git rm --cached filename.txt # Remove only from repository
Advanced Operations
Skip staging and commit directly:
git commit -a -m "Commit message"
Revert to a specific commit:
git reset --hard commit_id
Create .gitignore file to exclude files:
*.log
build/
!important.log
doc/**/*.pdf
Branch Management
Branch Concepts
By default, Git creates a master branch upon initialization. This branch typically holds stable code. For development, create feature branches to isolate changes before merging back to master.
Branch Commands
List branches:
git branch
Create and switch to a new branch:
git branch new-feature
git checkout new-feature
Or combine both:
git checkout -b new-feature
Switch between branches:
git checkout branch-name
Merge branches:
git checkout master
git merge feature-branch
Delete a branch:
git branch -d branch-name
Handle merge conflicts:
# After resolving conflicts manually
git add .
git commit -m "Resolved merge conflicts"
Remote Repository Operations with GitHub
Connection Methods
Connect to GitHub using HTTPS (requires credentials each time) or SSH (requires setup but allows password-less access). SSH is recommended for frequent interactions.
SSH key components:
- Private key (
id_rsa): Stored locally - Public key (
id_rsa.pub): Added to GitHub account
Remote Commands
Clone a remote repository:
git clone https://github.com/user/repo.git
Push local branch to remote (first time):
git push -u origin local-branch-name
Subsequent pushes:
git push
View remote branches:
git remote show origin
Track remote branch locally:
git checkout remote-branch-name
git checkout -b local-name origin/remote-branch-name # With rename
Pull remote changes:
git pull
Delete remote branch:
git push origin --delete branch-name