Git: main commands to push a stable version
Quick, copy-pasteable commands for beginners to save a stable file (e.g. index.html), connect to a remote, push, and recover from common mistakes. Mainly for me to refer to until it becomes muscle memory. Install Git here.
Quick TL;DR
1) Save a stable version:
git add .
git commit -m "Stable version before changes"
git push
2) If something goes wrong
git restore "file name"
All commands at a glance
# --- Setup & Configuration ---
git init
git clone https://github.com/username/repo.git
# --- Check Status & History ---
git status
git log --oneline (shows commit IDs and messages)
# --- Staging & Committing ---
git add
git commit -m "Message"
git reset HEAD (to unstage a file)
# --- Branching & Merging ---
git branch
git checkout
git merge
# --- Remote Repositories ---
git remote -v (to view remotes)
git fetch (to get latest from remote)
git pull (to fetch + merge)
git push (to push current branch)
# --- Undo & Recovery ---
git reset --soft HEAD~1 (to undo last commit, keep changes)
git reset --hard HEAD~1 (to undo last commit, discard changes)
git revert (lets you undo a bad commit)
git checkout -- (to discard changes in a file)
git restore
# --- Ignore & Cleanup ---
echo "node_modules/" >> .gitignore (to ignore node_modules)
git clean -fd (to remove untracked files)
git stash (to save changes temporarily)
git stash apply (restore stashed changes)
git stash pop (restore and remove from stash)
# --- Tagging & Releases ---
git tag
git tag -a v1.0 -m "Version 1.0"
git push origin v1.0
Push a stable version
Initialize, check, stage, commit, and push
Initialize a repo, stage the file, create a descriptive commit (a commit is a recorded snapshot of staged changes), connect to a remote repository, and push the branch to the remote.
Environment:
- Local repo: new or existing folder
- Remote: GitHub/GitLab URL (replace with your repo URL)
- File used in examples: index.html
Steps:
1) Initialize and check files:
git init
git status
2) Stage and commit (commit = recorded snapshot with message):
git add index.html
git commit -m "Stable: update index.html"
git add . (to add all changed files)
Notes:
- The commit message should briefly describe what changed.
- Each commit has an ID (commit hash) like: a1b2c3d4e5f6...
You can see these IDs with `git log`.
3) Connect to remote and push:
git remote add origin https://github.com/username/repo.git
git push -u origin main
# If your branch is 'master' or another name, replace 'main' accordingly.
4) Verify remote history:
git log --oneline
If something goes wrong
Undo last commit but keep changes (edit and recommit)
Removes the last commit but leaves your working files and staged changes intact so you can fix and commit again.
git reset --soft HEAD~1
# After fixing files (if needed):
git add index.html
git commit -m "Fixed: corrected commit message / changes"
git push origin main
Undo last commit and discard changes
Removes the last commit and any changes in the working directory (use only if you want to throw away edits).
git reset --hard HEAD~1
# If already pushed and you need remote to match local (dangerous):
git push origin main --force
Restore a single file to last committed state
Use this to discard local edits to a file and return it to how it was in the last commit.
git checkout -- index.html
Undo a specific commit that was already pushed (safe)
`git revert` creates a new commit that reverses the changes introduced by a previous commit. This is safe for shared repos because it preserves history.
git log --oneline
# Find the commit you want to undo; copy its short ID (e.g., a1b2c3d)
git revert a1b2c3d
# Resolve any merge/revert message, then:
git push origin main
Force-overwrite remote history (last resort)
Replaces the remote branch with your local branch. This rewrites history on the remote and can break collaborators' clones. Use only if you understand consequences.
# After resetting or rewriting local commits:
git push origin main --force
Git Setup Commands
# Install Git
sudo apt install git # Linux
brew install git # macOS
git --version
# Global configuration
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global init.defaultBranch main
git config --global --list
# Create and connect repository
git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/username/repo.git
git push -u origin main
# Clone existing repository
git clone https://github.com/username/repo.git
You can set up Git with GitHub, GitLab, or self-host with my Gitea guide here.
Key Takeaways
- Use
git add,commit, andpushto save stable versions. git resetandgit revertsafely undo mistakes.- Always push descriptive commits because they form your project history.