Master Git vs SVN: Essential Commands and Git‑SVN Workflow
This article compares Git and SVN, explains the core concepts of Git such as workspace, index, and repository, and provides a comprehensive set of frequently used Git commands—including initialization, configuration, branching, committing, pulling, and Git‑SVN operations—complete with code snippets and illustrative images.
Git vs SVN
Git and SVN each have their own strengths; Git is distributed while SVN is centralized.
Git is distributed, allowing offline work and cheap branching; SVN requires network access and its branches are copies of directories, making them expensive.
Git commands are numerous (add, commit, status, fetch, push, rebase, merge, cherry-pick, submodule, stash, etc.), while SVN is simpler for beginners.
Git Core Concepts
Workspace: the actual directory on your computer.
Index (staging area): a temporary cache for changes.
Repository: can be local or remote.
Switching from SVN to Git requires understanding the index and local repository, which greatly simplify workflow.
Typical commit workflow:
git add – move changes from workspace to index.
git commit – move changes from index to local repository.
git push or git svn dcommit – upload local repository to remote.
Common Git commands (image):
Git‑SVN Commands
When the server uses SVN but you want Git’s local branching, use Git‑SVN:
# Clone an SVN project with full history and initialize a Git repo
git svn clone -s [repository]
# Show repository info
git svn info
# Fetch all branch changes
git svn fetch
# Rebase local branch with remote changes
git svn rebase
# Upload local branch to remote
git svn dcommit
# Create a new remote branch
svn copy [remote_branch] [new_remote_branch] -m [message]
# Create a local branch tracking the remote branch
git checkout -b [local_branch] [remote_branch]Initialization
# Initialize a new Git repository in the current directory
git init
# Clone a project with full history (Git only)
git clone [url]Configuration
# List all configurations
git config -l
# Set command aliases
git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.br branch
# Set user information
git config [--global] user.name "[name]"
git config [--global] user.email "[email address]"Add / Delete Files
# Add all files in the current directory
git add .
# Add specific files
git add <file1> <file2> ...
# Add a directory recursively
git add <dir>
# Remove files and stage the deletion
git rm [file1] [file2] ...
# Stop tracking a file but keep it in the workspace
git rm --cached [file]
# Rename a file and stage the change
git mv [file-original] [file-renamed]Branch Management
# List local branches
git branch
# List all branches (local and remote)
git branch -a
# Create a new branch
git branch [branch-name]
# Create and switch to a new branch
git checkout -b [new_branch] [remote-branch]
# Switch to an existing branch
git checkout [branch-name]
# Merge a branch into the current branch
git merge [branch]
# Cherry‑pick a specific commit
git cherry-pick [commit]
# Delete a local branch
git branch -d [branch-name]
# Delete a remote branch
git push [remote] :[remote-branch]Commit Operations
# Commit staged changes with a message
git commit -m [message]
# Commit all changes directly
git commit -a
# Show diff in commit
git commit -v
# Amend the previous commit
git commit --amend -m [message]
# Push a branch to remote
git push [remote] [remote-branch]Pulling Changes
# Fetch all changes from remote (Git only)
git fetch [remote]
# Show remote repositories
git remote -v
# Show details of a remote
git remote show [remote]
# Add a new remote
git remote add [remote-name] [url]
# Pull and merge changes
git pull [remote] [branch]
# Pull and rebase changes
git pull --rebase [remote] [branch]Reverting and Stashing
# Restore a file from index to workspace
git checkout [file]
# Restore all files in current directory
git checkout .
# Reset a file to the last commit
git reset [file]
# Reset index and workspace to last commit
git reset --hard
# Reset branch pointer to a specific commit
git reset [commit]
# Reset branch and workspace to a specific commit
git reset --hard [commit]
# Revert a commit
git revert [commit]
# Stash uncommitted changes
git stash
# Apply stashed changes
git stash popQuerying Repository
# Show status of workspace
git status
# Show changes in a file
git diff [file]
# Show staged changes
git diff --cached [file]
# Show commit log
git log
# Filter log by author
git log --author=someone
# Show file history with patches
git log -p [file]
# Show details of a specific commit
git show [commit]In practice, Git is used more frequently than SVN because of its richer feature set.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
