git
Common git commands for undoing changes, branching and merging, stashing changes and more.
Undoing Changes
Fixing mistakes or rolling back changes when things go sideways.
git reset --hard [commit]
Oops! Undo changes in your working directory and reset to a specific commit. No turning back!
git reset --hard abc123
git checkout -- [file]
Don’t save that! Revert changes in a file to the last committed state.
git checkout -- file.txt
git revert [commit]
Clean slate! Create a new commit that undoes changes from a previous one.
git revert abc123
git cherry-pick [commit]
Steal a commit! Apply changes from a specific commit to your current branch.
git cherry-pick xyz789
git reflog
Time machine! See a history of all your local git actions, including resets and checkouts, to recover lost work.
git reflog
Branching & Merging
Managing different versions of your project simultaneously.
git branch
Show me the branches! List all branches in your repository.
git branch
git branch [branch-name]
Make a new branch to try something different.
git branch feature-xyz
git checkout [branch-name]
Let’s switch it up! Move to a different branch.
git checkout feature-xyz
git merge [branch-name]
Bring it together! Combine changes from another branch into your current one.
git merge feature-xyz
git rebase [branch-name]
Rewrite history! Integrate changes from one branch into another by reapplying commits. Useful for cleaning up history before merging.
git rebase main
git branch -d [branch-name]
Delete a branch (if it’s already merged).
git branch -d feature-xyz
git branch -D [branch-name]
Force delete a branch (even if it’s not merged).
git branch -D feature-xyz
Working with Remotes
Commands for collaborating with others on a shared repository.
git remote add origin [url]
Connect your local repository to a remote one.
git remote add origin https://github.com/user/repo.git
git fetch
Download changes from a remote repository without merging them.
git fetch origin
git pull
Download changes from a remote repository and merge them into your current branch.
git pull origin main
git push
Upload changes from your local repository to a remote one.
git push origin main
git remote -v
List all remote connections and their URLs.
git remote -v
git remote update
Fetch all changes from all remotes.
git remote update
Stashing
Temporarily shelve changes you’ve made to your working directory.
git stash
Stash your changes away temporarily.
git stash
git stash list
List all your stashed changes.
git stash list
git stash apply [stash-id]
Apply a specific stash to your working directory.
git stash apply stash@{0}
git stash pop
Apply the most recent stash and remove it from the stash list.
git stash pop
git stash drop [stash-id]
Remove a specific stash.
git stash drop stash@{0}
git stash clear
Remove all stashed entries.
git stash clear
Rewriting History
Changing existing commits (use with caution!).
git commit --amend
Modify the most recent commit.
git commit --amend
git rebase -i [commit]
Interactive rebase: Edit, squash, or drop commits in a range.
# Edit the last 3 commitsgit rebase -i HEAD~3
git filter-branch
Rewrite history for a range of commits (powerful, but complex). Use with extreme caution!
# Very complex, see documentationgit filter-branch --tree-filter ’...’ HEAD
Other Useful Commands
Less common but helpful git commands.
git bisect
Binary search to find the commit that introduced a bug.
git bisect start
git blame [file]
Show who made changes to each line of a file.
git blame file.txt
git config --global user.name "[name]"
Set your global username.
git config --global user.name "Your Name"
git config --global user.email "[email]"
Set your global email address.
git diff
Show changes between commits, branches, etc.
git diff main feature-xyz
git log --graph
Visualize the branch and merge history in a graph format.
git log --graph --decorate --oneline --all