GIT.LINUXUSERS.COM · PRACTICAL GUIDE

Git without
the mystery.

Start with a folder on your computer, connect it to first_commit, and learn the commands by seeing what actually happens.

first_commit
$ pwd
/home/vojin/projects/first_commit

$ git status
On branch main
nothing to commit, working tree clean

$ git push -u origin main
Everything up-to-date

READ THE OUTPUT

Every example is a visible experiment.

Commands are grouped by what they do. Each walkthrough starts by showing your current directory and the file you are working with, then shows the command and its result.

01

Beginner

Create a local repository and connect it to first_commit.

Begin here →
03

Advanced

Recover carefully, skim merge conflicts, and understand rebase.

Go further →
⌁
Important: read the terminal, not only the command.

Every example deliberately includes pwd, cat, and visible command output so you can see where you are, what changed, and what Git knows.

01 · BEGINNER · CREATE AND CONNECT

Set up your first repository

A repository is a project folder plus the history Git keeps in its hidden .git directory.

01

Check Git and configure your identity

Git records the name and email attached to your commits. This is local configuration on your computer.

SETUP · READ-ONLY FIRST
$ pwd
/home/vojin

$ git --version
git version 2.43.0

$ git config --global user.name "Vojin Urosevic"
$ git config --global user.email "you@example.com"

$ git config --global --get-regexp 'user\.(name|email)'
user.name Vojin Urosevic
user.email you@example.com
02

Create the local project

Make a directory, enter it, create a file, and initialize Git. The first cat makes the file contents explicit before Git tracks anything.

LOCAL FILES · CREATE
$ mkdir -p ~/projects/first_commit
$ cd ~/projects/first_commit

$ pwd
/home/vojin/projects/first_commit

$ printf '# First Commit\n\nA small Git practice repository.\n' > README.md
$ cat README.md
# First Commit

A small Git practice repository.

$ git init
Initialized empty Git repository in /home/vojin/projects/first_commit/.git/
03

Connect to the remote repository

The remote repository is named first_commit. origin is the short local name for that remote.

REMOTE · CONNECT
$ pwd
/home/vojin/projects/first_commit

$ cat README.md
# First Commit

A small Git practice repository.

$ git remote add origin git@git.linuxusers.com:username/first_commit.git
$ git remote -v
origin  git@git.linuxusers.com:username/first_commit.git (fetch)
origin  git@git.linuxusers.com:username/first_commit.git (push)
04

Stage, commit, and push

Staging selects the snapshot that becomes a commit. A commit is local history; pushing sends it to the remote.

FIRST COMMIT · PUBLISH
$ pwd
/home/vojin/projects/first_commit

$ cat README.md
# First Commit

A small Git practice repository.

$ git add README.md
$ git commit -m "Initial commit"
[main (root-commit) 4c1a7b2] Initial commit
 1 file changed, 3 insertions(+)
 create mode 100644 README.md

$ git branch -M main
$ git push -u origin main
branch 'main' set up to track 'origin/main'.

THE MENTAL MODEL

Where your changes live

Working fileswhat you edit
→
Staging areawhat the next commit includes
→
Local commitsyour project history
→
Remoteshared copy on git.linuxusers.com

WATCH THE FLOW

See Git move the work

Press play to watch the important objects move. Pause, replay, or select a step to read the command behind it.

LOCAL → REMOTE

From working directory to origin/first_commit

Working directory

You edit a file. Git sees the change, but it is not selected for a commit yet.

$ pwd
/home/vojin/projects/first_commit
$ cat README.md
# First Commit
A small Git practice repository.
REMOTE → LOCAL

fetch versus pull

Your local mainABworking directory
●remote information↓
origin/mainABCfirst_commit on server
Fetch: look without changing your files

The new remote commit is downloaded and your origin/main pointer moves. Your current files stay where they are.

$ git fetch origin
origin/main updated: B → C
Your current main branch is unchanged.

02 · INSPECT · UNDERSTAND STATE

See what Git sees

Inspection commands are safe: they explain the repository without changing files.

git status

Shows the current branch, changed files, and staging state.

$ pwd
/home/vojin/projects/first_commit
$ cat README.md
# First Commit
$ git status
On branch main
nothing to commit, working tree clean

git diff

Shows unstaged edits before you commit them.

$ pwd
/home/vojin/projects/first_commit
$ cat README.md
# First Commit

A small Git practice repository.
$ git diff
+ A second line added locally.

git log

Shows the commits that form your local history.

$ pwd
/home/vojin/projects/first_commit
$ git log --oneline --decorate -3
4c1a7b2 (HEAD -> main) Initial commit

03 · STAGE AND COMMIT

Choose what becomes history

The staging area is a review boundary. It lets you prepare a focused commit instead of committing every file automatically.

05

Stage one file, inspect, then commit

Use git add path for a precise commit. Use git restore --staged if you staged something by mistake.

STAGING · REVIEW
$ pwd
/home/vojin/projects/first_commit

$ cat README.md
# First Commit

A small Git practice repository.
A second line added locally.

$ git status
modified: README.md

$ git add README.md
$ git diff --staged
+ A second line added locally.

$ git commit -m "Document the project"
[main 8d2f101] Document the project

04 · BRANCHES · PARALLEL WORK

Make a branch for a change

A branch is a movable label pointing at a line of commits. It lets you work without disturbing main.

06

Create, switch, publish

git switch is modern syntax. Older tutorials use git checkout for the same job.

BRANCH · FEATURE WORK
$ pwd
/home/vojin/projects/first_commit

$ cat README.md
# First Commit

A small Git practice repository.
A second line added locally.

$ git switch -c feature/notes
Switched to a new branch 'feature/notes'
$ git branch --show-current
feature/notes
$ git checkout main
Switched to branch 'main'
$ git push -u origin feature/notes
branch 'feature/notes' set up to track 'origin/feature/notes'.

05 · SYNC · FETCH AND PULL

Fetch is not pull

Both download remote information. The difference is whether your current branch is changed automatically.

LOOK FIRST

git fetch

Downloads remote commits and updates names such as origin/main. It does not merge them into current files.

$ pwd
/home/vojin/projects/first_commit
$ cat README.md
# First Commit
$ git fetch origin
From git.linuxusers.com:username/first_commit
   4c1a7b2..9d8e011  main -> origin/main
$ git status
Your branch is behind 'origin/main' by 1 commit.
FETCH + INTEGRATE

git pull

Fetches remote changes and integrates them into the current branch. It may merge or rebase, depending on configuration.

$ pwd
/home/vojin/projects/first_commit
$ cat README.md
# First Commit
$ git pull --ff-only origin main
Updating 4c1a7b2..9d8e011
Fast-forward
 README.md | 2 +
Practical habit: fetch when you want to inspect remote work first. Pull when you are ready to update your current branch.

06 · ADVANCED · UNDO AND RECOVER

Undo with the right tool

Undoing local work and undoing shared history are different jobs. Check status and history before changing either.

SAFE FOR SHARED HISTORY

git revert

Creates a new commit that reverses an earlier commit. Prefer this after a commit has been pushed.

$ pwd
/home/vojin/projects/first_commit
$ cat README.md
# First Commit
A line to remove.
$ git log --oneline -2
9d8e011 Add temporary line
4c1a7b2 Initial commit
$ git revert 9d8e011
[main 3b71c55] Revert "Add temporary line"
DESTRUCTIVE · CHECK FIRST

git reset --hard

Moves the branch and discards tracked working-tree changes. Do not use it casually on shared work.

$ pwd
/home/vojin/projects/first_commit
$ cat README.md
Temporary local text
$ git status
modified: README.md
$ git reset --hard HEAD
HEAD is now at 4c1a7b2 Initial commit
$ cat README.md
# First Commit

Other recovery commands

git restore file discards unstaged changes · git reset --soft HEAD~1 undoes a commit but keeps changes staged · git reflog helps locate previous positions of HEAD.

07 · ADVANCED · MERGES AND CONFLICTS

Skim the hard parts safely

Conflicts happen when two lines of history edit the same area. Resolve the file, stage it, then complete the merge.

07

See the conflict markers

Git pauses the merge and writes both versions into the file. Edit away the markers, keep the final text, then stage and commit.

MERGE · RESOLVE
$ pwd
/home/vojin/projects/first_commit

$ cat README.md
# First Commit
<<<<<<<< HEAD
Text from main.
=======
Text from feature/notes.
>>>>>>> feature/notes

$ git status
both modified: README.md

$ cat README.md
# First Commit

Text from the final version.
$ git add README.md
$ git commit -m "Merge feature notes"
[main 7f221aa] Merge feature notes

Merge or rebase?

Merge preserves the branching story. Rebase rewrites local commits so they appear on top of the latest base. Follow team conventions and avoid rebasing commits other people already use.

$ pwd
/home/vojin/projects/first_commit
$ cat README.md
# First Commit
Text from main.
$ git fetch origin
From git.linuxusers.com:username/first_commit
$ git rebase origin/main
Successfully rebased and updated refs/heads/feature/notes.

Unrelated repositories

If two repositories have separate histories, Git may refuse to combine them. This is exceptional—inspect both projects and make a backup first.

$ pwd
/home/vojin/projects/first_commit
$ cat README.md
# First Commit
$ git merge other-branch --allow-unrelated-histories
Merge made by the 'ort' strategy.

KEEP THIS CLOSE

Git command reference

Use the command by its nature. When in doubt, start with pwd, cat, and git status.