Fundamentals 20 min read

Comprehensive Git Tutorial: Installation, Basic Operations, Branch Management, and Team Collaboration

This tutorial provides a step‑by‑step guide to Git, covering its core concepts, Windows installation, repository creation, common commands, branch creation and merging, conflict resolution, stash usage, remote repository setup, and best practices for multi‑person collaboration.

Java Captain
Java Captain
Java Captain
Comprehensive Git Tutorial: Installation, Basic Operations, Branch Management, and Team Collaboration

What is Git? Git is a modern distributed version control system that tracks changes in files, allowing work offline and synchronizing later via pushes and pulls. It contrasts with SVN, which is centralized and requires constant network access.

Installing Git on Windows Install the msysgit package, then configure your identity with git config --global user.name "Your Name" and git config --global user.email "[email protected]" . After installation, open "Git Bash" to verify the setup.

Creating a local repository Navigate to a project folder and run git init to create a hidden .git directory. The workspace holds your files, the index (staging area) holds files added with git add , and the repository stores committed snapshots.

Basic workflow Add files with git add <file> , commit them using git commit -m "message" , and view status with git status . Use git diff <file> to see unstaged changes and git log (or git log --pretty=oneline ) to inspect commit history. To revert, git reset --hard HEAD^ moves the branch pointer back one commit; specify HEAD~N for older commits.

Undoing changes Discard workspace modifications with git checkout -- <file> . If a file is already staged, the same command restores it to the staged state. To delete a tracked file, use git rm <file> followed by a commit.

Remote repositories After creating a repository on GitHub, add it as a remote with git remote add origin https://github.com/youruser/testgit.git . Push the initial commit using git push -u origin master . Subsequent changes are sent with git push origin master , and you can clone a remote repo via git clone https://github.com/youruser/testgit2.git .

Branching Create a new branch with git checkout -b dev (or git branch dev then git checkout dev ). Merge a branch back into master using git merge dev . To keep a merge record, use git merge --no-ff -m "msg" dev . Delete a finished branch with git branch -d dev .

Conflict resolution When merging, Git marks conflicts with <<<<<, =======, and >>>>>. Edit the conflicted file to resolve differences, then commit the merge.

Stashing Save unfinished work with git stash , list saved states via git stash list , restore with git stash apply (leaving the stash entry) or git stash pop (which also drops it).

Team collaboration Team members push to remote branches with git push origin <branch> . If a push is rejected, pull the latest changes using git pull , resolve any conflicts, commit, and push again. Set up tracking relationships with git branch --set-upstream-to=origin/dev dev to simplify pulls and pushes.

Best practices Keep the master branch stable for releases, perform feature work on dev or dedicated branches, merge back to master when ready, and use tags or releases to mark production versions.

gitcommand-lineversion controlCollaborationbranchingRemote Repository
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.