Kahibaro
Discord Login Register

Git fundamentals

Why Git Matters for Linux Developers

Git is the de facto version control system in modern software development and is tightly integrated with Linux workflows:

Here you’ll learn practical Git usage from the command line, focusing on how a Linux developer actually works day to day.


Basic Git Concepts

You don’t need to memorize internal data structures, but you do need to understand the core ideas:

Installing and Configuring Git

Most distributions provide Git via their package manager:

Minimal global configuration (run once per machine):

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global core.editor "nano"
git config --global init.defaultBranch main

View config values:

git config --global --list
git config --system --list    # system-wide
git config --local --list     # in current repo

Creating and Cloning Repositories

Initializing a new repository

To start version-controlling an existing directory:

cd my-project
git init            # creates .git directory
git status

Add an initial file and commit:

echo "# My Project" > README.md
git add README.md
git commit -m "Initial commit"

Cloning an existing repository

To obtain a copy of a remote project:

git clone https://github.com/user/project.git
cd project

Common variants:

  git clone git@github.com:user/project.git
  git clone https://github.com/user/project.git mydir

The Three States: Working Tree, Staging Area, Commits

Git tracks files in three main places:

Use git status frequently to see where changes are.


Everyday Commands: Status, Add, Commit

Checking status

git status

Shows:

Short format:

git status -sb   # concise status with branch

Adding changes

To stage a specific file:

git add file.c

To add everything (tracked and untracked) in the current directory:

git add .

To stage only some hunks from a file (useful for splitting commits):

git add -p file.c

You’ll be shown each hunk and can choose y (stage) or n (skip), etc.

Making a commit

Once changes are staged:

git commit -m "Implement feature X"

If you omit -m, your editor opens so you can write a multi-line message.

Good commit messages:

Example:

git commit
# In editor:
# Add config loader for JSON files
#
# This introduces a small helper library to read JSON config
# files. It is used by the main server binary to...

Inspecting History: log, show, diff

Viewing commit history

Basic history:

git log

A more compact, on-one-line view:

git log --oneline
git log --oneline --graph --decorate --all

Filter by file:

git log -- path/to/file.c

View a specific commit (replace with real hash):

git show 3f7a9ac

Shows the commit message and the patch.

Viewing differences

Compare working tree vs last commit:

git diff

Compare staged vs last commit:

git diff --cached

Compare two commits:

git diff old_commit new_commit
git diff 3f7a9ac HEAD

Diff for a specific file:

git diff HEAD~1 HEAD -- path/to/file.c

Working with Branches

Listing and creating branches

List branches:

git branch        # local branches
git branch -a     # local + remote-tracking

Create a new branch from current HEAD:

git branch feature-x

Create and switch immediately:

git switch -c feature-x
# or older style:
git checkout -b feature-x

Switching branches

git switch main
# or:
git checkout main

Deleting branches

After merging a feature branch:

git branch -d feature-x        # safe; refuses if unmerged
git branch -D feature-x        # force delete

Merging and Resolving Conflicts

Fast-forward vs merge commit

If your branch is strictly ahead of main, merging can be a “fast-forward”:

git switch main
git merge feature-x   # may just move main pointer forward

If both branches have diverged, Git creates a merge commit:

git merge feature-x   # opens editor for merge commit message

Handling merge conflicts

When Git cannot automatically merge:

Conflicted sections look like:

<<<<<<< HEAD
current branch version
=======
feature-x branch version
>>>>>>> feature-x

Resolve by editing files to the correct content, then:

git add path/to/conflicted_file
git commit          # completes the merge

You can also abort the merge:

git merge --abort

Working with Remotes: fetch, pull, push

Most workflows use a remote named origin:

Check remotes:

git remote -v

Add a remote:

git remote add origin git@github.com:user/project.git

Fetching vs pulling

Fetch only:

git fetch origin
git log --oneline --graph --all

Update your current branch with remote changes:

git pull origin main

You can configure a default upstream so you can just run git pull:

git branch --set-upstream-to=origin/main main

Pushing changes

First push of a new local branch:

git push -u origin feature-x

Subsequent pushes:

git push

Push a specific branch:

git push origin main

Delete a remote branch:

git push origin --delete feature-x

Stashing Work-In-Progress (WIP)

Sometimes you need to switch branches but have uncommitted changes:

Save them temporarily:

git stash
# or include untracked files:
git stash -u

List stashes:

git stash list

Restore latest stash and drop it:

git stash pop

Restore without dropping:

git stash apply stash@{1}

Undoing and Fixing Mistakes

Git provides several ways to adjust recent work. Use them carefully, especially on shared branches.

Amending the last commit

To fix the message or add omitted files:

git add missed_file.c
git commit --amend

This rewrites the last commit; avoid doing this after you’ve pushed that commit to a shared branch.

Resetting

Common forms:

  git reset --soft HEAD~1
  # now previous commit’s changes are staged again
  git reset HEAD~1
  # previous commit’s changes become unstaged
  git reset --hard HEAD~1
  # WARNING: lose work; use with caution

To discard all local changes and return to last commit:

git reset --hard HEAD

Restoring files

When you only want to revert a file, not change history:

git restore path/to/file.c            # reset working copy to HEAD
git restore --staged path/to/file.c   # unstage without touching file

(On older Git versions, similar operations used git checkout.)


Git Ignore Rules

Many generated files (build artifacts, logs) should not be tracked.

Create a .gitignore file:

# ignore object files
*.o
# ignore build directory
build/
# ignore editor backups
*~

Apply globally (for your user only):

echo "*.swp" >> ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global

Ignored files won’t show as untracked in git status.


Collaboration Patterns: Forks, Feature Branches, Pull Requests

While specific hosting platforms have their own UI, the Git side is similar:

  git switch -c fix-issue-123 main
  # work, commit
  git push -u origin fix-issue-123
    git remote add upstream https://github.com/original/project.git
    git fetch upstream
    git switch main
    git merge upstream/main
    # or: git rebase upstream/main

These patterns are central to open source contribution and CI/CD workflows.


Useful Git Tips for Linux Developers

  git config --global alias.co "checkout"
  git config --global alias.br "branch"
  git config --global alias.st "status -sb"
  git config --global alias.lg "log --oneline --graph --decorate --all"

With these fundamentals, you can comfortably manage source code on Linux, collaborate with others, and integrate Git into build and CI/CD workflows.

Views: 18

Comments

Please login to add a comment.

Don't have an account? Register now!