Master Git: From Installation to Advanced Branch Management on Windows
This comprehensive guide explains what Git is, compares it with SVN, shows how to install Git on Windows, and walks through creating repositories, adding and committing files, checking status, viewing diffs and logs, resetting versions, managing branches, handling remote repositories, using stash, and collaborating with teammates using push and pull commands.
What is Git?
Git is the most advanced distributed version control system in the world today.
SVN vs. Git
SVN is a centralized version control system that requires a connection to a central server for most operations, while Git is distributed, allowing each developer to work offline with a full copy of the repository.
Installing Git on Windows
Download the Windows version (msysgit) and run the default installer. After installation, you can find "Git → Git Bash" in the Start menu.
Complete the setup by configuring your username and email:
git config --global user.name "Your Name" git config --global user.email "[email protected]"Basic Operations
Creating a repository : Navigate to a folder and run git init. This creates a hidden .git directory that stores all version information.
Adding files : git add <file> moves files to the staging area.
Committing : git commit -m "message" records the staged changes in the repository.
Checking status : git status shows modified, staged, and untracked files.
Viewing differences : git diff <file> displays line‑by‑line changes.
Viewing history : git log lists commits; git log --pretty=oneline shows a concise one‑line format.
Version Revert
To revert to a previous commit, use git reset --hard HEAD^ (one step back) or git reset --hard HEAD~100 (100 steps back). You can also reset to a specific commit hash with git reset --hard <hash>.
Working Directory vs. Staging Area
The working directory contains the files you edit. git add moves changes to the staging area, and git commit records the staged snapshot into the repository.
Undo Changes and Delete Files
To discard uncommitted changes in a file: git checkout -- <file>. To delete a file from the repository, use git rm <file> followed by a commit.
Remote Repository
Create an SSH key, add it to GitHub, then create a remote repository. Link the local repo with
git remote add origin https://github.com/youruser/yourrepo.gitand push the initial commit with git push -u origin master. Subsequent pushes use git push origin master.
Branch Creation and Merging
Create and switch to a new branch: git checkout -b dev. List branches with git branch. Merge a branch into the current one with git merge dev. Delete a branch after merging with git branch -d dev. Use git merge --no-ff -m "message" dev to force a merge commit.
Bug Branches
For urgent bug fixes, create a temporary branch (e.g., git checkout -b bug-404), fix the issue, merge back to master, and delete the bug branch.
Stash
When you need to switch context without committing, run git stash to hide current changes. List stashes with git stash list. Restore with git stash apply (keeps stash) or git stash pop (removes stash after applying).
Collaboration
Push your branch with git push origin <branch>. If the push is rejected, pull the latest changes with git pull, resolve any conflicts, commit, and push again. Set upstream tracking with
git branch --set-upstream-to=origin/<branch> <branch>if needed.
Common Commands Summary
mkdir <dir>– create a directory pwd – show current path git init – initialize a repository git add <file> – stage a file git commit -m "msg" – commit staged changes git status – view repository status git diff <file> – view changes git log – view commit history git reset --hard HEAD^ – revert to previous commit cat <file> – display file content git reflog – view reference log with commit hashes git checkout -- <file> – discard changes git rm <file> – delete a file git remote add origin <url> – add remote repository git push -u origin master – push initial master branch git clone <url> – clone a remote repository git checkout -b dev – create and switch to a new branch git branch – list branches git checkout master – switch to master git merge dev – merge dev into current branch git branch -d dev – delete a branch git stash – hide current work git stash list – list stashes git stash apply – apply stash without dropping git stash pop – apply and drop stash git remote – show remote info git remote -v – detailed remote info git push origin master – push master to remote
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.
