Master Git Basics: From Commits to Conflict Resolution
This comprehensive guide demystifies Git by explaining version‑control concepts, commit objects, repository initialization, staging, branching, tagging, merging, conflict handling, and push strategies, empowering developers to use Git confidently and efficiently.
Why Developers Fear Git
Many developers are hesitant to use Git because they have never encountered it before or are unfamiliar with its workflow, often coming from SVN or CVS backgrounds.
Never Be Afraid To Try Something New.
Version Control Basics
Version control records changes to files over time, capturing who made the change, when it was made, and what was changed. It also enables undoing problematic commits.
Who: the author of the change
When: the timestamp of the change
What: the content of the change
Git Overview
Git is a distributed version‑control system (VCS) that stores a complete copy of the repository locally, allowing flexible collaboration without locking files.
Commit Object
Each commit is stored as a git commit object identified by a SHA‑1 hash (e.g., 9986222). The object contains the author, commit message, and a snapshot of the entire project state.
Initializing a Repository
$ git init hackers</code><code>$ cd hackers</code><code>$ git statusThe initial git status output shows:
On branch master</code><code>No commits yet</code><code>nothing to commit (create/copy files and use "git add" to track)Branches, HEAD, and Tags
Branches are references that point to specific commits; master is the default branch. HEAD points to the current branch or tag. Creating a new branch with git branch b creates a new reference, and git checkout b switches to it. Tags are immutable references often used for releases.
Staging Area (Index)
Changes go through three states: working directory, staging (index) area, and repository. git add moves changes to the index, preparing them for the next commit. The index stores a full snapshot of files, not deltas.
$ git add .Common Git Commands
git add– stage changes git add -p – interactively stage hunks git reset – unstage or reset changes git diff --staged – view staged changes git diff – view unstaged changes git commit – create a new commit git commit -a – stage all modified files and commit git commit --amend – modify the most recent commit git rebase -i HEAD~13 – interactively rewrite recent commits
Reference Operations
git branch b– create branch
b git checkout b– switch to branch
b git merge NAME– merge another branch into the current one git pull – fetch and merge remote changes git push – upload local commits to a remote repository
Merge Conflicts
When a conflict occurs, Git pauses the merge and marks conflict regions with <<<<<<< and =======. Resolve conflicts manually or with git mergetool, then stage the resolved files with git add . and commit.
Push Failures
If a push is rejected, it usually means the remote has new commits. Resolve by pulling and merging ( git pull), or force‑push with --force-with-lease when you are sure no other work will be lost.
Conclusion
Understanding Git’s objects, staging area, branching model, and conflict‑resolution workflow turns Git from a perceived “black magic” tool into a powerful ally for efficient development.
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.
Alibaba Cloud Developer
Alibaba's official tech channel, featuring all of its technology innovations.
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.
