Skip to content

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.

Terminal window
git add .

Amend Recent Commit

Amend the most recent commit without changing its message.

Terminal window
git commit --amend --no-edit

Force Push Safely

Force push changes safely, ensuring no unexpected remote updates are overwritten.

Terminal window
git push origin main --force-with-lease

Undoing 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!

Terminal window
git reset --hard abc123

Revert File Changes

Revert changes in a file to the last committed state.

Terminal window
git checkout -- file.txt

Revert Previous Commit

Create a new commit that undoes changes from a previous one.

Terminal window
git revert abc123

Cherry Pick Commit

Apply changes from a specific commit to your current branch.

Terminal window
git cherry-pick xyz789

View Reference Log

See a history of all your local git actions, including resets and checkouts, to recover lost work.

Terminal window
git reflog

Branching & Merging

Managing different versions of your project simultaneously.

List Branches

List all branches in your repository.

Terminal window
git branch

Create Branch

Make a new branch to try something different.

Terminal window
git branch feature-xyz

Switch Branch

Move to a different branch.

Terminal window
git checkout feature-xyz

Merge Branch

Combine changes from another branch into your current one.

Terminal window
git merge feature-xyz

Rebase Branch

Integrate changes from one branch into another by reapplying commits. Useful for cleaning up history before merging.

Terminal window
git rebase main

Delete Merged Branch

Delete a branch if it is already merged.

Terminal window
git branch -d feature-xyz

Force Delete Branch

Force delete a branch even if it is not merged.

Terminal window
git branch -D feature-xyz

Working with Remotes

Commands for collaborating with others on a shared repository.

Add Remote Repository

Connect your local repository to a remote one.

Terminal window
git remote add origin https://github.com/user/repo.git

Fetch Remote Changes

Download changes from a remote repository without merging them.

Terminal window
git fetch origin

Pull Remote Changes

Download changes from a remote repository and merge them into your current branch.

Terminal window
git pull origin main

Push to Remote

Upload changes from your local repository to a remote one.

Terminal window
git push origin main

List Remotes

List all remote connections and their URLs.

Terminal window
git remote -v

Update All Remotes

Fetch all changes from all remotes.

Terminal window
git remote update

Stashing

Temporarily shelve changes you’ve made to your working directory.

Stash Changes

Stash your changes away temporarily.

Terminal window
git stash

List Stashes

List all your stashed changes.

Terminal window
git stash list

Apply Specific Stash

Apply a specific stash to your working directory.

Terminal window
git stash apply <stash_id>
Example
git stash apply stash@{0}

Pop Recent Stash

Apply the most recent stash and remove it from the stash list.

Terminal window
git stash pop

Drop Specific Stash

Remove a specific stash.

Terminal window
git stash drop <stash_id>
Example
git stash drop stash@{0}

Clear All Stashes

Remove all stashed entries.

Terminal window
git stash clear

Rewriting History

Changing existing commits (use with caution!).

Interactive Rebase

Edit, squash, or drop commits in a range.

Terminal window
git rebase -i <commit_range>
Example
git rebase -i HEAD~3

Filter Branch History

Rewrite history for a range of commits. Use with extreme caution!

Terminal window
git filter-branch --tree-filter '<command>' HEAD
Example
git filter-branch --tree-filter 'rm -f password.txt' HEAD

Other Useful Commands

Less common but helpful git commands.

Binary Search Commit

Binary search to find the commit that introduced a bug.

Terminal window
git bisect start

Show Line Authors

Show who made changes to each line of a file.

Terminal window
git blame file.txt

Set Global Username

Set your global username.

Terminal window
git config --global user.name "Your Name"

Set Global Email

Set your global email address.

Terminal window
git config --global user.email "your.email@example.com"

Show Commit Differences

Show changes between commits, branches, etc.

Terminal window
git diff <commit_or_branch_1> <commit_or_branch_2>
Example
git diff main feature-xyz

Visualize History Graph

Visualize the branch and merge history in a graph format.

Terminal window
git log --graph --decorate --oneline --all