Git Commands Guide
Git Commands
Logout - Remove your user settings
# Unset global configurations
git config --global --unset user.name
git config --global --unset user.email
git config --global --unset credential.helper
# Clear cached credentials (if using cache helper)
git credential-cache exit
Remove file or directory from staging area (without deleting from local filesystem)
git rm --cached <file or dir to be remove from staging>
Setting up global config for 1st time (Used for reference of user)
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
Initialise Repository
git init
Determine current branch
git branch
Switch Between Branches
git checkout main
Rename Branch
# Rename branch name from master to main
git branch -m master main
Create a New Branch
git checkout -b blog-post-title
Delete the Feature Branch
git branch -D feature
# Delete this branch from remote
git push origin --delete feature
Add Your Content
git add <file-name>
Status of current state of the working directory & staging area
git status
Commit Your Changes
git commit -m "message"
# After adding file to staging, update the recent commit (not creating new commit)
git commit -m "message-updated" --amend
# Keeps all your work from the last 2 commits as "staged" changes
git reset --soft HEAD~2
# Remove last N commits (replace N with number, N=2 last 2 commits were reverted)
git reset --hard HEAD~N
# --force Required when rewriting history that's already pushed
git push origin <branch-name> --force
Stash
# Stash tracked modified files
git stash
# Stash un-tracked files
git stash -u
# Stash particular file
git stash push <src/app.js>
# Stash with message
git stash push -m "fixing bug"
# List All stash
git stash list
# Shows what inside stash
git stash show stash@{0}
# Apply Stash (keep in stash list)
git stash apply stash@{2}
# Pop stash (apply & remove from stash)
git stash pop stash@{2}
# Delete specific stash
git stash drop stash@{2}
# Delete all Stash
git stash clear
Add remote URL
git remote add origin <remote repo address>
Check Current remote URL
git remote -v
Update remote repo URL & can also add PAT token → login
git remote set-url origin <remote-url>
# example adding PAT token
git remote set-url origin https://<PAT-token>@github.com/xxxxxxx/xxxxxxxxxxx.git
Track and add remote repo in Local
# Track it's up-stream & clone to local
git checkout -t origin/feature-1
# You can unset its upstream
git branch --unset-upstream
# check branch details
git branch -vv
Push to Remote Repository
# origin (default) - remote repo name
# main - remote repo branch name
git push origin main
# Remove branch from Remote
git push origin --delete feature-1
# upstream reference
git push -u origin main
Version and Commit Changes in Repository
# last 5 log of current branch
git log -5 --graph # Graph is for branch structure
# Getting restore changes from different commit to HEAD (without creating branch)
git checkout <commit-hash> <., file name u want to restore>
Undo, Revert & Unstage Changes
# undo changes from working dir/staging
git restore .
# Unstage from staging & used for rewrite history (Use Cautious)
git reset .
# Undo changes via new commit history (safe)
git revert <commit-hash>
Merge Options
Abort from merge:
git merge --abort
1. Cherry-Pick
Include Specific Changes from One Branch Without Merging All Changes
git cherry-pick
2. Merge
Merge Commit that Combines the Histories of the Branches
git merge <branch-name>
3. Rebase
Reapply Commits on Top of Another Base Commit, Resulting in a Linear History
git rebase <branch-name>
Types of Authentication to Push Changes to Remote Repo
SSH (Secure Shell) Authentication
- Uses public-private key pairs
- Provides secure, password-less access
- URL format:
git@github.com:username/repository.git
HTTPS with Personal Access Token (PAT)
- Replaces passwords with tokens generated in the Git platform (e.g., GitHub)
- Tokens are used in the URL for authentication
- URL format:
https://username:token@github.com/username/repository.git
OAuth (Open Authorization)
- Token-based authentication without directly exposing credentials
- Often used with third-party services like GitHub Apps
GPG (GNU Privacy Guard) Signing
- Signs commits and tags with GPG keys for authenticity verification
Kerberos Authentication
- A network authentication protocol commonly used in enterprise settings
- URL format:
https://example.com/repo.git
Credential Manager (Windows/Mac/Linux)
- Stores and manages credentials securely using system-specific managers like Windows Credential Manager, macOS Keychain, or
git-credential-libsecretfor Linux
Git Large File Storage (LFS)
Git LFS (Large File Storage): An open-source Git extension for handling large files.
Purpose: Manages large files like audio, videos, datasets, and graphics efficiently.
How it Works:
- Replaces large files in Git with small text pointers
- Stores actual file content on a remote server (e.g., GitHub or GitHub Enterprise)
Feel free to share and spread the knowledge! 🌟 Enjoy Learning! 😊

Top comments (0)