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
cloneURL- Copy a remote repository.
status- What changed, what is staged.
addFILE/add-p- Stage changes (by parts with -p).
commit-mMSG- Save a commit.
log--oneline--graph- Show history.
diff[--staged]- Show changes.
switch-cBRANCH/switchBRANCH- Create / change branch.
pull/fetch/push- Get / send changes.
mergeBRANCH- Join a branch into the current one.
rebaseBRANCH- Replay your commits on top of another branch.
restoreFILE/restore--stagedFILE- Undo changes / unstage.
revertCOMMIT- New commit that undoes an old one (safe on shared branches).
reset--soft|--hardCOMMIT- Move the branch (hard deletes work!).
stash/stashpop- Put work aside and bring it back.
tag-sv1.4.0- Make a (signed) tag.
blameFILE/bisect- Who changed a line / find the bad commit.
remote-v/remoteaddNAMEURL- Manage remotes.
cherry-pickCOMMIT- Copy one commit to this branch.
Options 9
Most used
Useful settings and flags
Nothing matches this filter.
Examples 10
$ run as a normal user# run as root (or with sudo)
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 true2Start work on a feature branch
3See what happened
git status git log --oneline --graph --decorate -15 git diff main...feat/add-health-check --stat4Update your branch with main
git fetch origin git rebase origin/main git push --force-with-lease5Undo safely
git restore app/config.py git restore --staged app/config.py git revert 3f2a91c git commit --amend -m "Better message"6Put work aside
git stash push -m "half done" git switch main && git pull git switch - && git stash pop7Tag a release
git tag -s v1.4.0 -m "Shop 1.4.0" git push origin v1.4.0 git tag -v v1.4.08Find the commit that broke something
git bisect start git bisect bad HEAD git bisect good v1.3.0 git bisect reset9Remove 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.
10Stop secrets before they are committed
Nothing matches this filter.
Watch out
- Never
push --forcetomain; protectmainwith branch rules and required reviews. git reset --hardandgit clean -fddelete 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-commitframework hooks: gitleaks, terraform fmt, yamllint.
Exit codes
0 | Success. |
1 | Error or conflict. |
128 | Fatal error (not a repository, bad argument). |