Git Commands

Git Bash Basics

Ctrl + Insert - To Copy  
Shift + Insert - To Paste

Get Status

git status - Displays the current state of your Git repository, specifically comparing the working directory (local files) and the staging area (index) against the last committed snapshot. It helps you identify which files are staged for commit, modified but unstaged, or entirely untracked.

Initial Setup

git init
git add -all - Stage all files
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/gkovacs75/NewRepo.git
git push -u origin main

Staging

The staging environment (or staging area) is like a waiting room for your changes. You use it to tell Git exactly which files you want to include in your next commit.

git add <file> - Stage a file

git add --all or git add -A - (Recommended) Stage all changes(new, modified, and deleted files) across the entire working tree, regardless of your current directory.

git add . - Stages all changes (new, modified, and deleted files) but is limited to the current directory and its subdirectories.  If executed from a subfolder, it will not stage changes in parent directories or other branches of the repository tree.

git add * - (Avoid) Relies on shell wildcard expansion rather than Git's internal tracking.  It typically adds only files in the current directory, excludes hidden files (those starting with a dot), and ignores .gitignore rules, which can lead to unintended files being staged or deleted files being missed.

git status - See what is staged
git restore --staged <file> - Unstage a file

Commit

git commit -m "message" - Commit staged changes with a message
git commit -a -m "message" - Commit all tracked changes (skip staging)

History

git log [--oneline] [--stat] - See commit history

Tags

A tag in Git is like a label or bookmark for a specific commit. Tags are most often used to mark important points in your project history, like releases (v1.0 or v2.0).

Pushing commits with git push does not push your tags! You must push tags explicitly as shown above.

git tag <tagname> - Create a lightweight tag
git tag -a <tagname> -m "message" - Create an annotated tag
git tag <tagname> <commit-hash> - Tag a specific commit
git tag - List tags
git show <tagname> - Show tag details

Git Stash

Sometimes you need to quickly switch tasks or fix a bug, but you're not ready to commit your work.

What gets stashed?

To stash untracked files too, use git stash -u (or --include-untracked).

git stash - Stash your changes
git stash push -m "message" - Stash with a message
git stash list - List all stashes
git stash branch <branchname> - Create a branch from a stash
git stash apply - apply the most recent
git stash pop - apply the latest stash and remove it from the stack
git stash drop
git stash clear

Undo

git revert - undoes a previous commit by creating a new commit that reverses the changes

git revert HEAD - Revert the latest commit
git revert <commit> - Revert a specific commit
git revert HEAD~2 - Revert a commit further back in history
git revert --no-edit - Skip commit message editor
git log --oneline - Show commit history