$ run as a normal user# run as root (or with sudo)
Know it before you install
Git is a program on your computer, not a server. Your repository lives in the .git folder of your project. GitHub, GitLab and Bitbucket are servers that keep a shared copy (the remote).
| Item | Value |
|---|---|
| Package | git (RHEL: git or the small git-core) |
| Service | none (Git is a command-line tool) |
| Your settings | ~/.gitconfig (global), .git/config (one repository) |
| System settings | /etc/gitconfig |
| Ignore rules | .gitignore in the project, ~/.config/git/ignore for all projects |
| Network ports | 22 (SSH remotes) or 443 (HTTPS remotes) |
What you need:
- Any Linux, macOS or Windows machine.
- An account on GitHub or GitLab if you want to push code.
Install on RHEL, Rocky, Alma and Fedora
Install on Ubuntu and Debian
apt-get update
apt-get install -y git
git --versionAt work
Ubuntu LTS can have an older Git. For the newest stable version add the Git maintainers' PPA: add-apt-repository ppa:git-core/ppa, then apt-get install -y git.
First configuration: the settings to make first
Set these once on every new machine. Git writes your name and email into every commit.
git config --global user.name "Your Name"
git config --global user.email "ID+USERNAME@users.noreply.github.com"
git config --global init.defaultBranch main
git config --global pull.rebase false
git config --global fetch.prune true
git config --global core.autocrlf input
git config --global core.editor "vim"
git config --global --list| Setting | Why |
|---|---|
user.name, user.email | Who made the commit. Use the GitHub noreply address to keep your real email private. |
init.defaultBranch main | New repositories start on main, like GitHub and GitLab. |
pull.rebase false | git pull merges and does not ask every time (true = rebase, also fine — choose one for the team). |
fetch.prune true | Remote branches that were deleted on the server disappear locally too. |
core.autocrlf input | Windows line endings (CRLF) are changed to LF when you commit. On Windows use true. |
Add a global ignore file for files that never belong in Git:
.env
*.pem
*.key
.DS_Store
.idea/
.vscode/
__pycache__/
*.tfstate*
.terraform/Connect to GitHub or GitLab with an SSH key
SSH keys are safer and easier than passwords. Make one key per computer, protect it with a passphrase and add the public key to your account.
ssh-keygen -t ed25519 -C "laptop-2026"
cat ~/.ssh/id_ed25519.pub
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... laptop-2026
ssh -T git@github.com
Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.GitHub: Settings → SSH and GPG keys → New SSH key. GitLab: Preferences → SSH Keys.
Sign your commits with the same key, so GitHub shows them as Verified (add the key a second time as a Signing key):
Daily use: the commands and the real uses
Start or copy a project:
The daily loop — branch, change, commit, push, open a pull request:
git switch -c fix-backup-script
git add backup.sh
git commit -m "Fix the backup path"
git push -u origin fix-backup-script
git switch main
git pullLook at history and changes:
git log --oneline --graph --decorate -15
git diff
git diff --staged
git show HEAD
git blame backup.shUndo safely:
git restore file.txt
git restore --staged file.txt
git commit --amend
git revert a1b2c3d
git stash && git stash popReal uses at work:
- Keep server configs, scripts and Ansible/Terraform code in Git, with review in pull requests.
- GitOps: Argo CD or Flux deploy what is on the
mainbranch. - Find when a bug came in:
git bisect. Find a lost commit:git reflog.
Make it safe
Security checklist:
- Never commit secrets (
.env, keys, passwords,*.tfstate). Use the global ignore file above. - Scan before you push:
gitleaks git(or as a pre-commit hook). - An SSH key with a passphrase, one key per device; delete keys of lost devices on GitHub.
- Signed commits (
commit.gpgsign true) and branch protection onmain(review + green CI before merge). - Use HTTPS tokens only with short expiry and the smallest scope.
Watch out
If you committed a secret, deleting the file in a new commit is not enough — it is still in the history. First rotate the secret (make a new one), then clean the history if needed.
Update and remove
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
Permission denied (publickey) | The server does not know your key. | Add ~/.ssh/id_ed25519.pub to your account; test with ssh -T git@github.com. |
! [rejected] main -> main (fetch first) | The remote has commits you do not have. | git pull, fix conflicts, then git push. |
| Merge conflict | Two people changed the same lines. | Edit the file, remove the <<<<<<< markers, git add, git commit. |
| Wrong name or email in the last commit | Settings were missing. | Fix the config, then git commit --amend --reset-author --no-edit. |
You are in 'detached HEAD' state | You checked out a commit, not a branch. | git switch -c new-branch to keep work, or git switch main. |
fatal: not a git repository | You are not in the project folder. | cd into it, or git init. |
| Lost commits after a reset | The branch moved. | git reflog, then git switch -c rescue HASH. |
Next steps
- Quick how-tos: Undo the last commit, Resolve a merge conflict, Remove a secret from history, Sign commits, Branches and PRs, Rebase, Bisect, Stash, Release tags.
- Every
gitsub-command and option: Command page: git. GitHub from the terminal: Command page: gh. - Learn Git step by step: Git summary — Branches, Undo, Security.
- Practise: Lab: branches and merges.
- Git in real work: Git and CI/CD services. Find secrets: Command page: gitleaks.
1Why use the GitHub noreply email in user.email?
Every commit shows the email. The noreply address keeps your real email private.
2You pushed a password to GitHub. What do you do first?
Rotate the password (make a new one). Removing it from the history comes after.