Suggest an editImprove this articleRefine the answer for “Can you change the commit history?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)Yes, the commit history in Git can be changed using `git commit --amend`, `git rebase`, and `git reset`. **Key point:** it is only safe to do this for local or not-yet-published commits, while on public branches history is not rewritten and `git revert` is used instead.Shown above the full answer for quick recall.Answer (EN)Image## Why history can be changed at all Git is a **distributed system**. The commit history is not something "sacred," it is just a chain of objects, and Git allows you to: - rewrite commits - delete them - combine them - change the message and the author **As long as commits have not reached a shared repository**, you are free to do whatever you want with them. --- ## How history can be changed (main ways) ### 1. `git commit --amend` Used to change the **last commit**: - fix the message - add a forgotten file ```bash git commit --amend ``` This **rewrites the last commit**, creating a new one. --- ### 2. `git rebase` (especially interactive) The most powerful tool for changing history. It lets you: - change commit messages - combine commits (squash) - change the order - delete commits ```bash git rebase -i HEAD~3 ``` Interviewers really like this one, as a sign of solid Git knowledge. --- ### 3. `git reset` Lets you **move the branch pointer back**: - `--soft` - `--mixed` - `--hard` For example: ```bash git reset --hard HEAD~1 ``` Can **delete commits** if nothing else references them. --- ### 4. `git revert` (an important difference!) `git revert` **does not change the history**, it adds a **new commit** that undoes the changes of the old one. This is a safe way for public branches. --- ## The main rule (the key point for an interview) **You cannot rewrite history that is already used by others**. A phrasing interviewers really like: > History can safely be changed only on local or not-yet-published branches. --- ## Why this is dangerous in a shared repository If you changed the history and did `push --force`: - your colleagues' history breaks - conflicts appear - you can lose someone else's work --- ## Short answer for an interview > Yes, the commit history in Git can be changed, using `commit --amend`, `rebase`, `reset`. But it is safe only for local or unshared commits. On public branches, history is not rewritten, `git revert` is used instead. --- ## A common candidate mistake "You can't change history in Git" "You always can, if you're careful" **You can, but with a clear understanding of the context and the consequences**For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.