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.
GIT.LINUXUSERS.COM · PRACTICAL GUIDE
Start with a folder on your computer, connect it to first_commit, and learn the commands by seeing what actually happens.
$ 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-dateREAD THE OUTPUT
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.
Create a local repository and connect it to first_commit.
Work with staging, branches, fetch, pull, and everyday history.
Build confidence →Recover carefully, skim merge conflicts, and understand rebase.
Go further →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
A repository is a project folder plus the history Git keeps in its hidden .git directory.
Git records the name and email attached to your commits. This is local configuration on your computer.
$ 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.comMake a directory, enter it, create a file, and initialize Git. The first cat makes the file contents explicit before Git tracks anything.
$ 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/The remote repository is named first_commit. origin is the short local name for that remote.
$ 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)Staging selects the snapshot that becomes a commit. A commit is local history; pushing sends it to the remote.
$ 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
WATCH THE FLOW
Press play to watch the important objects move. Pause, replay, or select a step to read the command behind it.
origin/first_commitYou 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.fetch versus pullThe 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
Inspection commands are safe: they explain the repository without changing files.
git statusShows 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 cleangit diffShows 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 logShows the commits that form your local history.
$ pwd
/home/vojin/projects/first_commit
$ git log --oneline --decorate -3
4c1a7b2 (HEAD -> main) Initial commit03 · STAGE AND COMMIT
The staging area is a review boundary. It lets you prepare a focused commit instead of committing every file automatically.
Use git add path for a precise commit. Use git restore --staged if you staged something by mistake.
$ 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 project04 · BRANCHES · PARALLEL WORK
A branch is a movable label pointing at a line of commits. It lets you work without disturbing main.
git switch is modern syntax. Older tutorials use git checkout for the same job.
$ 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
Both download remote information. The difference is whether your current branch is changed automatically.
git fetchDownloads 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.git pullFetches 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 +06 · ADVANCED · UNDO AND RECOVER
Undoing local work and undoing shared history are different jobs. Check status and history before changing either.
git revertCreates 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"git reset --hardMoves 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 Commitgit 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
Conflicts happen when two lines of history edit the same area. Resolve the file, stage it, then complete the merge.
Git pauses the merge and writes both versions into the file. Edit away the markers, keep the final text, then stage and commit.
$ 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 notesMerge 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.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
Use the command by its nature. When in doubt, start with pwd, cat, and git status.