Manipulating Git History
Understanding Git History Modification Techniques
Git provides powerful mechanisms for modifying and managing commit history, enabling developers to maintain clean and organized version tracking.
Common History Manipulation Methods
Method |
Purpose |
Risk Level |
git revert |
Undo specific commits |
Low |
git reset |
Modify commit pointer |
Medium |
git amend |
Modify last commit |
Low |
git interactive rebase |
Restructure commit sequence |
High |
Reverting Commits Safely
## Revert a specific commit
git revert <commit-hash>
## Create a new commit that undoes changes
git revert HEAD
## Revert multiple commits
git revert <oldest-commit>..<latest-commit>
Interactive Commit History Modification
## Start interactive rebase
git rebase -i HEAD~3
## Reorder, squash, or drop commits
## Use commands like 'pick', 'squash', 'drop'
Commit Recovery Workflow
graph TD
A[Commit History] -->|Modify| B[Temporary Changes]
B -->|Confirm| C[Updated History]
B -->|Discard| D[Original State]
Reset Strategies
## Soft reset (keeps changes in working directory)
git reset --soft HEAD~1
## Hard reset (discards all changes)
git reset --hard HEAD~1
## Mixed reset (default, unstages changes)
git reset HEAD~1
Branch Manipulation Techniques
## Create new branch from specific commit
git checkout -b <new-branch> <commit-hash>
## Move branch pointer
git branch -f <branch-name> <commit-hash>