git
Commands for version control, branches, staging, history and undoing changes.
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 abc123git checkout -- [file]
Don’t save that! Revert changes in a file to the last committed state.
git checkout -- file.txtgit revert [commit]
Clean slate! Create a new commit that undoes changes from a previous one.
git revert abc123git cherry-pick [commit]
Steal a commit! Apply changes from a specific commit to your current branch.
git cherry-pick xyz789git reflog
Time machine! 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.
git branch
Show me the branches! List all branches in your repository.
git branchgit branch [branch-name]
Make a new branch to try something different.
git branch feature-xyzgit checkout [branch-name]
Let’s switch it up! Move to a different branch.
git checkout feature-xyzgit merge [branch-name]
Bring it together! Combine changes from another branch into your current one.
git merge feature-xyzgit rebase [branch-name]
Rewrite history! Integrate changes from one branch into another by reapplying commits. Useful for cleaning up history before merging.
git rebase maingit branch -d [branch-name]
Delete a branch (if it’s already merged).
git branch -d feature-xyzgit branch -D [branch-name]
Force delete a branch (even if it’s not merged).
git branch -D feature-xyzWorking 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.gitgit fetch
Download changes from a remote repository without merging them.
git fetch origingit pull
Download changes from a remote repository and merge them into your current branch.
git pull origin maingit push
Upload changes from your local repository to a remote one.
git push origin maingit remote -v
List all remote connections and their URLs.
git remote -vgit remote update
Fetch all changes from all remotes.
git remote updateStashing
Temporarily shelve changes you’ve made to your working directory.
git stash
Stash your changes away temporarily.
git stashgit stash list
List all your stashed changes.
git stash listgit 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 popgit stash drop [stash-id]
Remove a specific stash.
git stash drop stash@{0}git stash clear
Remove all stashed entries.
git stash clearRewriting History
Changing existing commits (use with caution!).
git commit --amend
Modify the most recent commit.
git commit --amendgit rebase -i [commit]
Interactive rebase: Edit, squash, or drop commits in a range.
# Edit the last 3 commitsgit rebase -i HEAD~3git filter-branch
Rewrite history for a range of commits (powerful, but complex). Use with extreme caution!
# Very complex, see documentationgit filter-branch --tree-filter ’...’ HEADOther Useful Commands
Less common but helpful git commands.
git bisect
Binary search to find the commit that introduced a bug.
git bisect startgit blame [file]
Show who made changes to each line of a file.
git blame file.txtgit 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 config --global user.email "your.email@example.com"git diff
Show changes between commits, branches, etc.
git diff main feature-xyzgit log --graph
Visualize the branch and merge history in a graph format.
git log --graph --decorate --oneline --all