Fundamentals 16 min read

Master Git Basics: From Repository Trees to Branch Merges and Rollbacks

This comprehensive guide walks you through Git fundamentals, explaining its distributed nature, repository tree structure, common commands for adding files, branching, merging, resolving conflicts, navigating commits, resetting, reverting, and synchronizing with remote repositories, all illustrated with clear examples and diagrams.

21CTO
21CTO
21CTO
Master Git Basics: From Repository Trees to Branch Merges and Rollbacks

Before we begin, let’s review VCS (Version Control Systems). Popular open‑source tools include CVS, SVN, TFS, Git, and Mercurial.

Git was created by Linus Torvalds, the author of the Linux operating system.

The key difference between Git and other VCSs is that Git is distributed. Each developer works with a full local repository in a .git folder and optionally a remote central repository, and every collaborator has an exact clone of the repository.

Git can be imagined as a layer above the file system that operates on files, or as a tree where each commit creates a new node. Most Git commands navigate and manipulate this tree.

In this article we explore Git repositories from the tree perspective, covering common use cases such as adding/modifying files, creating and merging branches (with and without conflicts), viewing history, rolling back to a commit, and syncing code to a remote repository.

Common Git Terminology

master – the main branch of the repository. clone – copy an existing repository to a local environment. commit – record changes to the repository (similar to “check‑in” in other VCSs). fetch / pull – retrieve the latest changes from a remote repository; pull also merges them. push – upload local commits to a remote repository. remote – the remote location of a repository, usually on a server. SHA – unique identifier for each commit/node in the Git tree. HEAD – reference to the current commit/working directory. branch – a series of snapshots representing a line of development.

Setting Up the Workstation

Install Git from https://git-scm.com/downloads and configure your name and email:

$ git config --global user.name "Your Name"
$ git config --global user.email "[email protected]"

Creating a New Repository

$ mkdir mygitrepo
$ cd mygitrepo
$ git init
Initialized empty Git repository in .../.git/

Check the repository status:

$ git status
# On branch master
# Initial commit
nothing to commit (create/copy files and use "git add" to track)

Adding and Committing a New File

$ touch hello.txt
$ echo Hello, world! > hello.txt
$ git add hello.txt
$ git commit -m "Add my first file"
 1 file changed, 1 insertion(+)
 create mode 100644 hello.txt

After the first commit the repository tree looks like this:

Initial commit tree
Initial commit tree

Adding Another File

$ echo "Hi, I'm another file" > anotherfile.txt
$ git add .
$ git commit -m "add another file with some other content"
 1 file changed, 1 insertion(+)
 create mode 100644 anotherfile.txt

The tree now shows the latest commit on master:

Tree after second commit
Tree after second commit

Creating a Feature Branch

$ git branch my-feature-branch
$ git checkout my-feature-branch
Switched to branch 'my-feature-branch'

Now modify hello.txt on the feature branch:

$ echo "Hi" >> hello.txt
$ git commit -a -m "modify file adding hi"
2fa266a] modify file adding hi
1 file changed, 1 insertion(+)

Merging and Resolving Conflicts

$ git checkout master
Switched to branch 'master'
$ echo "Hi I was changed in master" >> hello.txt
$ git commit -a -m "add line on hello.txt"
$ git merge my-feature-branch
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt

Resolve the conflict by editing hello.txt to:

Hello, world!
Hi I was changed in master
Hi
$ git commit -a -m "resolve merge conflicts"
[master 6834fb2] resolve merge conflicts

Resulting tree after merge:

Tree after merge
Tree after merge

Checking Out a Specific Commit

$ git log

Copy the short SHA (e.g., c8616db) and checkout:

$ git checkout c8616db
Note: checking out 'c8616db'.
You are in 'detached HEAD' state...

Rolling Back

$ git reset --hard c8616db
HEAD is now at c8616db add line on hello.txt

General syntax: git reset --hard <tag/branch/commit id> When the commit has already been pushed, use git revert to create a new commit that undoes the changes:

$ git revert 41b8684

Sharing / Syncing the Repository

$ git remote add origin [email protected]:username/repo.git
$ git remote -v
$ git push -u origin master

You can add multiple remotes for deployment:

$ git remote add deploy [email protected]:repo.git
$ git push deploy

Cloning an Existing Repository

$ git clone [email protected]:username/repo.git
Cloning into 'repo'...
... (output omitted)

Then enter the directory and verify remotes:

$ cd repo/
$ git remote -v

Further Resources

http://gitready.com/

Book: Pro Git by Scott Chacon

Try Git in 15 minutes

Introduction to Git with Scott Chacon of GitHub

Personal Git Cheat Sheet

Make the Switch to Git

Happy learning!

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

command-lineremotebranchingresetrevertMerge Conflicts
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

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.