https://git-scm.com/download/linux
sudo apt update
sudo apt install git
git --version
.gitignore - Specifies intentionally untracked files to ignore
.env
*.log
.terraform/
*.tfstate
*.tfstate.backup
Add .gitignore before the first git add . — files already committed stay tracked.
git config --global user.name "Billy Bob"
git config --global user.email "b.bob@domain.com"
git config --global init.defaultBranch main # New repositories start with branch "main"
git config -l # Show config
--global settings are stored in ~/.gitconfig and apply to all repositories of the current user.
mkdir gitproject01
cd gitproject01
git init . # Create an empty Git repository or reinitialize an existing one
git status # Show the working tree status
git add . # Stage all new and modified files — check "git status" first
git commit -m "version 1" # Record changes to the repository
git log -2 -p # Show the last 2 commits with diffs
git restore file # Discard unstaged changes in file (cannot be undone)
git restore --staged file # Unstage file, keep the changes
git diff --cached # View the changes staged for the next commit
git switch --detach a1b2c3d # Check out commit a1b2c3d (detached HEAD, view only)
git switch main # Return to the latest commit of branch main
git reset --soft HEAD~3 # Undo the last 3 commits, keep their changes staged
git reset --hard HEAD~3 # Remove the last 3 commits and discard uncommitted changes (uncommitted work is lost for good)
git commit --amend # Edit the most recent commit; don't amend commits already pushed
git clone https://github.com/user/repo.git
cd repo
git add .
git commit -m "version 1"
git push origin main
ssh-keygen -t ed25519 -C "b.bob@domain.com" # Create a key (set a passphrase)
cat ~/.ssh/id_ed25519.pub # Add it in GitHub > Settings > SSH and GPG keys
ssh -T git@github.com # Test the connection
git remote -v # List remotes
git remote set-url origin git@github.com:user/repo.git
git remote set-url origin git@github.com:user/repo.git
git branch # Existing branches are listed
git branch branch_name # Create the new branch, but it will not switch the working tree to it
git switch branch_name # To switch to the new branch
git checkout branch_name # The same
git checkout -b branch_name # Create branch and switch to the new branch
git merge branch_name # Merge branch_name into the current branch (switch to main first)
git branch -d branch_name # Delete a merged branch
git branch -D branch_name # Force delete — unmerged commits can only be recovered via git reflog
git push origin --delete branch_name # Delete remote branch
git clone git@github.com:user/repo.git
cd repo
git switch main
git pull # Get the latest changes
git switch -c my_branch # Create a branch and switch to it
git add .
git commit -m "some changes"
git push -u origin my_branch # Create my_branch on the remote and push
git tag # Show all tags
git tag -a v1.1.0 -m "Release 1.1.0" # Annotated tag (recommended for releases)
git push origin v1.1.0 # Push a tag
git switch --detach v1.1.0 # View the code at v1.1.0 (detached HEAD)