Fundamentals 47 min read

Master Git: Essential Concepts, Commands, and Best Practices for Developers

This comprehensive guide introduces Git’s core concepts, from repositories and branches to commits, merges, rebases, and remote workflows, providing detailed command explanations, visual diagrams, and practical tips for effective version control in software development.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Master Git: Essential Concepts, Commands, and Best Practices for Developers

01. Introduction to Git – Overview

Git is the most advanced and widely used distributed version control system. It is free, open‑source, and records every change to code files, allowing restoration to any previous state. It supports cross‑region collaboration and is a fundamental skill for developers.

Main Features

Open source and free.

Powerful history management with full snapshots, not just diffs.

Distributed multi‑user collaboration; most operations are local.

Simple branch management with efficient creation and merging.

Git architecture diagram
Git architecture diagram

02. What Git Does – Basic Concepts

Key concepts include Workspace (the visible directory where you edit files), Staging Area (temporary storage for changes, the index file), Repository (the .git directory that stores all history), Remote Repository (the server‑side repository), HEAD (a pointer to the current branch’s latest commit), Commit (a snapshot with a unique ID and message), Tag (a named pointer to a specific commit), and Branch (a movable pointer to a commit).

Workspace, Staging Area, Repository

The workspace is where you add, modify, or delete files. git add moves changes to the staging area. git commit records the staged changes into the repository.

Workspace‑Staging‑Repository diagram
Workspace‑Staging‑Repository diagram

HEAD, Branches, Tags

HEAD points to the latest commit of the current branch. Branches are pointers that move as you commit. Tags are immutable pointers used to mark releases.

HEAD, branch, tag diagram
HEAD, branch, tag diagram

03. Installing and Configuring Git

Download Git from https://www.git-scm.com/ . Use the command line (git‑bash on Windows) or a GUI client.

Check Version

$ git --version
git version 2.33.0.windows.2

Global Configuration

$ git config --global user.name "Your Name"
$ git config --global user.email "[email protected]"

.gitignore

Create a .gitignore file at the repository root to exclude files or directories from tracking. Common patterns:

# Ignore all .txt files
*.txt
# Exclude lib.txt from ignore
!lib.txt
# Ignore the top‑level temp directory
/temp
# Ignore the build directory
build/

04. GUI Tools for Git

If you prefer not to use the command line, several GUI tools are available:

TortoiseGit – Windows Explorer integration.

Sourcetree – Cross‑platform, free, supports Git Flow.

GitHub Desktop – Official GitHub client.

GitKraken – Cross‑platform with free and paid tiers.

05. Basic Git Workflow

Creating a Repository

Initialize a new repository: $ git init Or clone an existing remote repository:

$ git clone https://github.com/username/project.git

Staging Changes

# Add specific files
$ git add file1 file2
# Add all changes in the current directory
$ git add .
# Remove a file and stage the removal
$ git rm file

Committing

# Commit with a message
$ git commit -m "Add feature X"
# Commit all changes (including new files) without staging
$ git commit -a -m "Quick fix"
# Amend the previous commit
$ git commit --amend -m "Updated message"

Viewing History

# Show the last 20 commits
$ git log -n20
# One‑line summary
$ git log -n20 --oneline
# Graphical view
$ git log -n20 --graph

Diff

# Show changes between workspace and staging area
$ git diff
# Show changes between staging area and HEAD
$ git diff --cached
# Show changes between two commits
$ git diff abc123 def456

06. Remote Repositories

Git is distributed; each clone has its own repository. A central remote repository is used for collaboration.

Common Remotes

Public services: GitHub, GitLab, Gitee, Coding.

Self‑hosted: GitLab, Gitea, etc.

Authentication

Two common methods:

HTTPS – username/password (or token) authentication.

SSH – public‑key authentication.

HTTPS Example

$ git clone https://github.com/user/repo.git

To avoid repeated password prompts, you can store credentials:

$ git config --global credential.helper store

SSH Setup

# Generate a key pair
$ ssh-keygen -t rsa -b 4096 -C "[email protected]"
# Add the public key (id_rsa.pub) to your Git service account.
# Test the connection
$ ssh -T [email protected]

Remote Commands

# List remotes
$ git remote -v
# Add a remote
$ git remote add origin [email protected]:user/repo.git
# Fetch changes (no merge)
$ git fetch origin
# Pull (fetch + merge)
$ git pull origin main
# Push current branch
$ git push origin HEAD
# Force push (use with caution)
$ git push -f origin main
# Delete a remote branch
$ git push origin --delete feature-branch

07. Branch Management

Creating and Listing Branches

# List local branches
$ git branch
# List remote branches
$ git branch -r
# Create a new branch
$ git branch feature-x
# Create and switch to a new branch
$ git checkout -b feature-x
# Or using the newer command
$ git switch -c feature-x

Switching Branches

# Switch to an existing branch
$ git switch feature-x
# The HEAD pointer now points to feature‑x.

Merging

Fast‑forward merge (no new commit) occurs when the target branch has no divergent history:

$ git checkout master
$ git merge dev   # Fast‑forward if master is behind dev

To force a merge commit even when fast‑forward is possible:

$ git merge --no-ff -m "Merge dev without fast‑forward" dev

When branches have diverged, Git creates a merge commit. Conflicts must be resolved manually. Conflict markers look like:

<<<<<<< HEAD
// changes from current branch
===
// changes from merged branch
>>>>>>> dev

Rebasing

Rebase rewrites commits so that a branch appears to be built directly on top of another, producing a linear history.

# Rebase dev onto master
$ git checkout dev
$ git rebase master
# Then fast‑forward merge
$ git checkout master
$ git merge dev

Tags

Tags mark specific points (usually releases). They are immutable pointers to commits.

# Create an annotated tag
$ git tag -a v1.0 -m "Release 1.0"
# List tags
$ git tag
# Push a tag
$ git push origin v1.0
# Push all tags
$ git push origin --tags

08. Undoing Changes

Working‑Tree Changes

# Discard untracked changes
$ git checkout .
# Discard changes in a specific file
$ git checkout path/to/file
# Discard both staged and unstaged changes
$ git checkout HEAD .
# Reset the staging area
$ git reset
# Reset a specific file
$ git reset path/to/file

Commit‑Level Undo

# Move HEAD to a previous commit (hard reset)
$ git reset --hard HEAD~2
# Soft reset (keep index and working tree)
$ git reset --soft HEAD~1
# Mixed reset (default) – keep working tree, unstage changes
$ git reset HEAD~1
# Revert a commit (creates a new commit that undoes the changes)
$ git revert abc123 -m "Revert faulty change"

Stash

Temporarily save uncommitted changes to switch branches safely.

# Stash current changes
$ git stash
# List stashes
$ git stash list
# Apply the most recent stash (keeps it in the list)
$ git stash apply
# Pop the most recent stash (removes it from the list)
$ git stash pop
# Drop a specific stash
$ git stash drop stash@{0}

Cherry‑Pick

Copy a specific commit from another branch without merging the whole branch.

# Apply commit abc123 onto the current branch
$ git cherry-pick abc123

09. Git Flow Workflow

Git Flow defines a branching model for robust software development:

master – production‑ready code; only receives merges from release and hotfix branches.

develop – integration branch for features; daily development occurs here.

feature/* – short‑lived branches for individual features; merged back into develop.

release/* – preparation for a new production release; merged into master (tagged) and back into develop.

hotfix/* – urgent fixes on production; merged into both master and develop.

Typical Commands

# Start a new feature
$ git checkout -b feature/login develop
# Finish a feature
$ git checkout develop
$ git merge --no-ff feature/login
$ git branch -d feature/login

# Start a release
$ git checkout -b release/1.2 develop
# After testing, merge release
$ git checkout master
$ git merge --no-ff release/1.2
$ git tag -a v1.2 -m "Release 1.2"
$ git checkout develop
$ git merge --no-ff release/1.2
$ git branch -d release/1.2

# Hotfix
$ git checkout -b hotfix/1.2.1 master
# After fixing
$ git checkout master
$ git merge --no-ff hotfix/1.2.1
$ git tag -a v1.2.1 -m "Hotfix 1.2.1"
$ git checkout develop
$ git merge --no-ff hotfix/1.2.1
$ git branch -d hotfix/1.2.1

10. Additional Tips

GUI Clients Overview

TortoiseGit – Windows Explorer integration.

Sourcetree – Free, supports Git Flow.

GitHub Desktop – Official client for GitHub.

GitKraken – Feature‑rich cross‑platform client.

VS Code Integration

VS Code includes built‑in Git support. Extensions such as GitLens and Git History enhance visualization of commit history and blame information.

Best Practices

Commit frequently with clear messages.

Use branches for isolated work.

Keep the master branch stable; tag releases.

Prefer git pull --rebase for a linear history.

Never rewrite public history; use git revert for published commits.

By mastering these concepts and commands, developers can manage code efficiently, collaborate safely, and maintain a clean project history.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GitmergerebaseVersion Controlbranching
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

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.