Essential Git Commands for Daily Development Workflows
Getting Help Documentation
git switch --help
git switch -h
git
File Lookup Operations
Find filename based on hash value:
git rev-list --objects --all | grep 408d068ca838d811285199866c6cfa9a0f03e515
Log and Diff Commands
Display commits with one line per entry:
git log --pretty=oneline --abbrev-commit
Show differences between working directory and last commit:
git diff HEAD
Repository Cleanup
Remove untracked files:
git clean -f
System Architecture Comparison
SVN uses centralized management where code exists only on servers. Git employs distributed management where both local and remote repositories maintain complete copies. With Git, you may have all versioned code locally, while SVN typically contains only the currently checked-out version.
Branch and Tag Management
List all branches:
git branch -a
View available tags:
git tag
Check commit history:
git log
Version Control Operations
Revert to specific commit:
git reset --hard <commit_id>
Track previous commits after reset:
git reflog
Working Directory Management
Create ignore file:
touch .gitignore
Check file status across areas:
git status
Stage files for commit:
git add .
Commit staged changes:
git commit
Push to remote repository:
git push
Repository Synchronization
Clone repository when local code doesn't exist:
git clone
Fetch updates from remote:
git pull
Global configuration for user identity:
git config --global user.email "user@example.com"
git config --global user.name "username"
File Recovery
Restore deleted file that was committed:
git restore <filename>
Tag Operations
Create tag for commit:
git tag <tag_name> <commit_id>
List existing tags:
git tag
Delete tag:
git tag -d <tag_name>
Branch Operations
Create new branch:
git branch <branch_name>
Switch to branch:
git checkout <branch_name>
Merge branch into current:
git merge <source_branch>
Delete branch:
git branch -d <branch_name>
Force delete branch:
git branch -D <branch_name>
View all branches:
git branch -v
Create and switch to branch:
git checkout -b <branch_name>
Patch Operations
Apply patch file:
git am 0001-modify-contents.patch
Generate patches from recent commits:
git format-patch HEAD^
git format-patch HEAD^^ # Last 2 commits
git format-patch HEAD^^^ # Last 3 commits
Remote Repository Management
View remote URLs:
git remote -v
Discard Changes
Revert all modifications to last fetched state:
git checkout .
Revert single file:
git checkout <file_name>
Checkout remote branch:
git checkout remotes/origin/<remote_branch>
Checkout speicfic commit:
git checkout <commit_hash>
Initialization and Configuration
Initialize repository in directory:
git init
Compare file with last commit:
git diff <file_name>
Undo Operations
Discard working directory changes:
git restore <file_name>
Unstage file from staging area:
git restore --staged <file_name>
File Removal
Remove tracked file:
git rm <file_name>
Merge Operations
Create and switch to development branch:
git switch -c dev
Switch back to main branch:
git switch master
Merge development branch:
git merge dev
Non-fast-forward merge:
git merge --no-ff -m "merge message" dev
Stash Operations
Save current work state:
git stash
List stashed items:
git stash list
Restore and remove stash:
git stash pop
Cherry Pick
Apply specific commit to current branch:
git cherry-pick <commit_hash>
Multiple Remote Repositoreis
Manage multiple remotes:
git remote add github git@github.com:user/repo.git
git remote add gitee git@gitee.com:user/repo.git
git push github master
git push gitee master
Reset vs Checkout
git reset --hard <commit_id> moves both HEAD and current branch pointer to specified commit, discarding all changes made since that point. This affects the entire working directory and staging area.
git checkout <commit_id> switches HEAD pointer to specified commit while keeping branch pointers intact, allowing you to explore different states without affecting branch history.
Git Ignore
Create .gitignore file in repository root directory and add filenames to exclude them from version control.