Sub topic 11
Staying safe: shared branches and untracked files
This chapter assumes you know git init, add, commit, status, and log. It also assumes you know branches and the difference between --soft, --mixed, and --hard. Reset is safe when you know which data it can lose and which history other people depend on. This chapter covers both.
Rule 1: Do not reset history that others have
git reset moves your local branch pointer. It does not change the remote. If you reset a branch that you already pushed, your branch and the remote branch no longer agree.
Suppose main on the remote has commits A, B, C. You run git reset --hard HEAD~1. Your local main now ends at B. Git sees that the remote has a commit you lack, so a normal git push is rejected.
Forcing the push removes C from the remote. Teammates who already pulled C still have it. Their next git pull tries to merge their C with your new history, and conflicts and duplicate commits follow.
Use this rule of thumb:
- Commits that exist only on your machine can be reset freely.
- Commits you pushed to a shared branch should be undone with
git revert, which adds a new commit instead of removing one. - If you must rewrite a pushed branch that only you use, such as a feature branch, prefer
git push --force-with-lease. It refuses to overwrite commits you have not seen.
git push --force-with-lease origin my-featureRule 2: Know what `--hard` cannot recover
git reset --hard overwrites tracked files in the working directory. Committed work can be recovered with the reflog, which the previous chapter covers. Uncommitted changes to tracked files cannot. Git never stored them as commits, so no reference points to them.
Check before you run it:
git status
git diffIf you want a safety net, save the changes first:
git stash
git reset --hard HEAD~1
git stash popAlternatively, make a temporary commit. A commit is recoverable, and you can reset it away later.
Rule 3: Untracked files are a separate case
Untracked files are files Git has never been told about. git reset --hard leaves them alone, because it only touches files that are tracked or staged.
This is the safe direction. It also has a side effect: after a reset, old files may remain in your working directory. For example, if you reset past a commit that added notes.txt, then with --hard the file is removed only if Git was tracking it. If you had unstaged it first, it stays as untracked.
Removing untracked files is the job of git clean, which is destructive and has no undo. Preview first:
git clean -nThe -n flag lists what would be deleted without deleting it. Only after reading the list should you run git clean -f.
There is one more case. Files that are ignored via .gitignore are also left alone by reset. They survive unless you add -x to git clean.
| Item | Affected by `reset --hard`? | Recoverable? |
|---|---|---|
| Committed changes | Branch moves away from them | Yes, via reflog |
| Staged changes (tracked files) | Overwritten | Not reliably |
| Unstaged changes (tracked files) | Overwritten | No |
| Untracked files | Not touched | Not applicable |
| Ignored files | Not touched | Not applicable |
Staged changes are a partial exception. When you run git add, Git writes the file contents as objects. Those objects can sometimes be found with git fsck --lost-found, but treat that as a last resort.
A pre-reset checklist
- Run
git statusto see unstaged, staged, and untracked files. - Run
git log --oneline origin/main..HEADto list commits that are not pushed yet. Changemainto your branch name. - If any commits you want to remove are already pushed, use
git revertinstead. - If you have uncommitted work you want to keep, stash it or commit it.
- Prefer
--mixedor--softwhen unsure. They never delete working directory content.
Exercise
You ran git reset --hard HEAD~2 on a branch that you pushed yesterday, and your teammate has already pulled it. You have no uncommitted changes. What is the safe way to undo the two commits for everyone?
Show answer
Do not force-push the reset. Recover your local state with git reset --hard ORIG_HEAD (or use the reflog), then run git revert HEAD~1..HEAD, which creates new commits that undo the two commits. Push normally. Your teammate pulls without conflicts, because no history was rewritten.
Exercise
You have an untracked file scratch.txt and an unstaged edit to the tracked file.txt. You run git reset --hard. Which of the two is lost?
Show answer
The edit to file.txt is lost, because --hard overwrites tracked files. scratch.txt is untracked, so reset does not touch it.