Removing Untracked Files
-
git clean -f: Remove untracked files from the working directory. 1️⃣ → ❌-d- Remove untracked directories too.-n- Preview what would be removed without actually deleting anything.
Restoring Files
-
git restore <file>...: Discard unstaged changes in modified files. 1️⃣ → ❌--staged- Unstage files (move them from the staging area back to unstaged). 2️⃣ → 1️⃣--source <commit_hash>- Restore files to their state right after the specified commit. 3️⃣ → ❌ → 1️⃣
It doesn’t matter if the given commit was related to the targeted files, it restores them exactly as they were at that point.
Reverting Commits
git revert <commit_hash>: Create a new commit that reverses the changes introduced by the specified commit.
You may be asked to resolve conflicts. In such cases, you can use
git revert --abort to cancel the process or resolve them.Resetting to a Commit
git reset <commit_hash>: Rewind the repository to the specified commit state.
Updates the branch reference to point to the given commit.
--mixed(default) - Keep changes in the working directory as unstaged modifications. → 1️⃣--soft- Keep changes staged. → 2️⃣--hard- Discard everything completely. → ❌
Using Checkout
git checkout HEAD <file>...: Discard uncommitted changes. 2️⃣, 1️⃣ → ❌git checkout <commit_hash>: Travel to a specific commit in the history (to the state just after the commit).
- This puts your repository in a
detached HEADstate. - Git updates your project’s file tree to match the state of
HEAD.
What you can do in detached HEAD state:
- Explore the repository.
- Create a new branch from here (
git switch -c <new_name>).- This reattaches
HEADand sets the new branch’s starting point to the checked-out commit. It’s like starting a new line of work from an earlier point in history.
- This reattaches
- End the process (travel back) by switching to a branch.