Fundamentals 13 min read

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.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Comprehensive Guide to Git: Installation, Commands, Branch Management, and Remote Collaboration

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 status

Storage 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 main

Version 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 init

Branch 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-xyz

Merge a branch into the current branch using git merge <branch-name> .

# Merge feature-xyz into main
git checkout main
git merge feature-xyz

File 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.txt

Switch to a specific commit using git checkout <commit-hash> .

# Switch to a specific commit
git checkout 1a2b3c4d

File 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 --oneline

Remote 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.git

Push, pull, and fetch changes:

# Push to remote
git push origin main

# Pull from remote
git pull origin main

# Fetch remote branches
git fetch origin

Gitee (码云) 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-abc

Remote 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 main

Merge 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"
gitcommand lineversion controlbranch managementRemote Repository
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.