Master Git: From Basics to Advanced Branching, Remote Collaboration, and Conflict Resolution
This comprehensive guide walks you through Git fundamentals, installation on Windows, core concepts like workspace, index, repository and remote, essential commands for creating repositories, committing, branching, merging, stashing, handling conflicts, and collaborating with multiple developers using GitHub.
1. What is Git?
Git is the most advanced distributed version control system in the world.
2. How Git Works
Git operates with four main areas:
Workspace – the working directory you see on your computer.
Index/Stage – the staging area.
Repository – the local .git directory that stores all history.
Remote – a repository hosted on another server.
3. SVN vs. Git
SVN is a centralized system that requires a network connection to the central server for every operation, while Git is distributed; each developer has a full copy of the repository and can work offline, pushing changes to others when needed.
4. Installing Git on Windows
Download and install msysgit (the Windows version of Git). After installation, run "Git Bash" from the Start menu. Configure your identity with:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"5. Basic Operations
Creating a Repository
Navigate to your project folder and run: git init This creates a hidden .git directory that tracks all changes.
Adding and Committing Files
Example with a readme.txt file:
git add readme.txt
git commit -m "Add initial readme"Use git status to see untracked or modified files.
Viewing Changes
Show differences with:
git diff readme.txtVersion History
List commits with: git log For a concise one‑line view:
git log --pretty=onelineReverting Versions
Move the HEAD pointer back:
git reset --hard HEAD^ # one commit back
git reset --hard HEAD~100 # back 100 commitsFind a specific commit hash with git reflog and reset to it:
git reset --hard 6fcfc89Understanding Workspace vs. Staging Area
Changes are first added to the staging area with git add, then committed to the repository with git commit. You can view the status at any time with git status.
Undoing Changes
Discard uncommitted modifications: git checkout -- readme.txt Use the same command for files you have already staged but then edited again.
Deleting Files
Remove a file from the working tree and stage the deletion:
git rm b.txt
git commit -m "Delete b.txt"If you haven't committed yet, you can restore it with:
git checkout -- b.txt6. Remote Repositories
After creating a GitHub account, generate an SSH key ( ssh-keygen -t rsa -C "[email protected]") and add the public key to GitHub.
Create a new repository on GitHub (e.g., testgit) and link it to your local repo:
git remote add origin https://github.com/youruser/testgit.gitPush the initial commit: git push -u origin master Future pushes are simply:
git push origin masterCloning a Repository
To clone an existing remote repo:
git clone https://github.com/youruser/testgit2.git7. Branch Management
Create and switch to a new branch: git checkout -b dev List branches with git branch. Merge a branch back into master:
git checkout master
git merge devIf the merge is fast‑forward, Git simply moves the master pointer.
Delete a branch after merging:
git branch -d devMerge Strategies
Use --no-ff to force a merge commit and preserve branch history:
git merge --no-ff -m "Merge dev" dev8. Handling Conflicts
When merging conflicting changes, Git marks the conflict with <<<<<<<, =======, and >>>>>>>. Edit the file to resolve, then commit.
9. Bug‑Fix Branches
Create a short‑lived branch for a bug (e.g., issue-404), fix the problem, merge back, and delete the branch.
10. Stashing Work in Progress
If you need to switch context without committing, stash your changes:
git stash
# later
git stash pop # restores and removes the stash
# or
git stash apply # restores without removing11. Multi‑Developer Collaboration
Push your branch to the remote: git push origin dev Pull updates from others: git pull origin dev If the pull fails because the local branch isn’t tracking the remote, set the upstream: git branch --set-upstream-to=origin/dev dev Resolve any conflicts, commit, and push again.
12. Summary of Essential Commands
View branches: git branch Create branch: git branch name Switch branch: git checkout name Create & switch: git checkout -b name Merge: git merge name Delete branch: git branch -d name These commands form the core workflow for using Git effectively in software 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.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
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.
