Git Version Control and Basic Usage in Practice

Version control systems are essential for modern software development, enabling teams to track changes, collaborate effectively, and maintain project history. Git, as the most widely adopted distributed version control system, offers powerful capabilities for managing code across teams of any size.

Understanding Git

What is Git?

Git originated in 2005, created by Linus Torvalds to manage Linux kernel development. Unlike centralized version control systems, Git operates as a distributed system where every developer maintains a complete copy of the repository history on their local machine.

Key Advantages

  • Speed: Local operations execute instantly since everything happens on your machine.
  • Offline Capability: Full functionality remains available without network connectivity.
  • Data Integrity: Cryptographic hashing ensures data integrity and prevents corruption.
  • Flexible Branching: Branch creation and merging are lightweight operations.
  • Strong Community: Extensive documentation, tutorials, and tooling support.

Installation on Windows

To install Git on Windows, download the installer from git-scm.com. Run the executable and follow the default installation steps. After installation completes, verify the setup by opening Command Prompt and running:

git --version

The command should display the installed Git version number, confirming successful installation.

Remote Repository Setup

Creating a Remote Repository

For demonstration purposes, we will use Gitee (gitee.com), which provides excellent accessibility for users in certain regions. After creating an account, create a new repository through the web interface. Consider these points when creating your repository:

  • Public repositories allow access via HTTPS without authentication requirements.
  • Private repositories require SSH key authentication for secure access.
  • Initialize the repository during creation to avoid working with empty repositories.

Configuring SSH Authentication

For private repositories, SSH key authentication provides secure, password-free access. Generate an SSH key pair by opening your terminal and executing:

ssh-keygen -t ed25519 -C "your_description_here"

Press Enter four times to except default locations and leave passwords empty. The generated key files will be stored in your user directory under .ssh. View the public key by opening the id_ed25519.pub file in a text editor. Copy its contents and navigate to your Gitee account settings to add the public key under SSH Keys section.

Configuring Git Identity

Set your identity information globally, which applies to all repositories on your machine:

git config --global user.name "Developer Name"
git config --global user.email "developer@example.com"

To display your current configuration:

git config user.name
git config user.email
git config --list

Core Git Operations

Understanding the Three Areas

Git manages files through three distinct areas:

Working Directory: Your project folder containing all files you actively edit. Changes occur directly in this area.

Staging Area (Index): A holding zone where changes await commit. Use git add to move modifications from the working directory to staging.

Repository: The complete history of your project, stored in the .git directory. Execute git commit to permanently save staged changes to the repository.

Essential Commands

Adding Files to Staging:

git add main.py
git add .
git add -u

The first command stages a specific file. The second stages all changes in the current directory. The third stages modifications to tracked files only, excluding new files.

Committing Changes:

git commit -m "Implement user authentication module"

Checking Repository Status:

git status

This displays which files are modified, staged, or untracked.

Viewing Commit History:

git log
git log --oneline
git log -p

The first shows complete history. The second displays condensed one-line entries. The third shows detailed diffs for each commit.

Remote Operations

Managing Remote References:

git remote -v
git remote add upstream https://gitee.com/project/repo.git

Pushing Local Changes:

git push origin main
git push -u origin main

The second command sets the upstream tracking relationship.

Pulling Remote Changes:

git pull origin main
git pull -f origin v2.0:main

The second command forces synchronization, replacing local history with remote content.

Fetching Without Merging:

git fetch origin

Download changes without modifying your working directory.

Branch Management

Why Branches Matter

Branches create parallel development paths within the same repository. Each branch represents an independent line of development, allowing multiple features to progress simultaneously without interference.

Creating and Switching Branches

git branch feature-login
git checkout feature-login
git switch -c feature-login

The first creates a branch. The second switches to it. The third creates and immediately switches to the new branch (Git 2.23+).

Understanding Merge vs Rebase

Merge combines branch histories by creating a merge commit. It preserves the complete history but creates a branching structure:

git checkout main
git merge feature-login

Rebase re-applies commits on top of another branch, creating a linear history:

git checkout feature-login
git rebase main

Rebase rewrites commit history, so avoid rebasing commits that have been shared with others.

Advanced Git Features

Tagging Releases

Tags mark specific points in history, typically used for releases:

git tag v1.0.0
git tag -a v1.0.0 -m "First stable release"
git tag -a v0.9 9fceb02 -m "Beta version"

Push tags to remote:

git push origin v1.0.0
git push origin --tags

Resolving Merge Conflicts

When two branches modify the same lines, Git cannot auto-merge. Conflicts appear as:

<<<<<<< HEAD
local changes
=======
remote changes
>>>>>>> branch-name

Manual edit the file to keep desired changes, then:

git add resolved-file.py
git commit -m "Resolve merge conflict"

Git Hooks

Hook are scripts that execute at specific points during Git operations. They live in .git/hooks/ directory. Common hooks include:

  • pre-commit: Run tests before committing
  • commit-msg: Validate commit message format
  • pre-push: Execute checks before pushing

To activate a hook, remove the .sample suffix from the hook file and add your script content.

Tags: Git version-control git-branching remote-repositories ssh-keys

Posted on Thu, 05 Nov 2026 16:41:05 +0000 by My220x