git
Commands for version control, branches, staging, history and undoing changes.
Update Last Commit and Force Push
Stage all changes, amend the latest commit and safely force push to remote.
Stage All Changes
Stage all modified and new files.
git add .Amend Recent Commit
Amend the most recent commit without changing its message.
git commit --amend --no-editForce Push Safely
Force push changes safely, ensuring no unexpected remote updates are overwritten.
git push origin main --force-with-leaseUndoing Changes
Fixing mistakes or rolling back changes when things go sideways.
Reset Hard to Commit
Undo changes in your working directory and reset to a specific commit. No turning back!
git reset --hard abc123Revert File Changes
Revert changes in a file to the last committed state.
git checkout -- file.txtRevert Previous Commit
Create a new commit that undoes changes from a previous one.
git revert abc123Cherry Pick Commit
Apply changes from a specific commit to your current branch.
git cherry-pick xyz789View Reference Log
See a history of all your local git actions, including resets and checkouts, to recover lost work.
git reflogBranching & Merging
Managing different versions of your project simultaneously.
List Branches
List all branches in your repository.
git branchCreate Branch
Make a new branch to try something different.
git branch feature-xyzSwitch Branch
Move to a different branch.
git checkout feature-xyzMerge Branch
Combine changes from another branch into your current one.
git merge feature-xyzRebase Branch
Integrate changes from one branch into another by reapplying commits. Useful for cleaning up history before merging.
git rebase mainDelete Merged Branch
Delete a branch if it is already merged.
git branch -d feature-xyzForce Delete Branch
Force delete a branch even if it is not merged.
git branch -D feature-xyzWorking with Remotes
Commands for collaborating with others on a shared repository.
Add Remote Repository
Connect your local repository to a remote one.
git remote add origin https://github.com/user/repo.gitFetch Remote Changes
Download changes from a remote repository without merging them.
git fetch originPull Remote Changes
Download changes from a remote repository and merge them into your current branch.
git pull origin mainPush to Remote
Upload changes from your local repository to a remote one.
git push origin mainList Remotes
List all remote connections and their URLs.
git remote -vUpdate All Remotes
Fetch all changes from all remotes.
git remote updateStashing
Temporarily shelve changes you’ve made to your working directory.
Stash Changes
Stash your changes away temporarily.
git stashList Stashes
List all your stashed changes.
git stash listApply Specific Stash
Apply a specific stash to your working directory.
git stash apply <stash_id>git stash apply stash@{0}Pop Recent Stash
Apply the most recent stash and remove it from the stash list.
git stash popDrop Specific Stash
Remove a specific stash.
git stash drop <stash_id>git stash drop stash@{0}Clear All Stashes
Remove all stashed entries.
git stash clearRewriting History
Changing existing commits (use with caution!).
Interactive Rebase
Edit, squash, or drop commits in a range.
git rebase -i <commit_range>git rebase -i HEAD~3Filter Branch History
Rewrite history for a range of commits. Use with extreme caution!
git filter-branch --tree-filter '<command>' HEADgit filter-branch --tree-filter 'rm -f password.txt' HEADOther Useful Commands
Less common but helpful git commands.
Binary Search Commit
Binary search to find the commit that introduced a bug.
git bisect startShow Line Authors
Show who made changes to each line of a file.
git blame file.txtSet Global Username
Set your global username.
git config --global user.name "Your Name"Set Global Email
Set your global email address.
git config --global user.email "your.email@example.com"Show Commit Differences
Show changes between commits, branches, etc.
git diff <commit_or_branch_1> <commit_or_branch_2>git diff main feature-xyzVisualize History Graph
Visualize the branch and merge history in a graph format.
git log --graph --decorate --oneline --all