Git Basics: Core Concepts, Branches, and Command Reference
This article provides a comprehensive overview of Git, covering its distributed architecture, core concepts such as commits, HEAD, and remote repositories, detailed explanations of branches, and a thorough command reference for committing, branching, merging, rebasing, cherry-picking, and remote operations.
Git is a distributed version‑control system that lets developers commit locally without network dependency and later synchronize changes with remote repositories.
1. Basic Concepts
Advantages of Git : Unlike centralized systems (e.g., SVN) which require network access for every commit, Git enables offline commits, stores the full history locally, and allows each developer to clone the entire repository.
File status : Files can be in three states – modified (changed in the working directory), staged (added to the index with git add), and committed (recorded permanently in the repository).
Commit nodes : Each git commit creates a node identified by a SHA‑1 hash; successive commits form a linear chain (ignoring merges).
HEAD : A reference that points to the current commit (or to a branch, which in turn points to a commit). The working directory reflects the commit that HEAD points to.
Remote repository : The central location where developers push and pull changes. git clone copies the repository and its refs; git fetch updates remote refs without merging.
2. Branches
A branch is a lightweight movable pointer to a commit. Multiple branches can coexist, enabling parallel development such as feature work, bug‑fixes, or release preparation.
Typical workflow: create a branch for a new feature, develop, then merge back into master (or main) once the feature is complete.
3. Command Details
3.1 Commit‑related commands
Add a specific file to the index: git add <file‑path> Add all changes: git add . Undo changes in the working tree: git checkout -- <file‑name> Clear the index (unstage files): git reset HEAD <file‑name> Commit staged changes:
git commit -m "commit description"3.2 Branch‑related commands
Create a new branch (starts at the current HEAD): git branch <branch‑name> Switch to an existing branch: git checkout <branch‑name> Create and switch in one step: git checkout -b <branch‑name> Delete a branch that is no longer needed:
git branch -d <branch‑name>3.3 Merge‑related commands
Merge another branch or commit into the current branch: git merge <branch‑name|commit‑hash> Fast‑forward merges occur when the current branch is behind the target; otherwise a new merge commit is created.
3.4 Rebase‑related commands
Reapply commits on top of another base, producing a linear history:
git rebase <branch‑name|commit‑hash>3.5 Cherry‑pick
Apply selected commits onto the current branch:
git cherry-pick <commit‑hash>3.6 Reverting (detached HEAD) and reset
Detach HEAD to point directly at a commit:
git checkout <commit‑hash>
# or
git checkout --detachMove HEAD back N commits (relative reference): git checkout <branch>~N Reset the current branch and HEAD to an earlier commit (undo commits): git reset HEAD~N Amend the most recent commit after fixing issues:
git commit --amend3.7 Remote‑related commands
Clone a repository: git clone <repository‑url> Fetch updates from the remote without merging: git fetch <remote> [<branch>] Pull (fetch + merge) from the remote: git pull <remote> <branch> Pull with rebase instead of merge: git pull --rebase <remote> <branch> Push local commits to the remote:
git push <remote> <branch>Conclusion
HEAD and branches are merely lightweight references that, together with commit nodes, form Git’s distributed model.
Merge preserves chronological history, while rebase creates a cleaner linear history; choose based on the workflow.
Moving HEAD lets you inspect any past commit.
Cloning or fetching stores the full set of commits and refs locally.
Pull is essentially fetch followed by merge, optionally using rebase.
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.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
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.
