Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

git

Track every change of code and config, work on branches, and share work through remotes like GitHub — the base of CI/CD and GitOps.

Level
DevSecOps service
Group
Git and CI/CD
Needs root
No
Package
git

What it does

git keeps the full history of a project in a local repository (.git/). You change files, stage them (git add), and save a commit. Branches let you work on a feature without touching main; a pull request merges it after review and CI. A remote (origin) is the shared copy on GitHub, GitLab or Azure Repos.

In DevSecOps, Git is the source of truth: app code, Dockerfiles, Terraform, Kubernetes YAML and pipelines all live in Git, and every change goes through review. Signed commits and secret scanning protect it.

How to write it

git COMMAND [OPTIONS] [ARGS]

Main parts

Daily commands

clone URL
Copy a remote repository.
status
What changed, what is staged.
add FILE / add -p
Stage changes (by parts with -p).
commit -m MSG
Save a commit.
log --oneline --graph
Show history.
diff [--staged]
Show changes.
switch -c BRANCH / switch BRANCH
Create / change branch.
pull / fetch / push
Get / send changes.
merge BRANCH
Join a branch into the current one.
rebase BRANCH
Replay your commits on top of another branch.
restore FILE / restore --staged FILE
Undo changes / unstage.
revert COMMIT
New commit that undoes an old one (safe on shared branches).
reset --soft|--hard COMMIT
Move the branch (hard deletes work!).
stash / stash pop
Put work aside and bring it back.
tag -s v1.4.0
Make a (signed) tag.
blame FILE / bisect
Who changed a line / find the bad commit.
remote -v / remote add NAME URL
Manage remotes.
cherry-pick COMMIT
Copy one commit to this branch.

Options 9

Most used

Useful settings and flags

config --global user.name/user.email
Your identity.
config --global init.defaultBranch main
Default branch name.
config --global pull.rebase true
Rebase on pull.
config --global commit.gpgsign true
Sign every commit.
config --global gpg.format ssh
Sign with an SSH key.
-c KEY=VAL
Set config for one command.
--amend
Change the last commit (only before push).
--force-with-lease
Safer force push.
--depth 1
Shallow clone (CI).

Examples 10

$ run as a normal user# run as root (or with sudo)

  1. 1Set up your identity and signing

    git config --global user.name "s0sta"
    git config --global user.email "76218642+s0sta@users.noreply.github.com"
    git config --global gpg.format ssh
    git config --global user.signingkey ~/.ssh/id_ed25519.pub
    git config --global commit.gpgsign true
  2. 2Start work on a feature branch

    git clone git@github.com:s0sta/platform.git && cd platform
    git switch -c feat/add-health-check
    vim app/health.py
    git add -p
    git commit -m "Add /health endpoint"
    git push -u origin feat/add-health-check
  3. 3See what happened

    git status
    git log --oneline --graph --decorate -15
    git diff main...feat/add-health-check --stat
  4. 4Update your branch with main

    git fetch origin
    git rebase origin/main
    git push --force-with-lease
  5. 5Undo safely

    git restore app/config.py
    git restore --staged app/config.py
    git revert 3f2a91c
    git commit --amend -m "Better message"
  6. 6Put work aside

    git stash push -m "half done"
    git switch main && git pull
    git switch - && git stash pop
  7. 7Tag a release

    git tag -s v1.4.0 -m "Shop 1.4.0"
    git push origin v1.4.0
    git tag -v v1.4.0
  8. 8Find the commit that broke something

    git bisect start
    git bisect bad HEAD
    git bisect good v1.3.0
    git bisect reset
  9. 9Remove a secret that was committed

    git rm --cached .env && echo ".env" >> .gitignore
    git commit -m "Remove .env from repo"

    The secret is still in the history: rotate it first, then clean history with git filter-repo.

  10. 10Stop secrets before they are committed

    cat .git/hooks/pre-commit
    #!/bin/sh
    exec gitleaks protect --staged --redact
    chmod +x .git/hooks/pre-commit

Watch out

  • Never push --force to main; protect main with branch rules and required reviews.
  • git reset --hard and git clean -fd delete work you did not commit.
  • A secret in any commit is leaked — rotate it, do not only delete the file.

Files

~/.gitconfig
Your global settings.
.git/
The repository data.
.gitignore
Files Git should not track.
.git/hooks/
Local hooks (pre-commit, commit-msg).

Tips to remember

  • Small commits with clear messages ("Add …", "Fix …") make review and rollback easy.
  • Use pre-commit framework hooks: gitleaks, terraform fmt, yamllint.

Exit codes

0Success.
1Error or conflict.
128Fatal error (not a repository, bad argument).

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man git