Skip to content

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!

Terminal window
git reset --hard abc123

git checkout -- [file]

Don’t save that! Revert changes in a file to the last committed state.

Terminal window
git checkout -- file.txt

git revert [commit]

Clean slate! Create a new commit that undoes changes from a previous one.

Terminal window
git revert abc123

git cherry-pick [commit]

Steal a commit! Apply changes from a specific commit to your current branch.

Terminal window
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.

Terminal window
git reflog

Branching & Merging

Managing different versions of your project simultaneously.

git branch

Show me the branches! List all branches in your repository.

Terminal window
git branch

git branch [branch-name]

Make a new branch to try something different.

Terminal window
git branch feature-xyz

git checkout [branch-name]

Let’s switch it up! Move to a different branch.

Terminal window
git checkout feature-xyz

git merge [branch-name]

Bring it together! Combine changes from another branch into your current one.

Terminal window
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.

Terminal window
git rebase main

git branch -d [branch-name]

Delete a branch (if it’s already merged).

Terminal window
git branch -d feature-xyz

git branch -D [branch-name]

Force delete a branch (even if it’s not merged).

Terminal window
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.

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

git fetch

Download changes from a remote repository without merging them.

Terminal window
git fetch origin

git pull

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

Terminal window
git pull origin main

git push

Upload changes from your local repository to a remote one.

Terminal window
git push origin main

git remote -v

List all remote connections and their URLs.

Terminal window
git remote -v

git remote update

Fetch all changes from all remotes.

Terminal window
git remote update

Stashing

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

git stash

Stash your changes away temporarily.

Terminal window
git stash

git stash list

List all your stashed changes.

Terminal window
git stash list

git stash apply [stash-id]

Apply a specific stash to your working directory.

Terminal window
git stash apply stash@{0}

git stash pop

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

Terminal window
git stash pop

git stash drop [stash-id]

Remove a specific stash.

Terminal window
git stash drop stash@{0}

git stash clear

Remove all stashed entries.

Terminal window
git stash clear

Rewriting History

Changing existing commits (use with caution!).

git commit --amend

Modify the most recent commit.

Terminal window
git commit --amend

git rebase -i [commit]

Interactive rebase: Edit, squash, or drop commits in a range.

Terminal window
# Edit the last 3 commits
git rebase -i HEAD~3

git filter-branch

Rewrite history for a range of commits (powerful, but complex). Use with extreme caution!

Terminal window
# Very complex, see documentation
git 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.

Terminal window
git bisect start

git blame [file]

Show who made changes to each line of a file.

Terminal window
git blame file.txt

git config --global user.name "[name]"

Set your global username.

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

git config --global user.email "[email]"

Set your global email address.

Terminal window
git config --global user.email "[email protected]"

git diff

Show changes between commits, branches, etc.

Terminal window
git diff main feature-xyz

git log --graph

Visualize the branch and merge history in a graph format.

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