When you use git reset --hard <earlier_commit_hash>, here's what happens to the commits that were after the target commit on your current branch:
They are NOT immediately deleted from the repository.
Git is a content-addressable filesystem. Commits, trees, and blobs (file contents) are stored as individual objects in the .git/objects directory.
When you do git reset --hard, those objects still exist within your repository's object database.
They become "unreachable" by your current branch.
The git reset --hard command moves your current branch pointer (e.g., main, master, dev) and your HEAD pointer to the specified <earlier_commit_hash>.
It also updates your working directory and staging area (index) to match the state of that earlier commit.
Because the branch pointer no longer points to them, and subsequent commits don't chain back to them, they are effectively "orphaned" or "floating" within the repository.
If you run git log on your current branch, you will no longer see those commits. They are no longer considered part of that branch's history.
They are recoverable for a period of time.
The reflog is your savior! Git maintains a "reflog" (reference log) which records where your HEAD (and other references like branch tips) has been.
Every time you commit, reset, merge, rebase, or checkout a branch, Git makes an entry in the reflog.
You can view your reflog using git reflog. This will show you a history of your HEAD movements, including the commit hashes you were on before the reset.
To recover:
Find the commit hash of the "lost" commit in your git reflog output.
You can then create a new branch pointing to it: git branch <new_branch_name> <commit_hash>
Or you can reset back to it (if you made a mistake with the previous reset): git reset --hard <commit_hash>
Or cherry-pick specific changes from it: git cherry-pick <commit_hash>
They will eventually be garbage collected (deleted).
Git has a built-in garbage collection mechanism (git gc).
Objects that are unreachable by any reference (branches, tags, or reflog entries) after a certain period of time (typically 30-90 days for reflog entries, though this is configurable) are considered "dangling" and will be pruned by git gc.
So, while they are not immediately deleted by git reset --hard, they will eventually be removed from the .git/objects directory if no reference continues to point to them. This cleanup process is usually run automatically in the background or when you run certain Git commands.
In summary:
git reset --hard rewrites the history of your current branch locally.
Commits after the target commit are no longer part of that branch's history and become unreachable by it.
They remain in the repository's object database.
They are recoverable using git reflog for a grace period.
After that grace period, they will be garbage collected and permanently removed if no other references point to them.
This is why git reset --hard is a powerful command that should be used with caution, especially if you haven't pushed your work yet, as it discards local changes and history.
User
What is best practice for going back to an earlier commit without discarding changes from later commits?
Assistant
You're looking for ways to manipulate your Git history or navigate to earlier states without losing the content of later commits. git reset --hard is powerful but destructive for uncommitted changes and rewrites history.
Here are the best practices, depending on what exactly you want to achieve:
1. If you want to undo specific changes from later commits (and preserve the commit history)
This is best for when commits have already been pushed to a shared remote.
Command:git revert <commit_hash>
When to use:
You want to undo the changes introduced by one or more specific commits.
You have already pushed these commits to a shared remote repository.
You want to create new commits that explicitly reverse the old ones, keeping a clear audit trail.
What it does: Creates a brand new commit (or series of commits) that undoes the changes of the specified commit(s). The original commits remain in the history.
Pros:
Safe for shared history: Does not rewrite history, so it's safe to use on commits that others might have already pulled.
Provides a clear record: Shows exactly when and why changes were undone.
Cons:
Can clutter history with "revert" commits if used excessively.
Doesn't actually remove the original commit from history.
Example:
bash
git log --oneline # Identify the commit you want to undo (e.g., c1a2b3c "Add feature X")
git revert c1a2b3c
# Git will open your editor to let you review/modify the revert commit message.
# Save and close the editor. A new commit is created that undoes c1a2b3c.
To revert a range of commits: git revert HEAD~3..HEAD (reverts the last 3 commits). Use --no-commit to stage all reverted changes and commit them as one: git revert --no-commit c1a2b3c..d4e5f6g
2. If you want to move your branch pointer back, but keep all later changes staged (ready to re-commit)
This is for local history manipulation, before pushing.
User
I created a new branch and git reset to an earlier commit. I would like to compare changes in this branch to the scripts from the earlier branch. How do I do this?
Assistant
When you git reset --hard a branch to an earlier commit, you effectively move that branch's pointer back to that specific commit, and your working directory and staging area are updated to match it. The commits that were on that branch after the reset target are now "unreachable" by that branch pointer.
The "earlier branch" in your question could mean a couple of things:
The state of this same branch (my-new-branch) before you did the git reset --hard. This is the most common scenario for comparison after a reset.
Another existing branch (e.g., main, develop) from which your my-new-branch was originally created.
Let's cover both scenarios.
Scenario 1: Comparing to the state of your current branch before the git reset --hard
This is where git reflog is your best friend. git reflog records where your HEAD (and thus your branch) has been.
Identify the commit hash before the reset:
Open your terminal and type: git reflog
You'll see a list of actions. Look for an entry similar to:
code
a1b2c3d HEAD@{0}: reset: moving to earlier_commit_hash
e4f5g6h HEAD@{1}: commit: My last commit before the reset
The HEAD@{1} (or my-new-branch@{1}) entry points to the commit your branch was on just before the reset. Note down its hash (e.g., e4f5g6h) or simply use HEAD@{1} directly.
Compare your current branch state (after reset) to its previous state:
To see the changes that were discarded (i.e., what HEAD@{1} had that HEAD currently doesn't):
bash
git diff HEAD HEAD@{1}
# Or, if you noted the hash:
git diff HEAD e4f5g6h
This command shows you the changes that were present in your branch before the reset but are no longer there after the reset. Essentially, it's .
Command:
git reset --soft <earlier_commit_hash>
When to use:
You want to go back to an earlier point in time, but keep all the changes from the commits after that point as staged changes in your index.
You intend to re-commit these changes, possibly squashing them into a single new commit, or splitting them differently.
You have NOT pushed these later commits to a shared remote.
What it does:
Moves your HEAD and current branch pointer to earlier_commit_hash.
Keeps your working directory exactly as it is.
Keeps the changes of the "discarded" commits in your staging area (index).
Pros:
Allows you to easily re-commit a large set of changes as one.
Useful for cleaning up a messy series of commits before pushing.
Cons:
Rewrites history: Do NOT use on commits that have been pushed to a shared remote and others might be working on.
Example:
code
# Current history: A -- B -- C -- D (HEAD)
# Goal: Go back to B, but keep C and D's changes
git log --oneline # Identify B's hash (e.g., b1b2b3b)
git reset --soft b1b2b3b
# Now: A -- B (HEAD)
# Your working directory and staging area contain the changes from C and D.
git status # Shows all changes from C and D as staged.
git commit -m "New single commit with combined changes from C and D"
# Now: A -- B -- E (HEAD, where E is the new combined commit)
3. If you want to move your branch pointer back, and keep all later changes in your working directory (unstaged)
Also for local history manipulation.
Command:git reset --mixed <earlier_commit_hash> (This is the default for git reset)
When to use:
Similar to --soft, but you want the changes from the later commits to be unstaged in your working directory.
Useful if you want to selectively stage and commit parts of the later changes.
You have NOT pushed these later commits to a shared remote.
What it does:
Moves your HEAD and current branch pointer to earlier_commit_hash.
Keeps your working directory exactly as it is.
Unstages the changes that were part of the "discarded" commits.
Pros:
Gives you granular control over re-staging and re-committing.
Cons:
Rewrites history: Do NOT use on commits that have been pushed to a shared remote.
Example:
code
# Current history: A -- B -- C -- D (HEAD)
# Goal: Go back to B, but keep C and D's changes unstaged
git log --oneline # Identify B's hash (e.g., b1b2b3b)
git reset b1b2b3b # or git reset --mixed b1b2b3b
# Now: A -- B (HEAD)
# Your working directory contains the changes from C and D, but they are unstaged.
git status # Shows all changes from C and D as unstaged.
git add <specific_file> # Add individual files or chunks
git commit -m "New commit with some of the changes"
4. If you want to reorder, squash, edit, or drop specific commits (rebase for history cleanup)
This is the most flexible and powerful option for local history cleanup.
Command:git rebase -i <earlier_commit_hash> (or HEAD~N)
When to use:
You want to clean up your commit history before pushing to a shared remote.
You want to combine multiple small commits into one (squash).
You want to split a large commit into smaller ones (edit).
You want to reorder commits.
You want to delete a commit from history.
What it does: Git opens an interactive editor showing a list of commits. You can then change pick to squash, fixup, edit, drop, or reword next to each commit. Git then reapplies those commits (or their combined changes) on top of the base commit you specified.
Pros:
Extremely powerful for creating a clean, logical history.
Cons:
Rewrites history: Absolutely do NOT use on commits that have been pushed to a shared remote unless you know what you're doing and coordinate with your team (e.g., git push --force-with-lease).
Can lead to merge conflicts if commits touch the same lines.
Requires some understanding of Git.
Example:
code
# Current history: A -- B -- C -- D (HEAD)
# Goal: Squash C and D into B.
git rebase -i HEAD~2 # This operates on C and D (the last 2 commits)
# An editor opens with:
# pick <C_hash> Commit C message
# pick <D_hash> Commit D message
#
# Change it to:
# pick <C_hash> Commit C message
# squash <D_hash> Commit D message
# Save and close. Git will then prompt you to write a new commit message for the squashed C+D.
# Result: A -- B -- E (HEAD, where E is the squashed C+D)
5. If you want to apply specific changes from a commit onto your current branch
Command:git cherry-pick <commit_hash>
When to use:
You're on branch feature-A and you realize a commit from feature-B would be useful here.
You want to bring a single commit (or a few selected commits) from another branch or from a "lost" history into your current branch without merging.
What it does: Takes the changes introduced by a specific commit and applies them as a new commit on top of your current HEAD.
Pros:
Does not rewrite history on the source branch.
Allows surgical application of specific changes.
Cons:
Can lead to duplicate changes if not managed well.
Can introduce merge conflicts.
Example:
bash
# You're on `main`. You like a specific commit from `feature-branch`.
git checkout main
git log feature-branch --oneline # Find the commit (e.g., e1e2e3e "Fix typo in docs")
git cherry-pick e1e2e3e
# A new commit, identical in changes to e1e2e3e, is created on `main`.
6. If you just want to look at an earlier commit's state without changing your branch
You want to inspect the files, run tests, or build the project as it was at a specific past commit.
You don't want to modify your current branch or history.
What it does: Puts you into a "detached HEAD" state at that commit. Your branch pointer does not move. You can look around, but any new commits you make will not be part of your previous branch's history unless you explicitly create a new branch from this detached HEAD.
Pros:
Safe and non-destructive to your branch.
Great for historical analysis.
Cons:
If you forget you're in a detached HEAD state and start committing, those commits can become "lost" if you don't create a branch from them before checking out another branch. (But they can be recovered via git reflog).
Example:
bash
git checkout a1b2c3d # Go to this commit
# You are now in a detached HEAD state.
# Inspect files, run code.
git checkout your_branch_name # Go back to your previous branch
General Safety Precaution: git reflog is your friend!
No matter which method you use, if you ever feel like you've lost something, the git reflog command is your first line of defense. It records nearly every action that changes HEAD and will show you the commit hashes you were on. You can always git reset --hard <reflog_entry_hash> to jump back to a previous state.
Always ensure you understand the implications of history-rewriting commands (git reset, git rebase -i) before using them, especially when working in a collaborative environment.
git diff <new_state> <old_state>
To see the changes from the earlier commit your branch is now at (useful if you want to understand what the "target" of the reset was):
bash
git diff <earlier_commit_hash>^ HEAD
# Where <earlier_commit_hash> is the commit you reset to.
# The '^' means "the parent of this commit".
This shows you what changes the target commit (earlier_commit_hash) introduced compared to its parent.
Scenario 2: Comparing your reset branch to another existing branch (e.g., main)
This is a standard git diff between two different branches.
Let's assume your current branch is my-new-branch (which you reset) and you want to compare it to main.
To see changes that main has that your my-new-branchdoesn't (i.e., what you're "missing" compared to main):
bash
git diff my-new-branch main
# or
git diff <earlier_commit_hash> main
# where <earlier_commit_hash> is the current HEAD of my-new-branch
This shows you all the commits and changes present in main that are not present in your my-new-branch (because your branch was reset).
To see changes that your my-new-branch has that maindoesn't (less likely after a reset unless you added new commits after the reset):
bash
git diff main my-new-branch
If you only did a reset and haven't committed anything since, this diff will likely be empty or only show changes from the very first commit if my-new-branch's earliest commit is different from main's earliest common ancestor.
Useful git diff options:
git diff --stat: Shows a summary of file changes (additions/deletions).
git diff --name-only: Lists only the names of files that changed.
git diff --color-words: Shows diffs word-by-word instead of line-by-line, very useful for text/prose.
git diff <commit1> <commit2> -- <path/to/file.js>: To compare a specific file between two points.
Choose the comparison method that best reflects what you're trying to understand! The git reflog approach is usually what people need after a reset --hard.