Comprehensive Guide to Git: Installation, Commands, Branch Management, and Remote Collaboration
This article provides a thorough tutorial on Git, covering environment installation, core concepts, common commands for adding, committing, and checking status, branch creation and merging, file operations, diff and log viewing, remote repository handling, SSH key setup, multi‑person collaboration workflow, remote branch management, force pushes, and merge strategies with conflict resolution.
Git is a distributed version control system that supports offline operations and efficient version management.
Installation is straightforward across operating systems; after installing, run git --version to verify.
Basic Concepts
Working Directory : the area containing the files you edit.
Staging Area : temporary storage for changes before committing.
Repository : stores all version history, both locally and remotely.
These concepts form the foundation for all Git operations.
File Adding and Committing
Use git add <filename> to stage files and git commit -m "message" to record them in the local repository.
# Example: add and commit a file
# 1. Add file to staging area
git add example.txt
# 2. Commit to local repository
git commit -m "Initial commit for example.txt"Check status with git status .
# View status
git statusStorage Flow
The typical workflow is: modify files → git add → git commit → git push to a remote repository.
# Common storage flow
git add .
git commit -m "Update files"
git push origin mainVersion Management Principle
Git records snapshots of the file system; each commit is identified by a unique hash, enabling fast history traversal and comparison.
Initial Configuration
Set user name and email before committing:
# Set user name and email
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
# Initialize a new repository
git initBranch Management
Create and switch branches with git branch <branch-name> and git checkout <branch-name> .
# Create and switch to a new branch
git branch feature-xyz
git checkout feature-xyzMerge a branch into the current branch using git merge <branch-name> .
# Merge feature-xyz into main
git checkout main
git merge feature-xyzFile Deletion, Restoration, and Version Switching
Delete a file with git rm <filename> , restore staged files with git restore --staged <filename> , or revert to the last commit with git checkout -- <filename> .
# Delete and restore a file
git rm example.txt
git commit -m "Remove example.txt"
# Restore the deleted file
git restore --staged example.txtSwitch to a specific commit using git checkout <commit-hash> .
# Switch to a specific commit
git checkout 1a2b3c4dFile Diff and Log
Compare changes with git diff <filename> and view commit history with git log or git log --oneline .
# Show differences
git diff example.txt
# View full log
git log
# One‑line log
git log --onelineRemote Repository Operations
Add a remote with git remote add <name> <url> and view remotes with git remote -v .
# Add remote repository
git remote add origin https://github.com/your-repo.gitPush, pull, and fetch changes:
# Push to remote
git push origin main
# Pull from remote
git pull origin main
# Fetch remote branches
git fetch originGitee (码云) and SSH Key Pairing
Use SSH keys for secure authentication; generate a key pair with ssh-keygen -t rsa -b 4096 -C "[email protected]" and add the public key to Gitee.
Collaboration Workflow
Typical multi‑person workflow: pull main, create a feature branch, develop, commit, push, open a merge request, resolve conflicts, and merge back.
# Collaboration example
git checkout main
git pull origin main
git branch feature-abc
git checkout feature-abc
git add .
git commit -m "Develop feature-abc"
git push origin feature-abc
git checkout main
git merge feature-abcRemote Branch Management and Force Push
Delete a remote branch with git push origin --delete <branch> and force‑push with git push --force when necessary.
# Delete remote branch
git push origin --delete feature-xyz
# Force push
git push --force origin mainMerge Strategies and Conflict Resolution
Use strategies like --no-ff or --squash for clearer history. Resolve conflicts by editing files, adding them, and committing.
# Non‑fast‑forward merge
git merge --no-ff feature-abc
# Resolve conflict
git add conflict-file.txt
git commit -m "Resolve merge conflict"Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.