Fundamentals 17 min read

Comprehensive Guide to Common Git Commands

This article provides a detailed overview of essential Git commands—including configuration, repository initialization, cloning, adding, committing, branching, merging, remote operations, diffing, logging, tagging, undoing changes, and submodule management—to help developers efficiently manage version control in software projects.

Top Architecture Tech Stack
Top Architecture Tech Stack
Top Architecture Tech Stack
Comprehensive Guide to Common Git Commands

Git is a widely used distributed version control system that plays a crucial role in software development, project collaboration, and code management for both individual developers and large teams.

The article introduces common Git commands organized by functionality, enabling readers to master version control, branch management, code merging, and history inspection.

Basic Configuration Commands

Command format: git config [--global | --local | --system] [--unset] option value

Function: Sets various Git configuration items such as user information, editor, and merge tools. Options include --global (global config), --local (default local config), --system (system-wide config), and --unset (remove a config).

Examples:

Set global username: git config --global user.name "John Doe"

Set global email: git config --global user.email "[email protected]"

Set local editor to VS Code: git config --local core.editor "code --wait"

Repository Initialization and Cloning

git init – git init [directory] creates a new empty Git repository in the specified directory (or current directory if omitted), adding a hidden .git folder.

git clone – git clone [--depth ] [--branch ] [ ] copies a remote repository locally, with options for shallow clones ( --depth ) and specific branches ( --branch ).

Adding and Committing Changes

git add – git add [--all | --patch | ] stages file modifications. --all (or -A ) adds all changes, --patch (or -p ) allows interactive selection, and specifying a pattern adds matching files.

Example: git add -A

git commit – git commit [-m | --amend] creates a new commit from staged changes. -m provides a commit message; --amend modifies the previous commit.

Example: git commit -m "Add new feature X"

Branch Management

git branch – git branch [--list | -a | -r | -d | -D | ] lists, creates, or deletes branches. Options include -l (list local), -a (list all), -r (list remote), -d (delete merged), and -D (force delete).

Examples: git branch -l , git branch new-feature , git branch -D unwanted-branch

git checkout – git checkout [--branch | -b | | ] switches branches or restores files. -b creates a new branch while switching.

Examples: git checkout master , git checkout -b experimental

git merge – git merge merges the specified branch into the current one, handling conflicts if they arise.

git rebase – git rebase [--onto | ] [ ] rewrites commit history onto another base, producing a linear history.

Remote Repository Operations

git remote – git remote [--add | --remove | --rename | -v | ] manages remote repositories (add, remove, rename, view).

Examples: git remote add origin https://github.com/username/my-project.git , git remote --remove old-remote , git remote -v

git fetch – git fetch [--all | | / ] retrieves updates from remote without merging.

Example: git fetch --all

git pull – git pull [--rebase | [ ]] fetches and merges (or rebases) remote changes into the current branch.

Example: git pull origin

git push – git push [--force | --tags | [ ]] uploads local commits (or tags) to a remote repository.

Example: git push origin master

Diff, Log, and Show

git diff – git diff [--cached | | | ] displays differences between commits, branches, or files.

Example: git diff --cached

git log – git log [--oneline | --graph | --author | --since | --until | | ] shows commit history with various formatting options.

Example: git log --oneline

git show – git show [--stat | ] displays detailed information about a specific commit, optionally including file change statistics.

Example: git show --stat abc123

Tag Management

git tag – git tag [--list | -l | --delete | [ ]] creates, lists, or deletes tags.

Examples: git tag -l , git tag v1.0 , git tag --delete old-tag

git push --tags – git push --tags pushes all local tags to the remote repository.

Undo and Revert

git reset – git reset [--soft | --mixed | --hard | ] moves the HEAD pointer and optionally updates the index and working tree.

Examples: git reset --soft HEAD~1 , git reset --hard HEAD~1

git revert – git revert creates a new commit that undoes the changes introduced by the specified commit, preserving history.

Submodule Management

git submodule add – git submodule add adds an external repository as a submodule.

git submodule init – git submodule init initializes submodule configuration after cloning.

git submodule update – git submodule update [--remote | ] updates submodules to a specific commit or the latest remote version.

Example: git submodule update --remote

Mastering these Git commands is essential for efficient version control and collaborative software development.

software developmentgitcommand lineVersion ControlGit Basics
Top Architecture Tech Stack
Written by

Top Architecture Tech Stack

Sharing Java and Python tech insights, with occasional practical development tool tips.

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.