Fundamentals 29 min read

Master Git: From Basic Concepts to Advanced Command-Line Techniques

This comprehensive guide walks you through Git fundamentals, practical workflows, and deep‑dive concepts—including installation, repository setup, core commands, branching, merging, stashing, rebasing, and troubleshooting—providing clear examples and code snippets to help developers confidently manage version control in any project.

Open Source Linux
Open Source Linux
Open Source Linux
Master Git: From Basic Concepts to Advanced Command-Line Techniques

Git Introduction

Git

is a distributed version control system that works offline and offers many advantages, making it the preferred choice for project version management among developers and even non‑developers for document versioning.

Starting from zero exposure in college, the author now heavily relies on Git and is impressed by its power.

Although Git has a rich API, most projects only need a few basic commands; this article focuses on pragmatic usage and deep exploration of Git. Reading the pragmatic section is enough to start using Git in a project.

Note: All operations in this article are based on macOS.

Pragmatic

Preparation Stage

Download the appropriate installer from the Git website, install Git, open a terminal, navigate to a working folder (demonstrated on the desktop), and create a new demo folder.

Git installation screenshot
Git installation screenshot

Register a GitHub account, go to your repository, click Clone or download, then Use HTTPS, and copy the project URL https://github.com/gafish/gafish.github.com.git for later use.

Now everything is ready; the following sections cover the core operations.

Common Operations

The pragmatic approach covers the following essential Git commands, which satisfy over 90% of typical needs:

git clone
git config
git branch
git checkout
git status
git add
git commit
git push
git pull
git log
git tag

The following example demonstrates cloning the repository, checking out a branch, making changes, and pushing them.

git clone

git clone https://github.com/gafish/gafish.github.com.git

After cloning, a gafish.github.com directory appears. Enter it with:

cd gafish.github.com

git config

git config user.name gafish</code>
<code>git config user.email [email protected]

These settings attach your name and email to future commits.

git branch

git branch daily/0.0.0

Create a development branch named daily/0.0.0. Rename it if needed: git branch -m daily/0.0.0 daily/0.0.1 List branches with git branch. Delete a branch after its purpose is finished:

git branch -d daily/0.0.1

git checkout

git checkout daily/0.0.1

Switch to the newly created branch.

git status

git status

Shows modified files that are not yet staged.

git add

git add README.md

Stage the modified file. Use git add . to stage all changes.

git commit

git commit -m 'Add initial README changes'

Commit the staged changes with a message.

git push

git push origin daily/0.0.1

Push the branch to the remote repository. Successful push is confirmed by the command output.

git pull

git pull origin daily/0.0.1

Pull the latest changes from the remote branch. If conflicts arise, resolve them manually, then stage and commit the resolution.

git log

git log

View the commit history, including author, date, and message.

git tag

git tag publish/0.0.1</code>
<code>git push origin publish/0.0.1

Tag a milestone (e.g., publish/0.0.1) and push the tag to the remote.

.gitignore

touch .gitignore

Add patterns (e.g., demo.html, build/) to exclude files or directories from being tracked.

Summary

Mastering these basic commands enables you to start using Git effectively. For most everyday tasks, the above commands are sufficient; occasional issues can be solved via search. For advanced features, continue to the deep‑exploration section.

Deep Exploration

Basic Concepts

Working Directory

The visible folder on your computer (e.g., gafish.github.com) is the working directory.

Working directory illustration
Working directory illustration

Local Repository

The hidden .git folder inside the working directory stores the repository data.

Local repository diagram
Local repository diagram

Staging Area

The stage (or index) holds files that are ready to be committed, along with the automatically created master branch and its HEAD pointer.

Remote Repository

The remote repository resides on a server such as GitHub.

Remote repository illustration
Remote repository illustration

Relationship Diagram

Git workflow diagram
Git workflow diagram

Branch

Branches isolate development work, allowing multiple parallel changes.

Branch illustration
Branch illustration

Master Branch

The automatically created master (or main) branch serves as the primary line of development; other branches are merged back into it.

Master branch illustration
Master branch illustration

Tag

Tags mark specific points in history, often used for releases (e.g., publish/0.0.1).

HEAD

HEAD

points to the latest commit of the current branch.

HEAD pointer
HEAD pointer
Now that you understand these concepts, you can explore advanced Git usage.

File Operations

git add

git add -i

Interactive mode shows commands such as status, update, revert, etc. git add -p Enter patch mode to selectively stage changes. git add -u Stage modified or deleted files (new files are not included). git add --ignore-removal . Stage modified and new files while ignoring deletions.

git commit

git commit -m 'First line of commit message' -m 'Second line'

Commit with a multi‑line message without opening an editor. git commit -am 'Commit message' Commit all tracked changes directly. git commit --amend -m 'Updated commit message' Amend the most recent commit.

git mv

git mv a.md b.md -f

Rename a file and stage the change; -f forces the rename.

git rm

git rm b.md

Remove a file from both the working directory and the index. git rm src/ -r Remove a directory recursively.

git status

git status -s

Show a short summary of file states. git status --ignored Include ignored files in the status output.

Branch Operations

git branch

git branch -a

List all local and remote branches. git branch -r List remote branches only. git branch -D Force‑delete an unmerged local branch. git branch -vv Show detailed information for each local branch.

Branch list illustration
Branch list illustration

git merge

git merge --squash

Combine all commits from another branch into a single commit on the current branch. git merge --no-ff Create a merge commit even when a fast‑forward is possible. git merge --no-edit Merge without opening an editor for the commit message.

git checkout

git checkout -b daily/0.0.1

Create and switch to a new branch. git checkout HEAD demo.html Restore demo.html from the latest commit. git checkout --orphan new_branch Create a new branch with no history. git checkout -p other_branch Interactively compare and select changes between branches.

git stash

git stash

Save uncommitted changes to a stack. git stash list List saved stashes. git stash pop Apply the latest stash and remove it from the stack. git stash apply stash@{0} Apply a specific stash without dropping it. git stash branch new_branch Create a new branch from the latest stash. git stash clear Remove all stashes.

History Operations

git log

git log -p

Show commit history with diffs. git log demo.html Show history for a specific file. git log --since="2 weeks ago" Show commits from the last two weeks. git log -10 Show the most recent ten commits. git log --pretty=oneline One‑line summary of each commit.

git cherry-pick

git cherry-pick 170a305

Apply a specific commit onto the current branch.

git reset

git reset --mixed <commit>

Reset to a commit, keeping working files unchanged. git reset --soft <commit> Move HEAD without touching the index or working tree. git reset --hard <commit> Discard all changes and reset to the specified commit.

git rebase

git rebase branch_name

Reapply commits onto another base branch. git rebase -i HEAD~2 Interactive rebase to edit, squash, or reorder commits.

git revert

git revert HEAD

Create a new commit that undoes the changes introduced by the specified commit. git revert HEAD --no-edit Revert without opening an editor. git revert -n HEAD Stage multiple revert operations before committing.

git diff

git diff --stat

Show a summary of changes.

git reflog

View the reference log of all actions, including those that are no longer reachable via git log.

Remote Repository Connection

git init

Creates a .git directory in the current folder.

git remote

git remote -v

List existing remotes with URLs.

git remote add origin https://github.com/gafish/gafish.github.com.git

Add a remote named origin.

git fetch

git fetch origin daily/0.0.1

Retrieve updates for a specific remote branch.

Problem Diagnosis

git blame

git blame -L 1,10 demo.html

Show line‑by‑line attribution for the first ten lines of demo.html.

git bisect

git bisect start

Start a binary search for a bug. git bisect bad Mark the current commit as bad. git bisect good Mark the current commit as good. git bisect reset Return to the original branch after locating the problematic commit.

Additional Operations

git submodule

git submodule add https://github.com/gafish/demo.git demo

Add a submodule repository. git submodule update demo Update the submodule.

git gc

Run garbage collection to clean up unnecessary files and optimize the repository.

git archive

git archive -v --format=zip v0.1 > v0.1.zip

Create a zip archive of the repository at tag v0.1.

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.

command-linebranchingMergingrebasing
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

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.