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.
Git Introduction
Gitis 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.
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 tagThe 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.gitAfter cloning, a gafish.github.com directory appears. Enter it with:
cd gafish.github.comgit 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.0Create 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.1git checkout
git checkout daily/0.0.1Switch to the newly created branch.
git status
git statusShows modified files that are not yet staged.
git add
git add README.mdStage 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.1Push the branch to the remote repository. Successful push is confirmed by the command output.
git pull
git pull origin daily/0.0.1Pull the latest changes from the remote branch. If conflicts arise, resolve them manually, then stage and commit the resolution.
git log
git logView the commit history, including author, date, and message.
git tag
git tag publish/0.0.1</code>
<code>git push origin publish/0.0.1Tag a milestone (e.g., publish/0.0.1) and push the tag to the remote.
.gitignore
touch .gitignoreAdd 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.
Local Repository
The hidden .git folder inside the working directory stores the repository data.
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.
Relationship Diagram
Branch
Branches isolate development work, allowing multiple parallel changes.
Master Branch
The automatically created master (or main) branch serves as the primary line of development; other branches are merged back into it.
Tag
Tags mark specific points in history, often used for releases (e.g., publish/0.0.1).
HEAD
HEADpoints to the latest commit of the current branch.
Now that you understand these concepts, you can explore advanced Git usage.
File Operations
git add
git add -iInteractive 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 -fRename a file and stage the change; -f forces the rename.
git rm
git rm b.mdRemove a file from both the working directory and the index. git rm src/ -r Remove a directory recursively.
git status
git status -sShow a short summary of file states. git status --ignored Include ignored files in the status output.
Branch Operations
git branch
git branch -aList 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.
git merge
git merge --squashCombine 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.1Create 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 stashSave 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 -pShow 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 170a305Apply 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_nameReapply commits onto another base branch. git rebase -i HEAD~2 Interactive rebase to edit, squash, or reorder commits.
git revert
git revert HEADCreate 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 --statShow 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 -vList existing remotes with URLs.
git remote add origin https://github.com/gafish/gafish.github.com.gitAdd a remote named origin.
git fetch
git fetch origin daily/0.0.1Retrieve updates for a specific remote branch.
Problem Diagnosis
git blame
git blame -L 1,10 demo.htmlShow line‑by‑line attribution for the first ten lines of demo.html.
git bisect
git bisect startStart 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 demoAdd 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.zipCreate a zip archive of the repository at tag v0.1.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
