Master Git vs SVN: Essential Commands and Workflow for Developers
This guide compares Git and SVN, explains their core concepts and differences, and provides a comprehensive set of commands for using Git, Git‑SVN, and common version‑control tasks such as initialization, configuration, branching, committing, pulling, undoing changes, and querying history.
Git vs SVN
Git is a distributed version‑control system (DVCS) while Subversion (SVN) is centralized. Git enables offline work, cheap branching, and local branches; SVN requires network access and creates branches by copying directories, which is slower and more storage‑intensive. Mastering Git’s concepts provides greater flexibility despite a larger command set.
Core Git Concepts
The basic workflow revolves around three areas:
Workspace : the actual directory on your computer containing the source files.
Index (Staging Area) : a temporary cache where changes are staged before they become a commit.
Repository : stores commits; it can be local (on your machine) and/or remote (on a server).
Typical commit sequence: git add [file] – move changes from the workspace to the index. git commit -m "[message]" – record the index as a new commit in the local repository. git push [remote] [remote-branch] (or git svn dcommit) – send local commits to the remote repository.
Git‑SVN Workflow
When the upstream server is SVN but you want Git‑style offline work, use the git svn bridge. The most common commands are:
# Clone an SVN project (full history) as a Git repository
$ git svn clone -s [repository]
# Show repository information
$ git svn info
# Fetch all remote branches
$ git svn fetch
# Rebase local changes onto the latest remote branch
$ git svn rebase
# Commit local changes back to SVN
$ git svn dcommit
# Create a new remote branch in SVN
$ svn copy [remote-branch] [new-remote-branch] -m "[message]"
# Checkout the new remote branch as a local Git branch
$ git checkout -b [local-branch] [remote-branch]Repository Initialization
# Initialise an empty Git repository in the current directory
$ git init
# Clone an existing remote repository
$ git clone [url]Configuration
# List all configuration entries
$ git config -l
# Define useful aliases (global)
$ 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 identity (global)
$ git config --global user.name "[name]"
$ git config --global user.email "[email address]"Global settings are stored in ~/.gitconfig; repository‑specific settings reside in ~/.git/config.
Add / Remove Files
# Add all files in the current directory
$ git add .
# Add specific files
$ git add [file1] [file2] ...
# Add a directory (including sub‑directories)
$ git add [dir]
# Remove files and stage the deletions
$ 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 [old-name] [new-name]Branch Management
# List local branches
$ git branch
# List local and remote branches
$ git branch -a
# Create a new branch (stay on current 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 one
$ git merge [branch]
# Cherry‑pick a specific commit
$ git cherry-pick [commit]
# Delete a local branch (force with -D if needed)
$ git branch -d [branch-name]
# Delete a remote branch
$ git push [remote] :[remote-branch]Committing
# Commit staged changes with a message
$ git commit -m "[message]"
# Commit all changes (including unstaged) directly
$ git commit -a
# Show diff in the commit output
$ git commit -v
# Amend the previous commit
$ git commit --amend -m "[new message]"
# Push a local branch to the remote repository
$ git push [remote] [remote-branch]Pulling and Fetching
# Fetch all changes from a remote (Git only)
$ git fetch [remote]
# Show remote URLs
$ git remote -v
# Show details of a specific remote
$ git remote show [remote]
# Add a new remote repository
$ git remote add [remote-name] [url]
# Pull and merge changes (Git only)
$ git pull [remote] [branch]
# Pull and rebase changes (Git only)
$ git pull --rebase [remote] [branch]Undoing Changes
# Restore a file from the index to the workspace
$ git checkout [file]
# Restore all files in the current directory
$ git checkout .
# Checkout a specific commit
$ git checkout [commit]
# Reset a file in the index to the last commit (workspace unchanged)
$ git reset [file]
# Reset index and workspace to the last commit
$ git reset --hard
# Move branch pointer to a specific commit (index reset, workspace unchanged)
$ git reset [commit]
# Reset both index and workspace to a specific commit
$ git reset --hard [commit]
# Revert a specific commit (creates a new commit)
$ git revert [commit]
# Stash uncommitted changes
$ git stash
# Apply stashed changes back to the workspace
$ git stash popQuerying Repository State
# Show modified files in the workspace
$ git status
# Show detailed changes in a file
$ git diff [file]
# Show staged changes
$ git diff --cached [file]
# Show commit history
$ git log
# Filter log by author
$ git log --author="[author]"
# Show changes for a specific file with patches
$ git log -p [file]
# Show details of a specific commit
$ git show [commit]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.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
