Fundamentals 23 min read

Comprehensive Git Guide: Installation, Repository Basics, Branching, Tagging, and Common Commands

This article provides a detailed, beginner‑friendly tutorial on Git, covering its purpose, installation on Windows and macOS, repository structure, essential commands for committing, branching, merging, rebasing, stashing, tagging, and collaborative workflows, all illustrated with clear examples and code snippets.

Java Captain
Java Captain
Java Captain
Comprehensive Git Guide: Installation, Repository Basics, Branching, Tagging, and Common Commands

Preface

This article is based on Liao Xuefeng's Git materials and my own understanding, documenting my Git learning journey for future reference; corrections and suggestions are welcome.

What is Git

Officially, Git is a free, open‑source distributed version‑control system designed to handle projects of any size efficiently.

According to Liao Xuefeng, it automatically records file changes, enables collaborative editing, and lets you view any change with a quick glance.

Why Learn Git

It is frequently asked in interviews.

Most companies use Git for project development.

Mastering Git commands improves development efficiency.

Installing Git

Windows

Download from the official website. After installation, right‑click in any folder; if "Git Bash Here" appears, the installation succeeded. Then configure your identity:

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

The --global flag applies the settings to all repositories on the machine.

macOS

You can install Git the same way as Windows, or install Xcode from the App Store, then enable Command Line Tools via Xcode → Preferences → Downloads → Install.

Repository

A local repository consists of a working directory and a version area (.git). The working directory holds the actual files, while the version area contains the staging area, branches, and HEAD pointer.

Working directory: the set of files on disk.

Version area: the .git directory.

Version area = staging area + branch (master) + HEAD pointer.

Typical workflow example: git init – create a .git directory. git add . – stage all files. git commit -m "message" – commit staged files. git remote add origin https://github.com/username/repo.git – link to a remote repository. git push -u origin master – push local commits to GitHub.

Committing to GitHub

Earlier, I used the GitHub web UI to upload files. Now the typical command sequence is: git init – initialize repository. git add . – stage all files. git add ./readme.md/ – stage a specific file. git commit -m "your comment" – commit. git remote add origin https://github.com/username/repo.git – set remote. git push -u origin master – push to GitHub.

Version Rollback and Forward

Use git reset --hard <commit-id> to revert to a previous version; the commit ID can be found with git log. To move forward again, run the same reset command with a newer commit ID. git reflog records all commands, helping you locate lost commit IDs.

Undo

Scenario 1: Undo changes in the working directory with git checkout -- <file>, restoring the file to the version stored in the repository.

Scenario 2: If the file is already staged, reset to the previous commit and then checkout the file.

Scenario 3: If the change has been committed, reset to the previous commit and then checkout the file.

Delete

If you added a file to the staging area and then deleted it locally, Git records the deletion. To delete a file from the repository, use git rm followed by a commit.

If you accidentally delete a file locally, restore it with git checkout -- <file>.

Branch

A branch is like a parallel universe. You can create a private branch, work on it, and later merge it back to the main branch without affecting others.

Create and Merge Branches

Typical commands:

git branch other
git checkout other

List branches: git branch Current branch is marked with *.

Commit on other:

git add ./xxx/
git commit -m "xxx"

Switch back to master: git checkout master Merge: git merge other Delete the merged branch:

git branch -d other

Resolving Merge Conflicts

If other was committed and then master received new commits, a conflict may arise. Resolve it by editing the files to be consistent, then git add and git commit. Finally, merge the branches.

Branch Management Strategy

Use git merge --no-ff other to disable fast‑forward merges, preserving branch history.

Bug Branch

When a bug appears, create a temporary bug branch, fix it, merge, and delete the branch. If you need to switch to another branch while working, stash your changes with git stash , fix the bug, then restore your work.

Restore stashed work: git stash apply – restore without deleting the stash. git stash pop – restore and delete the stash.

Delete Branch

git branch -d <branch>

– safe delete (fails if unmerged). git branch -D <branch> – force delete.

Collaboration

git remote

– view remote information (default name: origin). git remote -v – detailed view. git push -u origin master – push master. git push -u origin other – push other branch.

Fetching Branches

When conflicts appear, run git pull to fetch and merge remote changes. If git pull fails, follow Git’s instructions to set up the correct upstream branch.

Rebase

git rebase

rewrites a divergent history into a linear one, making the log easier to read, though it rewrites commit IDs.

Tag Management

Tags mark specific points (e.g., releases) in history. They are pointers to commits.

Create Tags

Switch to master (or any branch) and run git tag <name>, e.g., git tag v1.0.

To tag an older commit, find its ID with git log and run git tag v1.0 <commit-id>.

List tags with git tag; they are sorted alphabetically.

Show tag details with git show <tagname>.

Create an annotated tag: git tag -a <name> -m "description".

Operate Tags

Delete a local tag: git tag -d v1.0.

Push a tag: git push origin <tagname>.

Push all tags: git push origin --tags.

Delete a remote tag: git push origin :refs/tags/v1.0.

Custom Git Settings

Enable colored output: git config --global color.ui true.

Create a .gitignore file to ignore files such as node_modules, compiled binaries, OS‑generated files, or sensitive config files.

Force‑add an ignored file: git add -f <file>.

Check why a file is ignored: git check-ignore -v <file>.

Define command aliases, e.g., git config --global alias.nb rebase so git nb runs git rebase.

Common Git Commands Summary

git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git init
git add .
git add ./<file>/
git commit -m "message"
git remote add origin https://github.com/username/repo.git
git push -u origin master
git push -u origin <other-branch>
git status
git diff
git log
git clone <repo-url>
git reset --hard <commit-id>
git reflog
git checkout -- <file>
git rm <file>
git branch
git branch <branch-name>
git checkout <branch-name>
git merge <branch-name>
git branch -d <branch-name>
git branch -D <branch-name>
git log --graph
git merge --no-ff <branch-name>
git stash
git stash list
git stash apply
git stash drop
git stash pop
git remote
git remote -v
git pull
git rebase
git tag
git tag <name>
git tag <tagName> <commit-id>
git show <tagName>
git tag -a <tagName> -m "description"
git tag -d <tagName>
git push origin <tagname>
git push origin --tags
git push origin :refs/tags/<tagname>
git config --global color.ui true
git add -f <file>
git check-ignore -v <file>

Conclusion

Liao Xuefeng's explanations of Git are clear and beginner‑friendly; spending a couple of days organizing this material will be rewarding.

Visit the author's website for more resources:

https://www.liaoxuefeng.com/

(End)

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-lineVersion ControlCollaborationTagging
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

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.